Motion Detector Using PIR Sensor Working and Arduino Interface With Code and Connection
by electronicsmith in Circuits > Arduino
36 Views, 0 Favorites, 0 Comments
Motion Detector Using PIR Sensor Working and Arduino Interface With Code and Connection


Hey! If you're attempting to make a basic motion sensor project with an Arduino and a PIR sensor, you're on the right site. Throughout this tutorial, I'll take you through how to assemble the circuit, sensor calibration, code, and test your prototype. If you're new to this or simply need a nice little project to tinker with, I hope this tutorial assists you in getting started. Let's go!
Supplies
- Arduino UNO R3
- PIR Motion Sensor (HC-SR501)
- LED
- Male-to-Female Jumper Wires
- Breadboard
- Resistor (220Ω)
- USB Cable
- Computer
- Arduino IDE
Installation of the Circuit
I have added a photograph and file of the Fritzing diagram for reference. I've taken this from an Arduino Project Hub tutorial in order to grasp the broad concept of programming a PIR sensor.
For this prototype, I left the LED on the UNO on Pin 13 and ground (Remember: Short leg in the ground; long leg in Pin 13).
To interconnect the PIR Sensor to the UNO, I have utilized the male-to-female jumpers. Power is where 5V is linked; Output is linked to Pin 2; Ground is linked to Ground (See PIR Sensor Diagram).
IMPORTANT NOTE: Make sure you have the power and ground wires properly connected to the sensor! I mixed up those two my first time through with the sensor. The second picture is a picture of the entire components of the PIR sensor.
Personalizing the PIR Sensor
The PIR sensor is adjustable by hand if you look at the back. I needed to do some minor tweaks to get the sensor more sensitive before I pushed too far on the code.
The first part is changing the Trigger Mode from low to high. It is a tiny jumper at the top right-hand side of the sensor with L and H labeled (refer to the PIR diagram). Reason: If it was set on Single Trigger Mode (Low), the LED would switch off within a few seconds even when the motion is continuous. On Repeatable Trigger Mode (High), the LED will remain on with continuous motion and switch off when there is no motion.
Next, adjust the Time Delay to the lowest setting. I want the sensor to react as fast as possible to when there is no motion. The lower the setting; the faster the light will be turned off. Keep in mind that Clockwise to raise; Counter-clockwise to decrease. And, the lowest setting will still have approximately a three-second delay.
Simple Test of the Sensor
After all that setup, I use the code from the Arduino Project Hub tutorial to test whether the sensor is in proper working condition.
When I run the code, I give the sensor 15-30 seconds to come up before I try motion.
The most important thing to be considered in this test is:
Does the sensor react when you wave a hand in front of it? (If not, check to see if the sensor is properly connected or the sensitivity is set too low)
Does the LED stay on if you keep waving your hand in front of the sensor? (If not, check if the jumper is high for Repeatable Trigger Mode)
Is the time delay for the LED a few seconds when no motion is detected? (If not, set Time Delay to the lowest setting)
Important Note: The LED light will stay on until the sensor is complete with calibrating, but the only time that the LED came on when the uploaded script was when I had the power and ground on the wrong pins. With my sensor, when connected correctly, the LED will stay off when uploading the script and was the only time that it would detect motion properly.
For code