ESPHome IR beam sensor - [SOLVED]

Hi All,

I wish to use an IR beam sensor to detect when someone has walked down my driveway.

I’ve read several posts that state, “it should be an easy ESP project” but then don’t share any code.

After trying several iterations of the following code, I still cannot get it to work, the sensor just reports “detected” permanently, no matter how many times I wave my hands in front of it.

Has anyone successfully created this project and wouldn’t mind helping out?

Thanks

esphome:
  name: esp-beam-sensor

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "removed for posting"

ota:
  password: "removed for posting"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp-Beam-Sensor Fallback Hotspot"
    password: "removed for posting"

captive_portal:

binary_sensor:
  - platform: gpio
    pin:
      number: D4
      mode:
        input: true
        pullup: true
    name: "ESP Beam Sensor"
    device_class: motion

Looks like the sensor has a relay and at least 2 contacts associated with it exposed instead of the typical 3. Can you confirm that triggering the beam will cause COM and OUT to be shorted and that there is no voltage on either terminal? If that is the case, connecting your GPIO to Out and GND (-) to COM should cause the GPIO to be pulled down to zero when the relay is closed assuming the relay contact exposed is NO (normally open). Sometimes the contact used is NC so you can detect a wire fault or someone cutting the wires… in that case the GPIO would be pulled down at all times and go high when the relay is triggered.

If you need to invert the logic, I believe you can use inverted: true but that is a problem for later once you see beam changes reflected in HA.

Just in case… you have the GPIO pulled up which means it is kept high by an internal resistor until you pull it low by connecting the pin to ground. I’d say that is the best way to go about this but wanted to be sure you did it intentionally.

1 Like

Thanks for the reply.

I checked the COM & OUT connections, there is no voltage on either terminal.

The device was originally configured as NC (normally closed), but I have now changed that to NO (normally open), this now means, COM & OUT are shorted when the beam is interrrupted.

The GPIO pullup line is in the latest version of the code, I previously tried it without this line, the exact same effect.

I have a theory, which I need to test. The 12v powering the sensors, is not the same PSU that is powering the Wemos D1, my theory is that the short between GND and D4 isn’t happening as the GND’s aren’t the same.

I’ll post back after i’ve tested.

Hello I have just installed a PIR for my gate to detect line crossing. It is powered 24v (says 5 to 24vdc) and with both NC and NO DRY relay. I think I used NC to prevent random bouncing if left open plus a pull-up resistor plus debounce. The line runs for 30m and I used twisted shielded. ESP8266 My YAML is

binary_sensor:

  - platform: gpio

    name: "Front Gate PIR"

    device_class: motion

    pin:

      number: D4

      inverted: false

      mode:

        input: True

        pullup: True

    filters:

      - delayed_on: 300ms

Just connect GND from the Wemos to Com and the GPIO pin to Out. I am assuming that Com on the sensor is NOT connected to the negative of its power supply so if they are powered by different power supplies it should not matter as the ESP sees it as a dry contact. Doing so will result in the GPIO being high all the time, and low once the barrier is interrupted and the relay closes (since you are using N.O.). To invert this, just add inverted: true right under number: D4

This will now cause the line to appear Low at all times, and High when the barrier is interrupted.

I would disconnect the sensor for a moment, and short D4 to GND to see if the pin state changes. You should see it go low only when shorted to ground since you have pullup: true (which is good so it is not floating).

1 Like

Thanks for the help. I now have it working

I have put together a github page outlining the whole, code, build, test, custom PCB and Home Assistant automation for flashing a light once the beam has been broken.

1 Like