/ #ESP32 #PIR Sensor 

Control Lights Based on Room Occupancy Using PIR and ESP32

Control Lights Based on Room Occupancy Using PIR and ESP32

Ever walked into a room, arms full of groceries, only to fumble for the light switch? Or worse—left the lights on all day in an empty room? With a PIR motion sensor and an ESP32, you can automate your lights to turn on when someone enters and off when the room is vacant. It’s a simple yet powerful way to save energy and add convenience to your smart home.

In this tutorial, I’ll walk you through setting up this system step-by-step, including wiring, ESPHome configuration, and troubleshooting tips. I’ve also sprinkled in a few hard-earned lessons from my own trial-and-error moments (like why you shouldn’t place the PIR sensor near a heater).


Materials You’ll Need

Here’s what I used for this project:

  • ESP32 development board (like the ESP32-WROOM-32)
  • PIR motion sensor (HC-SR501 or AM312) – Amazon link
  • Relay module (to control the lights) – 5V relay
  • Jumper wires (male-to-female recommended)
  • USB cable (for power/flashing)
  • Home Assistant (optional, for remote monitoring)
💡 **Pro Tip**: If you’re using high-voltage lights, ensure your relay supports the load (e.g., a 10A relay for incandescent bulbs). Always double-check safety ratings!

Step 1: Wiring the PIR Sensor and ESP32

Let’s start with the hardware setup. The PIR sensor detects motion by measuring infrared radiation changes (like body heat). Here’s how to wire it:

  1. Connect the PIR sensor to the ESP32:

    • PIR VCC → ESP32 3.3V (⚠️ Don’t use 5V—it can fry some PIR sensors!)
    • PIR GND → ESP32 GND
    • PIR OUT → ESP32 GPIO13 (or any free pin)
  2. Wire the relay for light control:

    • Relay VCC → ESP32 5V
    • Relay GND → ESP32 GND
    • Relay IN → ESP32 GPIO12
PIR and ESP32 wiring diagram

Step 2: ESPHome Configuration

I prefer ESPHome for its seamless Home Assistant integration. Here’s the YAML configuration for the ESP32:

esphome:
  name: room_light_automation
  platform: ESP32
  board: nodemcu-32s

binary_sensor:
  - platform: gpio
    pin: GPIO13
    name: "Room Occupancy"
    device_class: motion
    on_press:
      - switch.turn_on: light_relay
    on_release:
      - delay: 2min  # Adjust delay before turning off
      - switch.turn_off: light_relay

switch:
  - platform: gpio
    pin: GPIO12
    name: "Light Relay"
    id: light_relay

Key Notes:

  • Adjust delay to match how long lights stay on after motion stops.
  • Test the PIR’s sensitivity dial (on the sensor itself) to avoid false triggers.

Step 3: Flashing and Testing

  1. Flash the ESP32:
    esphome run room_light_automation.yaml
    
  2. Debugging Tips:
    • If lights flicker, check the PIR’s “retrigger” jumper (set to “H” for continuous signals).
    • Use ESPHome logs (esphome logs) to verify motion events.

Troubleshooting Common Issues

  • PIR sensor not detecting motion:

    • Ensure it’s not facing a window (sunlight interference).
    • Adjust the sensitivity potentiometer clockwise.
  • Relay not switching:

    • Confirm the relay’s input logic (some are active-low).

Enhancements

  1. Add a manual override: Wire a physical switch in parallel with the PIR.
  2. Integrate with Home Assistant: Use MQTT to log occupancy data.
  3. Combine with lux sensors: Only trigger lights if the room is dark.

FAQ

Q: Can I use an ESP8266 instead?
A: Yes! Just replace the platform: ESP32 line in the YAML.

Q: Why does my PIR sensor trigger randomly?
A: Avoid placing it near HVAC vents or heaters—temperature fluctuations cause false positives.

Q: How do I control multiple lights?
A: Use a multi-channel relay or a smart bulb (like this Zigbee setup).


Ready to automate more? Check out my guide on monitoring home temperature with ESP32. Happy tinkering!