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)
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:
Connect the PIR sensor to the ESP32:
- PIR
VCC
→ ESP323.3V
(⚠️ Don’t use 5V—it can fry some PIR sensors!) - PIR
GND
→ ESP32GND
- PIR
OUT
→ ESP32GPIO13
(or any free pin)
- PIR
Wire the relay for light control:
- Relay
VCC
→ ESP325V
- Relay
GND
→ ESP32GND
- Relay
IN
→ ESP32GPIO12
- Relay

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
- Flash the ESP32:
esphome run room_light_automation.yaml
- 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
- Add a manual override: Wire a physical switch in parallel with the PIR.
- Integrate with Home Assistant: Use MQTT to log occupancy data.
- 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!