ESP32 binary sensor and a photoelectric beam sensor

Hi all, I’m hoping someone will share insight with me. My garage is not very deep. I am trying to set up a threshold beam in my garage that if broken I can use HA to turn on a light. (Yes, a tennis ball on a string is much easier, but where is the fun in that?) So, if the beam is broken, a red light is on.

I am trying to set up a E-936-S45RRGQ photoelectric beam sensor with an ESP-WROOM-32 board (it does not support internal pull-ups.) The ESP32 board is talking to HA just fine. I’m powering the ESP board via USB connection.

I am powering the beam sensor with a 12VDC power supply. (Brown and blue wires).
image The beam sensor test light comes on when the beam is aligned correctly and goes off if I break the beam. Seco-larm tech support tells me that the relay output is a dry (no power) contact.

I have the white wire (com) connected to a GND on the ESP32 and the black wire (normally closed) connected to GPIO35. If the beam is connected, the power between GND and GPIO35 is 0 VDC, if the beam is broken the power floats between about .4 and .7VDC.

Here is the relevant YAML.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO35
      inverted: false
      mode:
        input: true
        pullup: false
    name: "a_input1"
    filters:
      - delayed_on: 10ms

I am inexperienced with ESP32 and electronics. My gut tells me that when the beam is connected, I need to see about 3.3V. But, I’m not sure how to deliver that.

All ESP32 modules support pull-ups.

Except for GPIs 34 to 39, which you are using:

Choose another GPIO and enable the pull up. The table lower down on this page is a good reference:

Here is my updated YAML. Switched to GPIO32.

Should I see state changes in the log for GPIO32 when I break the beam? HA still just shows the state as “On” all the time.

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO32
      inverted: false
      mode:
        input: true
        pullup: true
    name: "a_input1"
    filters:
      - delayed_on: 10ms

Most likely a wiring issue. Double check that White is connected to the board GND (not the beam sensor power supply ground). And that Black is connected to GPIO32.

If you have a multimeter you can also check the continuity between black and white as you break the beam to make sure it is working.

It is always possible I have wired up something wrong. And, I have been tinkering with this for a few weeks before I was ready to embarrass myself by asking for help. :slight_smile:

In my current wiring, I seem to have continuity between white and gray.

No. White and black. And does it change when you break the beam?

Black and white has continuity when the beam is establishes. It loses continuity when the beam is broken.

Then this should work:

What board definition are you using in your device config?

Here is the info from the YAML.

esphome:
  name: esp32-a
  friendly_name: ESP32-A

esp32:
  board: esp32dev
  framework:
    type: arduino

I’m using a cheap E3F-R2P1 optoreflective sensor from AliEx with a PNP output to turn on a light in a storage space and with this code. I can’t remember why I made the binary sensor internal and used a separate template switch but it works.

binary_sensor:
 
  - platform: gpio
    pin:
      number: GPIO5
      mode:
        input: true
        name: "IR Sensor"
    id: ir_sensor
    internal: true
    filters:
      - delayed_on: 10ms
    on_press:
        then:
          - switch.turn_on: store4_door_sw
          - switch.turn_on: break_led
    on_release:
        then:
          - switch.turn_off: store4_door_sw
          - delay: 2s
          - switch.turn_off: break_led
          
sensor:    

  - platform: wifi_signal
    name: "Store4 Door WiFi"
    update_interval: 60s   
    
switch:
  - platform: template
    name: "Store4 Door Switch"
    id: store4_door_sw
    optimistic: true
    
  - platform: gpio
    id: break_led
    pin:
      number: GPIO4
    internal: true
    restore_mode: ALWAYS_OFF

Thanks Gaz, that is an interesting sounding solution. That sensor’s range is 2m and my garage door is almost 6 meters wide.

I’ll try reconfiguring the binary sensor as a switch.

Don’t do that. It’s an input. Not an output.

Sadly, I did try. And, it did not work.

Can you share the logs from when you install the config?

Here is the log.

INFO ESPHome 2023.12.9
INFO Reading configuration /config/esphome/esp32-a.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing esp32-a (board: esp32dev; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Dependency Graph
|-- AsyncTCP-esphome @ 2.0.1
|-- WiFi @ 2.0.0
|-- FS @ 2.0.0
|-- Update @ 2.0.0
|-- ESPAsyncWebServer-esphome @ 3.1.0
|-- DNSServer @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- noise-c @ 0.1.4
RAM:   [=         ]  12.4% (used 40648 bytes from 327680 bytes)
Flash: [=====     ]  49.6% (used 910425 bytes from 1835008 bytes)
========================= [SUCCESS] Took 18.21 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.1.157
INFO Uploading /data/build/esp32-a/.pioenvs/esp32-a/firmware.bin (916192 bytes)
Uploading: [============================================================] 100% Done...

INFO Upload took 12.86 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.1.157 using esphome API
INFO Successfully connected to esp32-a @ 192.168.1.157 in 36.307s
INFO Successful handshake with esp32-a @ 192.168.1.157 in 1.508s
[18:25:14][I][app:102]: ESPHome version 2023.12.9 compiled on Feb 12 2024, 18:02:00
[18:25:15][C][wifi:573]: WiFi:
[18:25:15][C][wifi:405]:   Local MAC: E8:6B:EA:F6:FE:E4
[18:25:15][C][wifi:410]:   SSID: 'TechnoAlpin'[redacted]
[18:25:15][C][wifi:411]:   IP Address: 192.168.1.157
[18:25:15][C][wifi:413]:   BSSID: D8:EC:5E:46:A3:2B[redacted]
[18:25:15][C][wifi:414]:   Hostname: 'esp32-a'
[18:25:15][C][wifi:416]:   Signal strength: -53 dB ▂▄▆█
[18:25:15][C][wifi:420]:   Channel: 7
[18:25:15][C][wifi:421]:   Subnet: 255.255.255.0
[18:25:15][C][wifi:422]:   Gateway: 192.168.1.1
[18:25:15][C][wifi:423]:   DNS1: 192.168.1.1
[18:25:15][C][wifi:424]:   DNS2: 0.0.0.0
[18:25:15][C][logger:439]: Logger:
[18:25:15][C][logger:440]:   Level: DEBUG
[18:25:15][C][logger:441]:   Log Baud Rate: 115200
[18:25:15][C][logger:443]:   Hardware UART: UART0
[18:25:15][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'a_input1'
[18:25:16][D][api:102]: Accepted 192.168.1.13
[18:25:16][D][api.connection:1121]: Home Assistant 2024.2.1 (192.168.1.13): Connected successfully

And here is the full YAML minus the OTA pw and API key.

esphome:
  name: esp32-a
  friendly_name: ESP32-A

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "yada"

ota:
  password: "yadayada"

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



captive_portal:


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO32
      inverted: false
      mode:
        input: true
        pullup: true
    name: "a_input1"
    filters:
      - delayed_on: 10ms


Try this:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO32
      mode: INPUT_PULLUP
    filters:
      - delayed_on: 10ms

Another test to try:

Get a short piece of wire and carefully short the solder pads for P32 to GND directly on the dev board. Not on the screw terminal carrier board it is plugged into.

Does that cause the log to show the binary sensor turned off?

One more thing I just remembered. There is a dev boards that can not be powered properly via USB. You have to supply power via the 5V pin. Unfortunately I can’t find the forum post about this to compare it with yours. But it looks familiar…

Found it: Something is very wrong with the WiFi on the ESP32 boards I ordered from AliExpress. Is there any way to salvage these and fix the WiFi? - #5 by LoreNG

It does look like your board.

Tom, I really appreciate your sticking to this and all of your suggestions. All of the moderators and contributors here are what really makes Home Assistant successful. I can usually answer my own questions by searching. I was about to try another ESP32 board.

I think it is solved. I had taken the wiring apart and rewired everything a couple of times, but each time I think I had the polarity reversed. I finally tried it the other way. The Seco-larm documentation says the power input is non-polarized. But, I’m betting that only applies to AC.

1 Like