How to Use the HC-SR501 PIR Motion Sensor with Arduino

How to Use the HC-SR501 PIR Motion Sensor with Arduino

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

  1. HC-SR501 PIR Motion Sensor Module
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Breadboard and Jumper Wires
  4. 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

  1. Sensitivity: Adjust the detection range (up to ~7 meters).
  2. 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

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code by clicking Upload.
  4. Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600.
  5. 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

  1. Motion-activated lighting
  2. Intruder alarms
  3. Smart home automation
  4. 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!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.