Building a Laser Trip Sensor with a Photoelectric Sensor and ESPHome

Building a Laser Trip Sensor with a Photoelectric Sensor and ESPHome

Home Automation, ESPHome

Presence detection is a cornerstone of home automation. Whether you want lights that automatically switch on when someone walks into a room, or smarter security alerts, presence sensors give your automations real-world context.

While PIR (motion) sensors are common, another interesting option is the photoelectric sensor. These sensors detect when an object breaks a light beam, making them great for detecting someone passing through a doorway, entering a hallway, or triggering an event when something moves past.

In this post, I’ll walk through building a simple ESPHome-based presence sensor using a photoelectric sensor, an ESP microcontroller, and Home Assistant integration.


Why Photoelectric Sensors?

Most people default to PIR sensors, but photoelectric sensors offer some nice benefits:

  • Directional detection – can be placed across a doorway or passage.
  • More reliable indoors – less prone to false triggers from heat changes.
  • Fast response – detects instantly when the beam is broken.
  • Versatile – can detect objects, not just people.

This makes them especially useful in hallways, staircases, garages, or even for counting entries/exits.


Hardware Needed

  • ESP8266 or ESP32 board (NodeMCU, ESP32-DevKit, etc.).
  • Photoelectric sensor (common 5V or 12V models, like E18-D80NK).
  • Relay or MOSFET (optional) if the sensor requires different power than the ESP board.
  • Wiring & enclosure depending on where it’s installed.

Most photoelectric sensors provide a digital output (HIGH/LOW) when the beam is broken, which makes them easy to integrate with ESPHome.


Wiring Overview

  • VCC of the sensor → 5V (or 12V depending on model).
  • GND → Common ground with ESP board.
  • OUT → Connect to an ESP GPIO pin (with level shifting if needed).

ESPHome Configuration

Here’s a simple YAML config to turn the sensor into a binary sensor in Home Assistant:

esphome:
  name: photo-sensor
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "YOUR_WIFI"
  password: "YOUR_PASSWORD"

api:
logger:
ota:

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Presence Sensor"
    device_class: motion
    filters:
      - delayed_off: 500ms

✅ This setup creates a binary sensor entity in Home Assistant called Presence Sensor.
delayed_off prevents flickering if the beam is quickly interrupted multiple times.


Example Automations

Once in Home Assistant, you can start building automations like:

  • Hallway lights: Turn on when someone walks through.
  • Garage alert: Send a notification when the beam is broken after hours.
  • Entry counting: Use two sensors side-by-side to count in/out movement.

Example automation for lights:

alias: Hallway Lights On
trigger:
  - platform: state
    entity_id: binary_sensor.presence_sensor
    to: "on"
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway

Benefits of a Photoelectric Presence Sensor

  • Accurate – triggers only when the beam is broken.
  • Flexible – works for both people and objects.
  • DIY-friendly – simple binary sensor integration with ESPHome.
  • Expandable – add multiple sensors to track direction or build counters.

Conclusion

A photoelectric sensor paired with ESPHome gives you a fast, reliable way to detect presence and movement in your smart home. It’s a great alternative to PIR sensors, especially in narrow or controlled spaces like doorways, staircases, or garages.

Once integrated with Home Assistant, the possibilities are endless — from simple light automation to more advanced occupancy tracking.