Figure 11-13. Wiring a PIR motion detector
Open an editor (nano or IDLE) and paste in the following code. As with all the program
examples in this book, you can also download the program from the Code section of
the Raspberry Pi Cookbook website, where itis called pir_py.
import RPi.cPIO as cPIO
Anpert tine
PIO. setmode(GPI0.8cH)
P10. setup(18, GPTO.IN)
while True:
input_state - GPIO.input(18)
if input_state == True:
print( Hotton Detected")
time. sleep(1)
‘The program simply prints out the state of the GPIO input 18.
$ sudo python pir.py
Motion Detected
Motion Detected
Discussion
Once triggered, the output of the PIR sensor will stay high for a little while. You can
adjust this using one of the trimpots on its circuit board.
See Also
You could combine this recipe with Recipe 7.15 to send an email when an intruder is
detected,
11.8, Detecting Movement | 285
www .it-ebaoks.info