ESP32 Motion sensor

Configurare noua (How To)

Situatie

The ESP32 is a powerful, low-cost microcontroller with built-in Wi-Fi and Bluetooth, making it an excellent platform for motion-sensing projects such as smart lighting, security systems, occupancy tracking, and IoT automation.

1. Motion Sensors Compatible with ESP32

1.1 PIR Motion Sensor (HC-SR501)

Best for: Human motion detection, low power consumption, simple digital output

  • Detects changes in infrared radiation from warm objects
  • Outputs HIGH when motion is detected
  • Detection range: 3–7 meters (typical)

Limitations: Cannot detect stationary objects and has slower response than radar sensors.

1.2 Microwave Radar Sensor (RCWL-0516)

Uses Doppler radar instead of infrared. It is more sensitive than PIR sensors and can detect motion through thin walls, but may cause more false triggers.

1.3 Accelerometer / IMU (MPU6050)

  • Detects motion, tilt, and vibration
  • Common in robotics and wearables
  • Uses I2C communication

2. PIR Motion Sensor Basics

2.1 PIR Sensor Pinout (HC-SR501)

Pin Name Description
1 VCC 5V (some support 3.3V)
2 OUT Digital output (HIGH on motion)
3 GND Ground

Note: Most HC-SR501 modules output 3.3V logic, which is safe for ESP32 GPIO pins.

2.2 Onboard Adjustments

  • Sensitivity potentiometer (detection range)
  • Time delay potentiometer (output HIGH duration)
  • Trigger mode jumper: H = repeat, L = single trigger

3. ESP32 GPIO and Power Considerations

Recommended GPIO pins: 13, 14, 25, 26, 27, 32, 33

Avoid boot-sensitive pins: 0, 2, 12, 15 unless you understand ESP32 boot modes.

ESP32 uses 3.3V logic. PIR sensors may require 5V power. Always connect grounds together.

4. Wiring the PIR Sensor to ESP32

  • VCC → VIN (5V) or 3.3V (if supported)
  • GND → GND
  • OUT → GPIO 27

5. Basic Arduino Code Example

#define PIR_PIN 27

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
}

void loop() {
  if (digitalRead(PIR_PIN)) {
    Serial.println("Motion detected!");
    delay(500);
  }
}

6. Interrupt-Based Motion Detection

#define PIR_PIN 27
volatile bool motionDetected = false;

void IRAM_ATTR motionISR() {
  motionDetected = true;
}

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(PIR_PIN), motionISR, RISING);
}

void loop() {
  if (motionDetected) {
    Serial.println("Motion detected via interrupt!");
    motionDetected = false;
  }
}

7. Power Saving with Deep Sleep

esp_sleep_enable_ext0_wakeup(GPIO_NUM_27, 1);
esp_deep_sleep_start();

This allows the ESP32 to sleep until motion is detected, ideal for battery-powered systems.

8. Common Problems and Solutions

  • False triggers: Reduce sensitivity and avoid heat sources
  • No detection: Allow sensor warm-up time (30–60 seconds)
  • Random resets: Ensure stable power supply

9. Applications

  • Smart lighting systems
  • Home security and alarms
  • Occupancy detection
  • Energy-saving automation.

Solutie

Tip solutie

Permanent

Voteaza

(4 din 6 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?