• First Steps
  • General
  • Projects
Facebook Twitter YouTube
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
  • Hardware & GPIO
  • Sensors & Components
  • Scale
  • Cases
  • Fingerprint
  • Gas Sensor
  • Heartbeat
  • German
  • French
Tutorials for Raspberry Pi Tutorials for Raspberry Pi
Home»First Steps»Connect and control Raspberry Pi motion detector PIR

Connect and control Raspberry Pi motion detector PIR

Facebook Twitter LinkedIn Tumblr Email Reddit
Raspberry Pi PIR Motion Sensor
Share
Facebook Twitter LinkedIn Email Tumblr Reddit Telegram WhatsApp

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

Setup

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 pir.py

with the following content:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import RPi.GPIO as GPIO
import time
 
SENSOR_PIN = 23
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
 
def my_callback(channel):
    # Here, alternatively, an application / command etc. can be started.
    print('There was a movement!')
 
try:
    GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=my_callback)
    while True:
        time.sleep(100)
except KeyboardInterrupt:
    print "Finish..."
GPIO.cleanup()

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

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.

alarm system Hausautomation Hausautomatisierung Houseautomation motion detector PIR
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleRemote Control a Raspberry Pi with 433Mhz Radio Transmitter
Next Article Build Raspberry Pi GPS location/navigation device

Related Posts

How to build a ESP8266 Scale (with Weight Sensor HX711)

How-To: Bluetooth Connection between ESP32’s and Raspberry Pi’s

How to create a Solar Powered Raspberry Pi Bitcoin Miner

How to build a Smart Agriculture System using IoT

11 Comments

  1. Rush on 14. September 2018 1:06

    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?

    Reply
    • Cat Piss Hater on 18. August 2020 21:39

      It would be better to just get rid of the cats.

      Reply
  2. D on 17. January 2019 15:04

    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

    Reply
    • Razberry on 6. July 2019 20:24

      What is that extra code?

      Reply
  3. Razberry on 6. July 2019 20:25

    What is that extra code?

    Reply
  4. Razberry on 6. July 2019 20:26

    If anyone could tell me that would be awesome!

    Reply
  5. Sam Muncy on 5. September 2019 5:46

    Can this PIRbe set up to detect motion of non-living objects?

    Reply
    • Marcus on 16. April 2021 20:47

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

      Reply
  6. jjack on 1. August 2021 18:24

    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.

    Reply
  7. Michael Ibinola on 5. August 2021 1:50

    How can i interface this with a PI-Camera?

    Reply
  8. Jacob Philpott on 7. August 2021 15:52

    Could you add a code example of how you adjust the motion sensitivity level, as is mentioned in the intro?

    Reply

Leave A Reply Cancel Reply

Transfer Raspberry Pi Raspbian OS to an SD Card (Windows)

Display Raspberry Pi Data on the Wio Terminal LCD Screen

ESP8266 Access Point: Build a Cheap Wi-Fi Repeater Yourself

7 Segment Display (Anode) with MAX7219 / MAX7221 and Raspberry Pi

Top 21 OS for the Raspberry Pi for your Project – Overview

How to Build Your Own Raspberry Pi Android TV Box

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

Thank you!

We will contact you soon.

Tutorials for Raspberry Pi
Facebook Twitter YouTube
  • Contact & Disclaimer

Type above and press Enter to search. Press Esc to cancel.