Loading...
 

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 ;)
And on software side: python with RPi.GPIO module
  • 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.

DSC_0647.JPG
DSC_0648.JPG
DSC_0649.JPG
DSC_0652.JPG
DSC_0653.JPG

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.
Contributors to this page: Avyd .
Page last modified on Sunday 04 of January, 2015 11:32:10 CET by Avyd.

Upcoming Events

1)  Tue 30 of Jul, 2019 10:00 CEST
CCTF - Frissítések II.
2)  Wed 01 of Jan, 2020 18:00 CET
Kotes workshop