DIY ESP32-Based Motion Sensor for Home Assistant Security Automations
DIY ESP32-Based Motion Sensor for Home Assistant Security Automations
Ever walked into a dark room and wished the lights would just know to turn on? Or wanted a simple way to alert you when someone’s lurking near your front door? That’s where a DIY ESP32-based PIR motion sensor comes in. I recently built one to integrate with Home Assistant, and it’s been a game-changer for my home security automations. Here’s how you can do it too—no PhD in electrical engineering required.
Why This Project?
Motion sensors are the unsung heroes of smart homes. They’re cheap, easy to set up, and incredibly versatile. Pair one with an ESP32 and Home Assistant, and you’ve got a powerful combo for automations like:
- Turning on lights when motion is detected.
- Triggering alarms or notifications.
- Logging activity for security monitoring.
I opted for the ESP32 because it’s affordable, Wi-Fi-enabled, and plays nicely with ESPHome, which makes integration with Home Assistant a breeze. Plus, it’s a fun weekend project that doesn’t break the bank.
What You’ll Need
Here’s the shopping list (with affiliate-free links where possible):
- ESP32 Development Board (like this one) – The brains of the operation.
- PIR Motion Sensor (HC-SR501) – Detects movement like a champ.
- Breadboard & Jumper Wires – For prototyping.
- Micro-USB Cable – To power the ESP32.
- 5V Power Supply (optional) – For permanent installations.
- ESPHome – Installed in Home Assistant.
Step 1: Wiring the PIR Sensor to the ESP32
The HC-SR501 PIR sensor has three pins:
- VCC (5V) – Connect to the ESP32’s 5V pin.
- GND – Connect to any ground pin.
- OUT – Connect to a GPIO pin (I used
GPIO13
).
Here’s the wiring diagram:
PIR Sensor ESP32
-------------------
VCC → 5V
GND → GND
OUT → GPIO13
Step 2: Flashing the ESP32 with ESPHome
If you haven’t already, install the ESPHome add-on in Home Assistant. Then, create a new device with this configuration:
esphome:
name: esp32-motion-sensor
platform: ESP32
board: nodemcu-32s
wifi:
ssid: "YOUR_WIFI_SSID"
password: "YOUR_WIFI_PASSWORD"
# Enable logging for debugging
logger:
# Enable Home Assistant API
api:
password: "YOUR_API_PASSWORD"
# OTA updates (so you can update wirelessly)
ota:
password: "YOUR_OTA_PASSWORD"
# Define the PIR sensor
binary_sensor:
- platform: gpio
pin: GPIO13
name: "Motion Sensor"
device_class: motion
Flash this to your ESP32 via USB. Once it’s online, it should appear in Home Assistant as a new device.
Step 3: Configuring Home Assistant Automations
Now for the fun part: making the motion sensor do something. Here’s a simple automation to turn on a light when motion is detected:
automation:
- alias: "Turn on lights when motion detected"
trigger:
platform: state
entity_id: binary_sensor.esp32_motion_sensor_motion_sensor
to: "on"
action:
service: light.turn_on
entity_id: light.your_light_entity
Want to get fancy? Add a delay to turn the light off after no motion:
action:
- service: light.turn_on
entity_id: light.your_light_entity
- delay: "00:05:00" # 5 minutes
- service: light.turn_off
entity_id: light.your_light_entity
Troubleshooting Common Issues
Sensor Not Detecting Motion:
- Check the PIR’s sensitivity and timeout dials (yes, it has tiny potentiometers!).
- Ensure the OUT pin is connected to the correct GPIO.
ESP32 Won’t Connect to Wi-Fi:
- Double-check your Wi-Fi credentials in the ESPHome config.
- Move the ESP32 closer to your router for testing.
False Triggers:
- PIR sensors can be triggered by pets or even heating vents. Adjust the sensitivity or use a pet-friendly sensor.
Going Further
- Battery Power: Use an 18650 battery and a low-power ESP32 variant for a wireless setup.
- Multiple Sensors: Chain several PIR sensors to cover larger areas.
- Notifications: Send alerts to your phone via Home Assistant’s app when motion is detected.
FAQ
Q: Can I use an ESP8266 instead of an ESP32?
A: Absolutely! Just adjust the platform
and board
in the ESPHome config.
Q: How do I make this work outdoors?
A: Use a weatherproof enclosure and a PIR sensor rated for outdoor use. Seal all connections with silicone.
Q: Can I integrate this with other smart home systems?
A: Yep! ESPHome supports MQTT, so you can tie it into systems like OpenHAB or Node-RED.
And that’s it! You’ve just built a DIY motion sensor that’s ready to supercharge your Home Assistant automations. For more projects like this, check out my guide on monitoring home temperature with ESP32. Happy tinkering! 🛠️