Control Lights Based on Room Occupancy Using PIR and ESP32

May 27, 2025 · ESP32, PIR Sensor, Home Automation, Smart Lighting, DIY IoT

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:

💡 **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:


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


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 build more smart home automation? Explore these related tutorials:

Building something like this in production?

I help teams turn setups like this into reliable, monitored infrastructure.

Get a free consulting call

Get my monitoring stack checklist

The exact checklist I use when setting up observability for a new team. No spam, unsubscribe anytime.