Due to its design, the PIR motion sensor module is very easy to use because it already has the components installed. Raspberry Pi motion detectors in home automation and/or outdoor applications (as a classic outdoor motion detector) are easier than ever to implement. I will show the commissioning in this tutorial.
This Arduino/Raspberry Pi motion sensor responds and moves, with the “strength” of movement controlled by an adjustable resistor (potentiometer). So you can set the motion sensor very sensitive, or try to avoid “noise”. As soon as something moves, a signal is sent that can be received and responded by the Raspberry Pi.
Required Hardware Parts
- PIR motion sensor
- Jumper cable (female-female)
- if necessary, LEDs or other components to activate
The setup is very simple since only one pin has to be activated during movement. The pins on the PIR are labelled:
- VCC to pin 2 (5V)
- OUT to pin 16 (GPIO 23)
- GND an pin 6 (ground)
Software for controlling the Raspberry Pi motion detector
To execute the code, we will create a new file
sudo nano pir.py
with the following content:
import RPi.GPIO as GPIO
SENSOR_PIN = 23
# Here, alternatively, an application / command etc. can be started.
print('There was a movement!')
GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=my_callback)
Here, a function is set via
GPIO.add_event_detect, which is called as soon as electricity flows. The exact functionality can be read here. Otherwise, you could also declare an infinite loop, where the status of the GPIO pin is queried with each call.
After saving and exiting (CTRL + O, CTRL + X) the file can be executed:
sudo python pir.py
If you want to stop the script, you can do so with CTRL + C.
I had to turn a little bit on the potentiometers and test which setting works best.
Many applications or commands are now available which are started or executed as soon as the Raspberry Pi motion detector detects an activity. Examples of this are alarm systems or, in conjunction with the official touch screen module, an automatic switching on of the display as soon as someone hets near to it.
One way to start the script via autostart, I have shown here.
I Am definitely interested in setting this up for my home; There’s an area I want to keep my cats out of (they keep marking the carpeting in this area), and I’d like to see about setting this up, and when motion is detected, I’d like it to play a sound file, but stop/reset ready to play again when motion stops. Is this possible with this script and PIR, or is that going to need some additional fine tuning?
It would be better to just get rid of the cats.
Yes that’s possible, you’d need a speaker though (easy enough to find one) and a little bit of extra code to make that run
What is that extra code?
What is that extra code?
If anyone could tell me that would be awesome!
Can this PIRbe set up to detect motion of non-living objects?
If the non-living objects emit heat and move then I guess it might be possible but I’ve never tried it myself.
i want this to work with another code from Adafruit. It has to do with these eyes on the screen. i want the eyes to move with who ever enters the room.
How can i interface this with a PI-Camera?
Could you add a code example of how you adjust the motion sensitivity level, as is mentioned in the intro?