ESP32 - input pin state constantly changing

Hello all :wave:

I am trying to accomplish what seems to be basic task - to detect open circuit.
Schematic of my current setup is:

(despite setting a pin as pull up - I still add physical pull up as I though that maybe internal one is not enough. I also try a setup without a resistor connected.

I power it using USB with good quality “phone charger”.

Code for EspHome:

esphome:
  name: my-sensor-02

esp32:

  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

web_server:
  port: 80

# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

wifi:
  ssid: "my-wifi-2.0"
  password: "... secret ..."

  ap:
    ssid: "my-sensor-02 Fallback Hotspot"
    password: "... secret ..."


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO21
      mode: INPUT_PULLUP
    name: "my_sensor_8_state"
    device_class: opening
    filters:
      - delayed_on: 100ms
      - delayed_off: 100ms

I also tried syntax (cannot find definitive reference documentation which one is correct):

  - platform: gpio
    pin:
      number: 21
      mode:
        input: true
        pullup: true

When I close the circuit - it correctly reads ON - and the state is stable - will stay like this for multiple minutes.
When I open the circuit - the state changing multiple times a minute. This is log:

[15:03:08][C][logger:305]:   Hardware UART: UART0
[15:03:08][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'my_sensor_8_state'
[15:03:08][C][gpio.binary_sensor:015]:   Device Class: 'opening'
[15:03:08][C][gpio.binary_sensor:016]:   Pin: GPIO21
[15:03:08][C][web_server:161]: Web Server:
[15:03:08][C][web_server:162]:   Address: my-sensor-02.local:80
[15:03:08][C][mdns:112]: mDNS:
[15:03:08][C][mdns:113]:   Hostname: my-sensor-02
[15:03:08][C][ota:093]: Over-The-Air Updates:
[15:03:08][C][ota:094]:   Address: my-sensor-02.local:3232
[15:03:08][C][api:138]: API Server:
[15:03:08][C][api:139]:   Address: my-sensor-02.local:6053
[15:03:08][C][api:143]:   Using noise encryption: NO
[15:03:13][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:13][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:14][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:14][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:14][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:14][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:15][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:15][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:18][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:18][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:19][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:19][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:20][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:23][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:23][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:23][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:23][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:24][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:24][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:24][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:24][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:25][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:25][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:26][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:26][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:27][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:27][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:29][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:29][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:30][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:30][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:34][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:34][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:37][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF
[15:03:37][D][binary_sensor:036]: 'my_sensor_8_state': Sending state ON
[15:03:37][D][binary_sensor:036]: 'my_sensor_8_state': Sending state OFF

I also used another ESP32 board i had at home with same result.

Please help with figuring out how to get stable state with open circuit.

I can’t see where the wires are actually plugged up, but assuming they are on the correct pins:

  1. When you pull the switch LOW to GND you are going to one of the GND pins on the dev board?

  2. The pullup resistor should not be needed, but it won’t hurt. That should make a good connection between a 3V3 on the board and the pin being sensed. Seems like the connection somewhere might be weak.

  3. A slightly longer filter of 120ms 150ms might filter the rest out, but it the button being sensed is a momentary push button I would not go much longer. With a toggle switch you could probably go 500ms without anyone noticing the delay.

Good luck!