Connect and control Raspberry Pi motion detector PIR

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


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)

Raspberry Pi Bewegungsmelder PIR Anschluss

Software for controlling the Raspberry Pi motion detector

To execute the code, we will create a new file

sudo nano

with the following content:

Here, a function is set viaGPIO.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

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.

Raspberry Pi Bewegungsmelder PIR


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.

9 Responses

  1. 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?

  2. 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

    • If the non-living objects emit heat and move then I guess it might be possible but I’ve never tried it myself.

  3. 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.


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!