Using PIR Sensors with ESP32 and Home Assistant

Introduction

Home Assistant is a popular open-source home automation platform. Combining it with ESP32 and PIR sensors lets you create powerful automations. This guide walks you through the setup.

Hardware Needed

  • ESP32 development board (e.g., NodeMCU-32S)
  • PIR sensor (AM312 or HC-SR501)
  • Jumper wires
  • Breadboard (optional)
  • USB power supply

Step 1: Wiring

Connect the PIR sensor to the ESP32:

  • VCC → 3.3V (for AM312) or 5V (for HC-SR501, if using 5V pin)
  • GND → GND
  • OUT → GPIO pin (e.g., GPIO4)

Note: If using HC-SR501 with ESP32, ensure the output voltage is 3.3V compatible (most are). Alternatively, use a voltage divider.

Step 2: Install ESPHome or Tasmota

Two popular firmware options:

ESPHome (Recommended)

ESPHome integrates seamlessly with Home Assistant. Follow the ESPHome installation guide to flash your ESP32.

Example ESPHome YAML configuration:

esphome:
  name: motion-sensor

esp32:
  board: nodemcu-32s

wifi:
  ssid: "YourSSID"
  password: "YourPassword"

mqtt:
  broker: 192.168.1.100  # Your Home Assistant IP

binary_sensor:
  - platform: gpio
    pin: GPIO4
    name: "Living Room Motion"
    device_class: motion
    filters:
      - delayed_on: 100ms  # Debounce

Upload the configuration to your ESP32.

Step 3: Integrate with Home Assistant

If using ESPHome, the device will auto-discover in Home Assistant. If using MQTT, ensure the MQTT integration is configured.

You’ll see a binary sensor “Living Room Motion” that turns on when motion is detected.

Step 4: Create Automations

In Home Assistant, go to Configuration → Automations. Example: Turn on lights when motion detected after sunset.

alias: "Living Room Lights on Motion"
trigger:
  platform: state
  entity_id: binary_sensor.living_room_motion
  to: "on"
condition:
  condition: sun
  after: sunset
action:
  service: light.turn_on
  entity_id: light.living_room_lights

Step 5: Add More Sensors

Repeat for multiple rooms. Consider using different GPIO pins or multiple ESP32s.

Power Considerations

If using battery power, choose a low-power PIR (AM312) and use deep sleep on ESP32. ESPHome supports deep sleep with wake on motion (requires wiring PIR output to RTC pin).

Troubleshooting

  • Sensor not detected: Check wiring, voltage, and MQTT connection.
  • False triggers: Add debounce filters in ESPHome.
  • Range issues: Adjust sensitivity or lens.

Conclusion

Integrating PIR sensors with ESP32 and Home Assistant is straightforward and opens endless automation possibilities. Start with one sensor and expand.

Leave a Reply

Your email address will not be published. Required fields are marked *