Skip to content

Commit efe97be

Browse files
Rafael Koch PeresRafael Koch Peres
Rafael Koch Peres
authored and
Rafael Koch Peres
committed
Added tutorial for tilt sensor.
1 parent c53812f commit efe97be

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

sensor-tilt/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Sensor KY-020: Tilt switch sensor
2+
3+
![ky020](images/ky020.jpg)
4+
5+
The pins for the extension board, where (`sensor: board`) - following the [example code](tilt.py):
6+
* -: G
7+
* (middle pin): V
8+
* S: D15

sensor-tilt/images/ky020.jpg

1010 KB
Loading

sensor-tilt/tilt.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
read state from tilt switch sensor using callback functions
3+
4+
"""
5+
import RPi.GPIO as GPIO
6+
import time
7+
8+
tiltPin = 15 # S connected to D15
9+
10+
GPIO.setwarnings(False)
11+
GPIO.setmode(GPIO.BCM)
12+
GPIO.setup(tiltPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
13+
14+
lastState = GPIO.input(tiltPin)
15+
16+
def eventTiltSwitch(e):
17+
print("Tilted!")
18+
19+
GPIO.add_event_detect(tiltPin, GPIO.BOTH, bouncetime = 200, callback = eventTiltSwitch)
20+
21+
while(True):
22+
time.sleep(0.1)
23+
#print(GPIO.input(tiltPin))

0 commit comments

Comments
 (0)