ESP32 binary sensor bouncing

This is probably a really simple question, but I’ve been banging my head on it for a few days so figure it’s time to ask for a second opinion.

I have a reed sensor intended to determine if a door is opened or closed. First thing, making sure I have it wired correctly - 3.3v → sensor → GPIO15.

The relevant portion of my config is:

binary_sensor:
  - platform: gpio
    id: close_endstop
    pin:
      number: 15
      mode: INPUT_PULLUP

I’ve tried adding debouncing stuff also:

    filters:
      - delayed_on: 500ms
      - delayed_off: 500ms

Regardless, pulling the magnet ends up bouncing around, but from what the multimeter says things are stable unless maybe it doesn’t update fast enough?

[13:22:42][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:43][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:43][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:43][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:43][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:43][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:45][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:45][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:45][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:45][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:46][D][binary_sensor:036]: 'close_endstop': Sending state OFF
[13:22:46][D][binary_sensor:036]: 'close_endstop': Sending state ON
[13:22:48][D][binary_sensor:036]: 'close_endstop': Sending state OFF

If someone has an idea why dumb thing I’m missing, please let me know (or what other info is needed). :). I did try putting a 10K resister in as well rather than the internal pullup without any change.

Hi
GPIO 15 is one of the strapping pins( so the warning kept telling me when I tried uploading to esp device) I kept getting a warning when I used it as a physical momentary switch. Worked for first month then in recent esphome update it worked randomly. Move to a different pin is my advice. I moved to GPIO 19.

priority pins

Try any of these pins.

Andreas Spiess points the way. This link I found very useful for esp32 pins.

This is interesting, thanks. It seems like there’s a lot of little nuance with these boards I’m still trying to learn.

I tried both 19 and 27 and got the same result unfortunately.

It’s important to understand the various pins as @Spiro said, but I think your issue is a floating pin. You need to make either the pull resistor a pull down, or wire your sensor to ground. You may need to use the invert option too to get the right logic for your open and closed states. It depends on whether your sensor is NO/normally open or NC/normally closed.

In your current case, when the sensor is a closed circuit, your pin will sit at precisely 3.3v. No current will flow, so no resistor is necessary. However, when the circuit is open, your pin will now float since there is no reference. You need to pull it down to ground and you need a resistor to dissipate the current.

1 Like

Yes invert pin is next. This was my own setup on that binary sensor

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO16
      mode: INPUT_PULLUP
      inverted: True
    name: "momentary_button1"

Thank you both for your responses, and I know this is super basic but I’m still struggling with it. Mostly (I think?) the concept of pull up and down resistors – google keeps getting me so close to understanding, but something is just escaping me. I think I understand what a floating pin is - there is nothing to either keep it at 3.3v (in this case with the esp32) or ground without the appropriate resistor - of which the esp32 has internal ones.

I think I have this correct - a pull down resistor shorts the pin and ground to make sure there is no current on the pin unless it is “high” from the circuit being closed and a pull up resistor shorts the pin and 3.3v unless it goes low when the circuit is opened.

In the case of this reed switch it is NO (there is no continuity across it without the magnet, add the magnet and the circuit is closed), so if I use the INPUT_PULLDOWN resistor I would then wire one side of my reed switch to 3.3v and the other to the GPIO pin, correct?

I could also switch this around and use the INPUT_PULLUP and wire one side of the reed switch to the gpio pin and the other to ground and closing the circuit would pull the pin down.

Sorry for the basic question, trying to make sure I understand this - appreciate the comments!

What I understand the floating pin is that it’s neither 3v or GND. Electrical interference causes tiny current in the wire. We are surrounded by many electrical fields. Even putting you hand near the wire will cause interference and the sensor to flip on/off. Keeping it pulled up to 3v will stop it flipping about until it gets a definite change in the reed by moving the magnet.
10K Resistors seem to be a good compromise (for most simple sensors) so device can read Open and Closed easily with no false negatives and false positives. This is just the way I think of it anyway.
Pull up resistors are much more common than pull down as are NO reed switches. With a NO reed on a door I would expect that the door will be closed. Most of the time so the circuit will be closed most of the time. :nerd_face:
Have you tried the setup with “inverted: True”. If it works it works. :grinning:

Oh and look up the section in ESPhome on Debounce. This could also be the problem.

Ok, I finally got it working - thank you very much for your help @parautenbach and @Spiro!

Where I ended up is:

  - platform: gpio
    pin:
      number: 17
      mode: INPUT_PULLDOWN
    id: close_endstop
    filters:
      - delayed_on: 50ms
      - delayed_off: 50ms

with the reed switch between the 3.3v and gpio pin.

1 Like

For INPUT_PULLUP, the reed switch should be between the input pin (15) and ground. That is, when the reed switch is open, the internal pull-up resistor will define a logic high (3.3 volts). When it the switch is closed, it will be at ground potential (0 volts). I would prefer this to a pull-down resistor because you avoid routing the 3.3 volts to the door. If everything is DC isolated from some common ground, it wouldn’t make much difference.

There are also ways to protect the input from electrostatic damage, but for adequate protection, you’d need to use a separate pull-up resistor (and a few other components) versus the internal one. I mention this because there might be a long run of wire between the ESP32 and the door.

https://www.digikey.com/en/articles/protecting-inputs-in-digital-electronics

I’d consider what’s in Figure 11.

1 Like

Cool - thanks for the additional info! The great thing about the internet is there is so much information to try to teach yourself new things, but sometimes it’s hard to get the base knowledge needed while working on a project intended to get you the base knowledge. :slight_smile: