In this project you’re going to create a simple circuit with an Arduino and PIR motion sensor that can detect movement. An LED will light up when movement is detected.
Watch the video below to see how it works
Introducing the PIR Motion Sensor
The PIR motion sensor is ideal to detect movement. PIR stand for “Passive Infrared”. Basically, the PIR motion sensor measures infrared light from objects in its field of view.
So, it can detect motion based on changes in infrared light in the environment. It is ideal to detect if a human has moved in or out of the sensor range.
The sensor in the figure above has two built-in potentiometers to adjust the delay time (the potentiometer at the left) and the sensitivity (the potentiometer at the right).
Pinout
Wiring the PIR motion sensor to an Arduino is pretty straightforward – the sensor has only 3 pins.
- GND – connect to ground
- OUT – connect to an Arduino digital pin
- 5V – connect to 5V
Parts required
Here’s the required parts for this project
- 1x PIR Motion Sensor (HC-SR501)
- Arduino UNO – read Best Arduino Starter Kits
- 1x LED
- Jumper Cables
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Schematics
Assemble all the parts by following the schematics below.
Code
Upload the following code.
/*
Arduino with PIR motion sensor
For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
Modified by Rui Santos based on PIR sensor by Limor Fried
*/
int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
Wrapping Up
This post shows a simple example on how to use the PIR motion sensor with the Arduino. Now, you can use the PIR motion sensor in more advanced projects. For example, you can build a Night Security Light project.
If you’re an absolute beginner, and you’re just getting started, we recommend taking a look at our Free Arduino Mini Course.
Thanks for reading. If you like this post probably you might like our next ones, so please support us by subscribing our blog.
Hi,
Did You plain add pir-sensor with SMS-gateway (ex. Huawei GSM modem) or Google Docs ?
Hi!
Not exactly, I do plan to make a more advanced tutorial in a few weeks.
Integrate the PIR motion sensor with my website: http://homeautomationserver.com/
So it notifies on your account and changes the state when motion is detected.
I’m currently working on the homeautomationserver API, so we can send input data, such as temperature or sensor values.
But it’s taking longer than I thought and I won’t have much time left in the upcoming weeks.
You should connect the data PIR connector to the GND with a resistor on your schematic to delete the no-wanted parasits signal
Thank you Cynder, you’re right it works better with that resistor, I forgot to add it to my circuit!
you can programming by so easy way
I’m glad I could help!
I love your work sir!
I am trying to do this using sensor to control movement of antenna. can you help me.
You’re welcome.
I don’t have any tutorials on that exact subject…
Good luck with your project,
Rui
I love ur work!
You need targeted visitors for your Arduino with PIR Motion Sensor | Random Nerd Tutorials website so why not try some for free? There is a VERY POWERFUL and POPULAR company out there who now lets you try their website traffic service for 7 days free of charge. I am so glad they opened their traffic system back up to the public! Check it out here: http://kururu.info/yourls/2u
Not warking :/
just on the LED thats it What should I do?
I don’t know much details about your problem. Is you arduino in good condition? The arduino code has uploaded properly? have you followed my exact schematics?
thanx.. its work
You’re welcome! Thanks for trying my projects,
-Rui
hi
can i reduce the distance of sensor from 6 meter to 1 m
Yes, you can
how do you do it?
i would like to know 🙂
Dear
Sir I very like your project and learnd and can you tell about how to make the gsm security alarm with this project and code. Thank you very much
You can use this program with a GSM shield.
Sir, how to upload arduino code from computer to arduino board? Is there any setup or application to do that?
You need to go the official arduino website and download the free official software called “Arduino IDE”.
Here’s the download page: arduino.cc/en/Main/Software
Dear
Sir I very like your project and can you tell about how to make with project gsm security alarm and code
Sir,
I like you PIR motion sensor with arduino project. Can you tell me how can I use this model in place of 230v switch to make it automated? Can you provide me with diagram of the circuit and the materials required in this project?
Thank you
sorry about my earlier comment it works perfectly thanks a lot my mistake
sorry for the inconvenience
anyway thanks
Rui Santos
i like your projects
Thank you, I’m glad you found the solution!
Rui
nice work . can u get me circuit in protues 8?? i hope u can help me thnks you sir.
No, I don’t have that circuit…
Thank you
You’re welcome!
the PIR sensor is very sensitive. can you somehow tell me how to reduce the turning on and off of the sensor on its own…. please reply cuz i need to submit it quickly.
There is a potenciometer on the back of the PIR motion sensor that you can use to adjust the sensitivity of your sensor…
Hi
Can you show us how we can linked this 5v to 220V with a realy Please!
Thanks
I don’t have any tutorials on that subject…
Rui
my idea is to detect the human alive inside the rubble,so can i use PIR
Yes, it can be used to do something like that 🙂
With the rcwl-0516 sensor, it is more possible to find a creature that can move partially in the debris.
thank you very much
You’re welcome!
THE LED BLINKING IS NOT STOPPING
Try to adjust the PIR motion sensor sensitivity by rotating the potentiometer
/*
I NEED FURTHER EXPLANATION BECAUSE IM JUST A BEGINNER
*/
int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW) { //————————-IN THIS PART HOW TO MAKE IT STOP?
Serial.println(“Motion detected!”);
state = HIGH; // update variable state to HIGH //——- THERE IS NO TIME INTERVAL PUTTED HERE
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println(“Motion stopped!”);
state = LOW; // update variable state to LOW
}
}
}
I dont really understand the part which motion is detected… how you make if stop if it detects a motion?
can you please explain me further?
thank you ! and godbless
You could use interrupts. Search for PIR Motion Sensor Arduino Interrupt example
hi Rui Santos i will PM you .. if i recieve my ardiuno .. thanks a lot.. 🙂 .. more power and godbless to you sir.
You’re welcome.
Rui
Can i use a AC 220V bulb instead of this led? How can i do this?
Yes. You just need to replace the LED with a lamp connected to a relay.
Take a look this project were we light up a lamp when movement is detected: https://randomnerdtutorials.com/build-a-night-security-light-with-arduino/
I hope this helps.
Thanks.
Great! Can I use this PIR?
Yes, that PIR should work!
Enjoy the content of your site.
Trying with an wemos hooked up the pir to pin 4 D2 and the led to pin 13 D7 the led blinking like crazy , tried in dark, tried the resistor between data pin of pir and GND , tried with a second brand new pir adjusted those both potentiometers on the pir all directions .. btw(why there 2 whats the role of the second? if one is sensitivity?) nothing still blinking like crazy any idea?
thanks
Teodor
Hi Teodor.
The best way to use the PIR with the wemos is setting it as an interrupt and set a callback function to light up the LED for a predetermined number of seconds whenever the interrupt is triggered.
With the code in this example, you may get a lot of interference and get false positives, which results in the LED blinking.
Regards,
Sara 🙂
Hey, could you explaIN how to activate a LED strip with this?
Hi Mark.
Inside the following if statement, add the code to control the strip.
if (state == HIGH){
Serial.println(“Motion stopped!”);
state = LOW; // update variable state to LOW
//your code to control the strip
What type of strip do you want to activate? Addressable or just an RGB LED strip?
We have a tutorial on how to control an addressable RGB LED strip with Arduino: https://randomnerdtutorials.com/guide-for-ws2812b-addressable-rgb-led-strip-with-arduino/
If want to control a simple LED strip, we only have a tutorial with the ESP8266. You can follow this tutorial to figure out how to wire the strip to the Arduino (it shows how to do with an ESP32/ESP8266, but the logic is the same: https://randomnerdtutorials.com/esp32-esp8266-rgb-led-strip-web-server/
The code to control the strip is the same as controlling an RGB LED. You can read our tutorial about the RGB LED with Arduino here: https://randomnerdtutorials.com/electronics-basics-how-do-rgb-leds-work/
I hope this helps.
Regards,
Sara
Hi,can you make it work without the wire connecting to the arduino,and use batteries instead?
hi
good project
is someone able to help me on connecting an extra LDR (Light Detection) on the PIR Board ?
I try to but it is not working
thanks
Hello
I am using the code provided and followed the schematic but the LED stays lit all the time, no matter the position of the potentiometers. What could the problem be. It is all new parts from a beginner kit as I am a beginner. Not sure how to trouble shoot this. Any advice?
Thanks
jack
Hi.
If you have a multimeter, check the output from the PIR motion sensor. Check if it changes to LOW and HIGH on the right times. Also, double-check if everything is connected properly.
Regards,
Sara
Hello, I need an application with a very low sensitivity. I need a sensor that does not detect movement at a distance larger than 10 centimetres. Is it possible to do that using potentiometer or should I look for different type of sensor?
Thank you.
The PIR shown is used by many projects. I got a bunch of these to use for my ESPHome projects around the house, and all of them fail miserably, with false positives. Every few seconds with some of the sensors, to every few minutes for the better ones. If you search for this sensor, people complain about how useless they are, and the frustrations and waste of money. No amount of pull-up or pull-down resistors, added capacitors are going to fix it. I found a place they suggested to use the Panasonic EKMB or EKMC series PIR’s. You can buy these from DigiKey or Mouser. They are a little more. I got some of them, and they work perfectly. The one I got said the data pin would be VIN minus half a volt. So for my ESP32, I powered it with 3.3 volts so I wouldn’t overpower the data line to my ESP32.
Hi.
Thanks for the suggestion.
I’ll take a look at those models.
Regards,
Sara