Motion detection with RPi, GPIO and PIR
My goal was very simple: detect motion in short range (some meters).
What is needed: RPi, breadboard (optional), jumper cables and a PIR sensor.
- The PIR costs only a few euros (I got 5 for 8 EUR). searx it ;)
- If you don't have RPi.GPIO, you can install it from pip
The connections are simple, the PIR I have used has three legs (gnd,vcc,out). Basically you connect PIR's gnd to RPi's ground, VCC to 5V and out to a GPIO leg. The pictures will tell everything.
After you have connected everything, you can use python to get notices. Example code:
#!/usr/bin/python
# Example code for PIR sensor
import RPi.GPIO as GPIO
import time
# Set GPIO
GPIO.setmode(GPIO.BCM)
pir_in = 7 # -> your pin number
GPIO.setup(pir_in, GPIO.IN)
# Start detecting motion
try:
while True:
if GPIO.input(pir_in):
print (time.strftime("%H:%M:%S")) + ": Motion detected!"
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()Finally you can adjust the sensitivity by turning the controls on the PIR (time/delay).
Note: a few PIR sernsors are available in H.A.C.K.