Raspberry Pi Motion Sensor

by | Feb 17, 2022 | 4 comments

Motion Sensors

 

Motion detection can be done quite easily by using the GPIO ports of the Raspberry Pi and a motion sensor. Motion detectors use a Pyroelectric sensor which triggers a signal pin when exposed to infra red (IR) radiation, when a person or an animal pass in front of the sensor the IR energy is detected and an output pin is activated. Motion detectors usually have what is called a “Fresnel lens” on the front which greatly expands the detection area of the sensor. In this article I will show you how to connect a PIR (Passive Infra-Red) sensor to the Pi and we will also use one of the GPIO’s as an output to connect up a simple LED so that you can check to see that the whole setup is working.

 

Equipment

Required

Optional

 

Note: The Adafruit motion sensor provides additional options if required on the circuit board to adjust the sensitivity, delay and trigger settings.

 

Connecting the Motion Sensor and LED

 

In this tutorial I will be using the Keyestudio motion sensor because it is a little easier to mount on a breadboard for prototyping and testing but it really come down to your own personal preference and mounting requirements as to which one to use. I will be following the Broadcom (BCM) pin numbering convention when making connections to the Pi.

Raspberry Pi GPIO pin number layout

There are 3 pins on the motion sensor, Ground, Power and Signal, these are generally labelled on the sensor so make sure to double check your connections.

  • Connect the Power (Vcc) pin of the motion sensor to a 3.3v on the Pi
  • Connect the Ground pin of the motion sensor to a Ground pin on the Pi
  • Connect the Signal pin of the motion sensor to GPIO pin 17 on the Pi

 

 

That’s it, 3 simple connections and you’re motion sensor is ready

In order to check that the sensor is working we’ll add an LED so that we can see when the sensor is activated, to do this you will need an LED and a 330Ω resistor.

  • Connect the 330Ω resistor to GPIO port 27 on the Pi
  • Connect the long leg (+ve) of the LED to the other leg of the resistor
  • Connect the short leg (-ve) of the LED to a Ground pin on the Pi

 

 

If your not to sure on how LED’s should be connected then read my tutorial on connecting an LED and a Button, while that article uses a slightly different method to control the LED both techniques work fine.

 

The Code

 

Now that we have all of our connections made we need to add some python code to read the sensor and then activate the LED.

 

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)  # Configures pin numbering to Broadcom reference
GPIO.setup(27, GPIO.OUT)  # Set our GPIO pin to output
GPIO.output(27, False)  # Set output to off
# Set GPIO pin to input and activate pull_down resistor
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print ("Motion Sensor Test")
sleep(2)
print ("Ready")
try:
    while True:
        if GPIO.input(17):
            GPIO.output(27, True)  # Turn LED on
            print ("Sensor Triggered")
            sleep(5)
        else:
            GPIO.output(27, False)  # Turn LED off
except KeyboardInterrupt:  # catch that ctrl+C has been pressed
    print ("Shutting Down")
except:
    print("An Error or Exception has occured")
finally:  # set used GPIO pins to inputs
    print ("Resetting GPIO pins")
    GPIO.cleanup()

 

This code will setup GPIO pin 17 as an input and pin 27 as an output, it then waits for the motion sensor to be triggered and turns on the LED, once the motion has ceased the LED will turn off. Pressing ctrl+ C will stop the program and perform a GPIO cleanup which will set all the ports we have used back to inputs. This line of code helps to prevent damaging your Pi from a situation where you have a port set HIGH as an output and you accidentally connect it to GND (LOW), which would short-circuit the port and possibly break it. Inputs can handle either 0V (LOW) or 3.3V (HIGH), so it’s safer to leave ports as inputs.

 

Note: The GPIO.cleanup() command only affects the GPIO pins that you have used in your code, so in this case it will only reset pins 17 and 27 to inputs, it does not make changes to any of the other pins.

 

Summary

 

Motion sensors like other sensors are great for IOT projects and using your Pi to interact with the physical world, by adding other equipment such as a relay instead of a LED you can use it to turn on a simple security light. Other options might be to add speakers to play a song when you walk in the room or if you integrate it with the Raspberry Pi camera you can use it for home security.  There are a huge range of projects out there that benefit from the addition of a motion sensor and given that they are so easy to connect, they are great for beginners.

 

If you have any thought’s about this article, improvements or errors let me know in the comments below and if you found this helpful, why not share it with others.

 

Pin It on Pinterest

Share This