Raspberry Pi Motion Sensor
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
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.
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.
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.
Excellent article, I taught Electronics at TTI for a few years in the 90s and this article is easy to follow and very informative.
Anyone new to electronics and breadboarding can follow your step by step explanation and have this board functioning in now time at all.
I taught Micro Processor assembly and programming using binary and hex. This code is much easier to understand and more like basic so a student or person could program this very quickly and make adjustments with little effort.
Could this sensor be used on a field camera to trigger a camera to capture pics or video clips?
Thank you for the article and it is an excellent read.
Hi Robert
You read my mind, my next article is going to be about using the motion sensor to trigger the camera and then emailing the picture or video.
My first read this was bit over my head, but so interesting.
How is the Python code implemented? (Edit- oops, nevermind, I found your article on Raspian in software tuts)
Also, I wonder, are housings available for the Raspberry Pi and motion sensor? That’d be nice if I was going to use the detector day to day.
This is a cool, well-written site. I’ll be back Thanks!
Hi Geoff
Glad that you found the information useful, as for cases there are several out there, here’s a selection of my favorites
Hope this helps