Binary Sensor constantly changing from on/off

I’m trying to use this sensor to tell when someone walks into my garage to turn the lights on. I am powering the sensor using a 12V 1amp power supply hooked to the brown & blue wires. The unit has a white common wire, a gray (NC) wire and black (NO) wire. I have the white common wire hooked to ground on a ESP32 board and the black (NO) wire hooked to GPIO 23. Here is my ESPHome code

> esphome:
>   name: motion_sensor_board
>   platform: ESP32
>   board: nodemcu-32s
> wifi:
>   ssid: "Too Fly For My WiFi"
>   password: "XXXXXXXXXXX"
>   #Static IP
>   manual_ip:
>     static_ip: 192.168.1.199
>     gateway: 192.168.1.1
>     subnet: 255.255.255.0
>   #Turn Off Power Save Mode
>   power_save_mode: none
>   # Enable fallback hotspot (captive portal) in case wifi connection fails
>   ap:
>     ssid: "Motion-Sensor-Board"
>     password: "XXXXXXXXXXXX"
> captive_portal:
> # Enable logging
> logger:
> # Enable Home Assistant API
> api:
> ota:
> binary_sensor:
>   - platform: gpio
>     pin: GPIO23
>     name: "Garage Sensor"

When the sensor is not tripped the log shows

[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF

When the sensor is tripped the log repeatedly goes from on to off several times per second

[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:15:59][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state ON
[21:16:00][D][binary_sensor:036]: 'Garage Sensor': Sending state OFF

Does anyone have any ideas on how to fix this?

I did some more troubleshooting. It doesn’t seem to be an issue with the sensor. I unhooked the sensor and had the same issue when the circuit was open. Closing the circuit (grounding the GPIO pin) stops it. I also tried GPIO22, GPIO12, & GPIO13 without a sensor hooked to them. All had the same issue.

Have you wired the ground from the power supply to ground in the esp32? Im not sure for your case, but with led strips you have to do that to work. Ground has to be ‘shared’.

I haven’t tried that, but I’m having the issue even with just the ESP32 powered with a USB plug and no sensor connected to it. Maybe I messed something up on the board when I hooked up the sensor with out connecting the grounds?

I tried connecting ground on ESP32 to the ground on my separate power supply powering the sensor. When the grounds are connected the infinite on/off toggle stops but the sensor change does not register on ESPHome. When the ground is disconnected I get the same issue as my original post. ESPHome does change when the sensor is tripped and the circuit is closed. But when the circuit is open I get the infinite on/off toggle again.

This is just a switch to ground according to your description so you need to enable the pull-up resistor for the pin. Otherwise all it sees is an floating input or ground. With the pull-up resistor it sees 3.3V or ground.

  binary_sensor:
    - platform: gpio
      pin: 
        number: GPIO23
        mode: INPUT_PULLUP
      name: "Garage Sensor"
2 Likes

Thank you @tom_l! that fixed it!

1 Like

Thanks Tom! That did the trick :slight_smile:

binary_sensor:
  - platform: gpio
    name: button
    pin:
      number: GPIO33
      inverted: true
      mode:
        input: true
        pullup: true

The config format has changed a bit in the 4 years since I wrote that. You got it sorted though :+1:

1 Like