Motion Detection

Overview

In this project, we’ll build a PIR motion sensor system with an ESP32. When motion is detected, the LED will turn on. Since the ESP32 operates at a 3.3V logic level, we’ll be using the SR602 PIR sensor, which is compatible with low-voltage applications like this one.

SR602

The SR602 PIR sensor is a small, low-power motion detector module often used in hobbyist and DIY electronics projects. It is based on Passive Infrared (PIR) technology, meaning it detects motion by sensing changes in infrared radiation emitted by objects, such as humans or animals.

Key Features

  • Operating Voltage: 3.3V to 9V (commonly used at 3.3V or 5V).
  • Detection Range: Typically 3 to 7 meters, adjustable with a resistor.
  • Output: Digital signal (HIGH when motion is detected, LOW otherwise).
  • Detection Angle: Around 100°.
  • Quiescent Current: Very low, typically < 20 µA, making it suitable for battery-powered applications.
  • Time Delay: Fixed to approximately 2 seconds.
  • Compact Size: Ideal for space-constrained projects.

Pinout

  1. VCC: Power input (3.3v – 5v).
  2. OUT: Digital output (motion detected = HIGH, no motion = LOW).
  3. GND: Ground.

Wiring

Using the SR602 PIR sensor with an ESP32 is a simple and effective way to create a motion detection system. Below is a step-by-step guide for wiring, programming, and running your ESP32 with the SR602 PIR sensor.

1. Wiring the SR602 to ESP32

Connection Diagram:

SR602 PIR Sensor ESP32
VCC 3.3V
OUT GPIO 23
GND GND

Diagram

Arduino Code

The ESP32 will read the digital signal from the SR602 sensor’s OUT pin to determine whether motion is detected.

#define PIR_PIN 23 // Define the GPIO pin connected to SR602 OUT
#define LED_PIN 22  // Optional: Built-in LED on ESP32 (GPIO 2)

void setup() {
    Serial.begin(115200);
    pinMode(PIR_PIN, INPUT);  // Set PIR_PIN as input
    pinMode(LED_PIN, OUTPUT); // Set LED_PIN as output
}

void loop() {
    if (digitalRead(PIR_PIN) == HIGH) {
        Serial.println("Motion Detected!");
        digitalWrite(LED_PIN, HIGH); // Turn on the LED if motion is detected
    } else {
        digitalWrite(LED_PIN, LOW);  // Turn off the LED if no motion is detected
    }

  delay(10); // Add a small delay to stabilize readings
}
C++

Code Explained

digitalRead(PIR_PIN)
C++

The SR602’s OUT pin outputs HIGH (3.3V) when motion is detected and LOW (0V) when no motion is detected. The ESP32 reads this signal through the digitalRead() function.

if (digitalRead(PIR_PIN) == HIGH) {
        Serial.println("Motion Detected!");
        digitalWrite(LED_PIN, HIGH); // Turn on the LED if motion is detected
    }
C++

If motion is detected, the onboard LED (GPIO 22) turns on, and a message is printed to the Serial Monitor.

Testing the Setup

  1. Upload the code to the ESP32 using the Arduino IDE.
  2. Open the Serial Monitor (Ctrl + Shift + M) and set the baud rate to 115200.
  3. Move your hand or body within the sensor’s range (approximately 3-7 meters), and observe the serial output or LED.
  4. The SR602 has a fixed time delay of about 2 seconds; once motion is detected, the output remains HIGH for 2 seconds before resetting to LOW.

Tips

If you experience noise in the output, add a small ceramic capacitor (e.g., 0.1 µF) between VCC and GND near the sensor to stabilize power.

RELATED ARTICLES