GPIO Binary Sensor goes wacky

Hi everyone! I’ve set up 2 esp32 with relay boards and one gpio binary sensor each that are connected to a simple switch (no voltage or anything) so that when i press the switch, the binary sensor triggers an automation in HA. it worked well enough for a few days, but now the binary sensors turn on and off multiple times on one esp and permanently stays on on the second esp without touching any switches… I checked the connections and switches and they are good. I changed the gpio pin on one esp, works fine for a few days, then it starts acting up on that gpio pin the same as previous. Some are just random, while others start after i turn on or off one of the relays. BTW, the relays are working fine (no random on/off). Same after 2023.5.1 update. Board is esp32 dev.

Has anyone else experience this? Here is my code below:

binary_sensor:
  - platform: gpio
    pin:
      number: 17
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Garage Button"
    filters:
      - delayed_on: 150ms
1 Like

Weird. Sounds like you’ve ticked off on the usual suspects to me.

Now to be honest I can’t say I really understand how they work, but have you tried removing your inverted and/or trying pulldown? But I doubt that’s it.

Are you soldered or Dupont-ed?

How long are the cables to the switches?

One cable is about 15 centimeters and the other is about 6 meters. All cables terminate into duponts.
Thing is… on one esp it only started misbehaving after i interacted with said esp, for ex: turn on the relay…
If i disconnect the cables, they go back to off. Maybe they are picking some sort of interference and the esp’s are too sensitive? But then why would one start misbehaving only after i send a command to the esp? The other just started randomly going haywire out of nowhere without any interaction…

That is probably it. Especially with the 6m length one.

Try putting a 0.1uF MLC capacitor between the GPIO and GND. As close as possible to the GPIO pin, keeping the leads of the capacitor as short as possible.

I’ll try measuring with a multimeter to see what is going on, especially the one that goes wacky after i interact with it, but i will probably cancel the button functionality for now

For anyone else stumbling across this problem in the future, in the end i moved the switches from pin 17 to pin 16 on both esps and never had any issues so far. I think that pin 17 is not good for this so i’ll try to avoid it in future projects.

Both gpio16 and 17 are fine to use as inputs. See the table here:

I followed that exact guide when starting the project to see which pins to use, but for some reason both esps had issues with pin 17 used with a switch. Who knows… Maybe this was just a firmware bug, after all there were 2 updates installed in the meantime that solved the issue, not the action of changing to pin 16…

well, it’s been a little while and both esp’s went haywire once again… after some thought, i figured that the most probable cause is a faulty pullup that eventually lets the pin float… question is: how do i wire an external pullup for esp32 with multiple switches? hopefully i may be able to save those 2 projects…

You use a physical resistor and connect it between 3.3v and the gpio, that’s “pulled up to positive” pulling it down, you’d connect the resistor between ground and gpio “pulled down to ground” There’s tons of guides and information available to anyone that makes an effort to only do a Google search. You also need to specify which esp32 your using when asking for help. People keep using that Random Nerd link as an esp32 pinout and there are dozens of esp32 dev boards and they don’t all match the pinout from that website.

thanks for the info! i went on hours long google search but found mixed answers (mostly concerning arduino boards), so i posted a question here just to be sure and not fry something :slight_smile: everyone says that a 10 kohm resistor should do the job, correct? should i need multiple resistors for multiple pins (switches), or can i just use one? answers online are not very clear… the esp dev board i bought is probably a knock-off, but is the only vendor i found so far in my country and does not have a pinout schematic of any kind… i did however tried multiple pins on the boards with the same result. i will try one switch with the spare board i have and the wiring of the resistor you sugested and post my results here

One resistor for each pin.

For 95% of things you find that deal with wiring, it usually doesn’t matter if it sais Arduino, esp32 or esp8266 because they’re all nearly identical.

thank you for the great info! i will try it this weekend and check back in after a little while with the results!

Coming back here to say it works well so far. Thanks again for the great info!

1 Like

Awesome! Just an FYI, pull-up/down resistors are extremely common in circuits. Understanding the differnce between the two and when to use which one, its something i would recommend taking some time to read about and learn because your going to run into these a lot and Understanding them will help you tremendously.