PIR Motion Sensor: How to Use PIRs with Arduino & Raspberry Pi

Table of Contents


In this tutorial, you will learn how to use a PIR motion sensor to detect movement. At the end of this tutorial you will learn:

  • How PIR motion sensors work
  • How to use PIR sensors with Arduino
  • How to use PIR sensors with Raspberry Pi

How PIR Motion Sensors Work

Passive Infra Red sensors can detect movement of objects that radiate IR light (like human bodies). Therefore, using these sensors to detect human movement or occupancy in security systems is very common.  Initial setup and calibration of these sensors takes about 10 to 60 seconds.

The HC-SR501’s infrared imaging sensor is an efficient, inexpensive and adjustable module for detecting motion in the environment. The small size and physical design of this module allow you to easily use it in your project.

The output of PIR motion detection sensor can be connected directly to one of the Arduino (or any microcontroller) digital pins. If any motion is detected by the sensor, this pin value will be set to “1”. The two potentiometers on the board allow you to adjust the sensitivity and delay time after detecting a movement.

PIR modules have a passive infrared sensor that detects the occupancy and movement from the infrared radiated from human body. You can use this module in security systems, smart lighting systems, automation, etc. There are different PIR modules available in the market, but all of them are basically the same. They all have at least a Vcc pin, GND pin, and digital output. In some of these modules, there is a ball like a lens on the sensor that improves the viewing angle.

Using a PIR Sensor with Arduino


You can connect PIR output to any digital pin.

There is a jumper behind this module. If you move the jumper to L position, the sensor will ‘toggle’ (change state) whenever motion is detected. This is unlikely to be of much use in a practical applications. This mode is called non-triggering or Single Triggering mode.

Moving the jumper to the H position will result in the more usual sensor logic. The sensor will turn on when motion is detected and turn off a while after the last motion is detected. This sensor will reset the timer (which would otherwise turn the output off) each time motion is detected; this would be applicable, for example, for room occupancy lighting control where you don’t want the lights to blink off while the unit resets. This is called Retriggering mode. (or repeatable trigger mode).

There are also two potentiometers behind this module. By changing the SENSITIVITY potentiometer, you can reduce or increase the sensitivity of the sensor (clockwise increase), and also by changing TIME potentiometer the output delay after movement detection will be  changed.


You must add the library and then upload the code. If it is the first time you run an Arduino board,  Just follow these steps:

  1. Go to www.arduino.cc/en/Main/Software and download the software of your OS. Install the IDE software as instructed.
  2. Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
  3. Choose the board in tools and boards, then select your Arduino Board.
  4. Connect the Arduino to your PC and set the COM port in tools and port.
  5. Press the Upload (Arrow sign) button.
  6. You are all set!


modified on 5 Feb 2019
by Saeed Hosseini @ ElectroPeak

int ledPin = 13;                // LED 
int pirPin = 2;                 // PIR Out pin 
int pirStat = 0;                   // PIR status
void setup() {
  pinMode(ledPin, OUTPUT);     
  pinMode(pirPin, INPUT);     
void loop(){
  pirStat = digitalRead(pirPin); 
  if (pirStat == HIGH) {            // if motion detected
    digitalWrite(ledPin, HIGH);  // turn LED ON
    Serial.println("Hey I got you!!!");

  else {
    digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion

For proper calibration, there should not be any movement in front of the PIR sensor for up to 15 seconds (until pin 13 is turned off). After this period, the sensor has a snapshot of its viewing area and it can detect movements. When the PIR sensor detects a movement, the output will be HIGH, otherwise, it will be LOW.

Using a PIR Sensor with Raspberry Pi



import sys
from Adafruit_CharLCD import Adafruit_CharLCD

lcd=Adafruit_CharLCD()     # instantiate LCD Display

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering

# Set up input pin
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# Set up LED output
GPIO.setup(20, GPIO.OUT)

# Callback function to run when motion detected
def motionSensor(channel):
    GPIO.output(20, GPIO.LOW)
    if GPIO.input(21):     # True = Rising
        global counter
        counter += 1
        lcd.message('Motion Detected\n{0}'.format(counter))
        GPIO.output(20, GPIO.HIGH)

# add event listener on pin 21
GPIO.add_event_detect(21, GPIO.BOTH, callback=motionSensor, bouncetime=300) 
counter = 0

    while True:
        sleep(1)         # wait 1 second

finally:                   # run on exit
    GPIO.cleanup()         # clean up
    print "All cleaned up."

Related Projects

Buy A PIR Motion Sensor

Liked What you see?

Get updates and learn from the best

More To Explore

Comment (1)

Leave a Reply

Your email address will not be published. Required fields are marked *