Raspberry Pi Motion Sensor
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 that triggers a signal pin when exposed to infrared (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.
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 comes 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 labeled 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 you’re not too 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 to work fine.
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.
This code will set up 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 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.
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 is 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.