How to Use the MH-SR602 PIR Motion Sensor with Arduino

How to Use the MH-SR602 PIR Motion Sensor with Arduino

The MH-SR602 is a compact PIR (Passive Infrared) motion sensor used for detecting movement within its range. Its small size, low power consumption, and adjustable delay make it ideal for motion-activated lighting, alarms, and IoT projects. In this tutorial, we’ll guide you through interfacing the MH-SR602 with Arduino.


What You Will Need

  1. MH-SR602 PIR Motion Sensor
  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 MH-SR602 Sensor

The MH-SR602 detects motion by sensing infrared radiation changes in its surroundings. When motion is detected, it outputs a HIGH signal; otherwise, it remains LOW.

MH-SR602 Pinout

Pin Function
VCC Power Supply (3.3V-5V)
OUT Digital Output Signal
GND Ground

Adjustable Features

  • Delay Time: The onboard potentiometer adjusts the time the output remains HIGH after motion is detected (approx. 2-100 seconds).

Step 2: Wiring the MH-SR602 to Arduino

Here’s how to connect the sensor to the Arduino:

MH-SR602 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 MH-SR602 OUT to pin 2

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  Serial.println("MH-SR602 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 to the Arduino 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 MH-SR602 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("MH-SR602 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 MH-SR602

  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 delay potentiometer is not set too high.
  • False triggers: Reduce environmental noise (e.g., moving heat sources) and shield the sensor from direct sunlight.
  • Unstable readings: Ensure stable power and avoid excessive vibrations.

Conclusion

You’ve successfully interfaced the MH-SR602 PIR motion sensor with Arduino. This versatile sensor is perfect for motion detection in various projects. Experiment with additional features like adjusting the delay time or integrating it with other components for more advanced applications!

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.