The HC-SR501 is a popular Passive Infrared (PIR) motion sensor module that detects movement by measuring infrared radiation changes in its surroundings. It’s commonly used in motion-activated lighting, security systems, and IoT projects. This tutorial will guide you through setting up and using the HC-SR501 with Arduino.
What You Will Need
- HC-SR501 PIR Motion Sensor Module
- Arduino Board (e.g., Uno, Mega, Nano)
- Breadboard and Jumper Wires
- A computer with the Arduino IDE installed
Step 1: Understanding the HC-SR501 Sensor
The HC-SR501 module has adjustable sensitivity and delay time settings, making it versatile for various motion detection applications. It operates on 5V power and outputs a HIGH signal when motion is detected.
HC-SR501 Pinout
Pin | Function |
---|---|
VCC | Power Supply (5V) |
OUT | Digital Output Signal |
GND | Ground |
Adjustable Settings
- Sensitivity: Adjust the detection range (up to ~7 meters).
- Delay Time: Adjust the duration of the HIGH signal after motion is detected (approx. 5 seconds to 5 minutes).
Step 2: Wiring the HC-SR501 to Arduino
Here’s how to connect the HC-SR501 to an Arduino Uno:
HC-SR501 Pin | Arduino Pin |
---|---|
VCC | 5V |
OUT | Pin 2 |
GND | GND |
Step 3: Upload the Code
Here is an example sketch to monitor motion detection and display the status on the Serial Monitor:
const int pirPin = 2; // Connect HC-SR501 OUT to pin 2
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600);
Serial.println("HC-SR501 PIR Motion Sensor Test");
}
void loop() {
int motionStatus = digitalRead(pirPin); // Read the sensor output
if (motionStatus == HIGH) {
Serial.println("Motion detected!");
} else {
Serial.println("No motion detected.");
}
delay(500); // Update every 500ms
}
Step 4: Test the Setup
- Connect the Arduino to your computer via USB.
- Open the Arduino IDE and select the correct Board and Port under the Tools menu.
- Upload the code by clicking Upload.
- Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to
9600
. - Move within the sensor’s range, and observe the motion status printed on the Serial Monitor.
Optional: Controlling an LED with Motion
You can modify the code to turn on an LED when motion is detected:
Wiring the LED
LED Pin | Arduino Pin |
---|---|
Anode (+) | Pin 13 |
Cathode (-) | GND |
Modified Code
const int pirPin = 2; // Connect HC-SR501 OUT to pin 2
const int ledPin = 13; // Connect LED to pin 13
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("HC-SR501 PIR Motion Sensor Test");
}
void loop() {
int motionStatus = digitalRead(pirPin); // Read the sensor output
if (motionStatus == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
Serial.println("Motion detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED
Serial.println("No motion detected.");
}
delay(500); // Update every 500ms
}
Applications of the HC-SR501
- Motion-activated lighting
- Intruder alarms
- Smart home automation
- Proximity-triggered IoT devices
Troubleshooting
- No motion detected: Ensure the sensor is powered correctly and the sensitivity is adjusted appropriately.
- False triggers: Reduce environmental noise (e.g., heat sources) and avoid direct sunlight exposure.
- Unstable readings: Check for proper grounding and stable power supply.
Conclusion
You’ve successfully interfaced the HC-SR501 PIR motion sensor with Arduino. This versatile sensor is perfect for motion detection in various projects. Experiment with additional features, such as adjusting sensitivity or integrating it with other components, to create advanced motion-based systems!