A beginners guide to using PIR sensors in Arduino Projects for motion detection
Introduction
The PIR (passive infrared) sensor is a great component for
detecting motion and presence without any physical contact.
You can either use the miniature one, shown here, or that larger one with the dome that is an inch across. Either will work.
In this
beginner's guide, we will look at how to connect the PIR sensor to an
Arduino board and write simple code to detect
motion.
PIR sensors are often used in security systems, automatic
lighting, or any project where detecting movement is useful.
Over the
course of this tutorial, we will cover the basics of how a PIR sensor
works, how to connect one to an Arduino, example code to detect motion,
and testing our circuit.
By the end, you'll have a working motion
detector built with an Arduino and PIR sensor.
What is a PIR sensor?
It is basically an ultra sensitive heat detector and is
very useful for detecting the presence of a "hot" body e.g. an animal or
a human. This is why they are used extensively in security systems. The
one below is a smaller version of the SR501 and is ideal for use in
Arduino projects.
You only need to connect one input pin on your Arduino to the PIR
sensor, as the sensor only has one output. The other two connections are
power and ground
The PIR (passive infrared) sensor allows you to detect human
or animal motion by detecting changes in infrared radiation (heat).
Inside the sensor is a pyroelectric detector that can sense variations
in temperature.
All objects emit some level of infrared energy based on
their temperature. The sensor triggers when it detects a temperature
change in its range. This makes PIR sensors great for motion detection
without any physical contact.
How does a PIR sensor work
The key component inside an Arduino PIR sensor is a pyroelectric
infrared detector. This is typically made from a crystalline material
such as lithium tantalate that generates an electric charge in response
to temperature changes.
The pyroelectric detector is positioned with a pair of lenses inside the
sensor module. The lenses allow infrared radiation (heat) to enter and
focus onto two detection sections of the pyroelectric material.
As an object moves within the sensor's field of view, the temperature at
each detection area changes very slightly. This causes a small voltage
proportional to the temperature change to be generated in the
pyroelectric material.
This means that the sensor is insensitive to sunlight as
both areas would receive the same infrared heat from the sunlight.
Without the two areas the sensor would be triggered by just the amount
of infrared light falling on it. Using the two lenses means the
PIR sensor can detect changes (movement) rather than react to sunlight.
An integrated circuit inside the module then amplifies this
tiny pyroelectric voltage and compares the signal levels between the
two detection sections. Any difference detected indicates motion has
occurred in the observed area.
The input pin of the Arduino (to the PIR sensor output) is then activated by the integrated
circuit to change state, and signal that motion was detected. This output
can then be connected to a microcontroller like the Arduino for
processing.
What projects could you use it in?
Home security system
Automatic light switching
Presence detection
Appliance control
Energy savings
Photography/surveillance
Pet feeder
Intruder detection
Activity counter
Wildlife monitoring
Required Components
Arduino Uno
PIR motion sensor
1 LED
1k Ohm resistor
Breadboard
Jumper wires
PIR sensor Connections
The circuit diagram shows how to connect an Arduino to the PIR sensor's
power, ground, and output pins. The sensor's Vcc pin connects to the 5V pin on the Arduino. Its GND pin
connects to the ground pin. The output pin connects to digital pin 2 on
the Arduino. This allows the Arduino to detect when motion is sensed and
the output pin changes state. the LED connected to pin5 via the 1k shows when motion is detected.
Diagram using fritzing
Schematic Diagram
Diagram using fritzing
Libraries needed
You don't need a library as the output from the sensor is
digital (High means that the PIR sensor was triggered by movement, and low
means it was not).
Example Sketch
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
// PIR Sensor: motion detector
// Motion detected pin on the PIR sensor is digital pin PIRPin
// LED output on pin LEDpin
constint PIRpin = 13;
constint LEDpin = 5;
voidsetup(){
pinMode(PIRpin, INPUT);
pinMode(LEDpin, OUTPUT);
Serial.begin(9600);
}
voidloop(){
if(digitalRead(PIRpin) == HIGH){
Serial.println("Motion detected!");
digitalWrite(LEDpin, HIGH);
}
elsedigitalWrite(LEDpin, LOW);
}
Short Code Explanation
Set digital pin 13 as the input to read the PIR sensor
Set digital pin 5 as the LED output control
Read the value on digital pin 13 each loop
If the value is HIGH, motion was detected
Print "Motion detected!" to the serial monitor
Detailed Code Explanation
The code first sets pin 13 as an input since that is where
the PIR sensor's output pin is connected to pin 5 for the LED output. It
initializes serial
communication at 9600 baud. In the loop, it reads the digital value on
pin 13. If the value is HIGH, motion was detected by the sensor changing
the output pin state. It then prints "Motion detected!" to the serial
monitor so we can see when motion triggers the sensor. It also lights
the LED when motion is detected so you don't have to have the Serial
monitor on screen.
Uploading the Code
There are a few steps to uploading the code for the Arduino and PIR sensor using the Arduino IDE:
Connect the Arduino Uno to the PC with a USB cable.
Select the Arduino Uno hardware.
Open a new sketch.
Paste the code above into the new page (overwrite everything).
Press the upload button (right arrow at top).
You can find a more detailed tutorial on the Arduino IDE
page.
Testing the Circuit
To test the Arduino motion sensor circuit, upload the code then open the serial
monitor. Walk or wave your hand in front of the PIR sensor. You should
see "Motion detected!" print each time the sensor is triggered.
The re-rigger time is slow - meaning that the sensor keeps the
"motion detected" signal high for a few seconds - even when there is no
more motion. So this sensor does not restart quickly. It is however,
ultra sensitive and works over a very long distance.
Observations
Two things you should note about any PIR sensor are:
It works over a very long distance,
It does not reset quickly.
The PIR sensor has a long detection range, which makes it suitable for
applications where you need to detect movement from a distance, such as
security alarms. It can detect movement within a room without needing to
be right next to the area you want to monitor.
However, the long detection range comes at the cost of a slow reset
time. After detecting motion, the sensor output remains triggered for a
few seconds, even without continued movement. This makes it unsuitable
for applications needing to detect separate, rapid movements - like
counting people entering an area, where intervals between detections
must exceed the reset delay.
Because
of its slow reset, the PIR sensor is best applied to situations where
you want to trigger a longer-term action or state in response to
detecting any movement, rather than counting separate movements.
Common examples include triggering an alarm, starting a recording, or taking a photograph when motion is detected.
Conclusions
In this beginner's guide, you learned how to connect an Arduino to a PIR sensor (easy), we covered how a PIR sensor
works, and ran example code to detect motion,
and figured out how to test the circuit.
You can see that a PIR sensor is suited for longer term actions
after being triggered by movement e.g. an alarm, motion triggered
photograph etc.
You now have a working motion detection
project built with an Arduino and PIR sensor. This is a useful component
for detecting presence without contact in many automations and
sensor-based projects.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant
as a research assistant.
How to get accurate DHT22 digital humidity sensor readings with an Arduino. Did you know it also measures temperature as Well? Find out why, in this page...
A PIR sensor lets your Arduino sense movement without contact. This tutorial covers PIR sensor basics, connecting one to an Arduino board and coding a motion detector.
Arduino Hall Effect Sensor: Add magnetic sensing superpowers to your Arduino projects with an easy-to-use hall effect sensor. With full code and layout...
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.