Reading a button with an ESP32 looks like the easiest job in electronics: one wire to a GPIO, one to ground, done. Then the input reports presses nobody made, one press counts as three, the board refuses to boot with the button held, or a doorbell wire running across the house triggers at random. All of these have known causes, and most are fixed with one resistor, a few lines of configuration or a cheap optocoupler.
This article covers the classic ESP32. The same ideas apply to the ESP32-C3, S3 and other chips, but their pin numbers and boot pins differ, so check the datasheet of the chip you have.
An open contact needs a pull-up
An input pin draws almost no current. The ESP32 datasheet gives a maximum input current of 50 nA. When the button is open, nothing holds the pin at either level, and it picks up whatever is nearby: your hand, a mains cable, the Wi-Fi radio. This is the "floating input" that ESPHome's documentation blames for endless ON/OFF events in the log.
The fix is a resistor that holds the pin in a known state while the contact is open. The common arrangement is:
- a pull-up resistor from the pin to 3.3 V,
- the button or contact between the pin and ground.
Open contact reads HIGH, closed contact reads LOW. The logic is inverted, which software handles in one line. This arrangement has a practical advantage: any device that pulls a line to ground, such as an open-collector output on an alarm panel or a relay contact, connects the same way.
The ESP32 has internal pull-up and pull-down resistors with a typical value of 45 kΩ. They are fine for a button on the same board or a short wire. Two exceptions:
- GPIO34 to GPIO39 are input-only and have no internal pull-up or pull-down. If you use them for a button, fit an external resistor, typically 10 kΩ.
- 45 kΩ is a weak pull-up. At 3.3 V it lets only about 73 µA flow when the contact closes, so a long wire can easily induce enough noise to cross the threshold. More on that below.
In the Arduino core, pinMode(pin, INPUT_PULLUP) turns on the internal pull-up. In ESPHome it is part of the pin schema:
binary_sensor:
- platform: gpio
name: "Hall button"
pin:
number: GPIO27
mode:
input: true
pullup: true
inverted: true
filters:
- delayed_on: 20ms
- delayed_off: 20ms
inverted: true makes a pressed button read as ON. The filters are the debounce, explained next.
Pins to keep buttons away from
Some ESP32 pins are read at reset to decide how the chip starts. The datasheet lists these strapping pins and their default levels:
| Pin | Default | What it decides |
|---|---|---|
| GPIO0 | pull-up | boot mode |
| GPIO2 | pull-down | boot mode |
| GPIO12 (MTDI) | pull-down | flash supply voltage (VDD_SDIO) |
| GPIO15 (MTDO) | pull-up | boot messages on UART0 |
| GPIO5 | pull-up | SDIO slave timing |
If GPIO0 is low at reset (with GPIO2 low or unconnected), the chip starts the serial bootloader instead of your program. A button on GPIO0 that happens to be pressed when power comes back, or a normally closed contact to ground on that pin, leaves the board waiting for a firmware upload. GPIO12 is worse. If it is high at reset, the flash supply switches to 1.8 V, and Espressif's esptool documentation warns that a module with 3.3 V flash may then fail to boot or flash because the flash browns out. A pull-up on GPIO12, or anything that holds it high at power-up, can cause exactly that.
Also avoid GPIO6 to GPIO11 (and GPIO16 and GPIO17 on modules with PSRAM), which Espressif says are usually connected to the module's flash and PSRAM, and GPIO1 and GPIO3, which carry the serial port used for flashing and logs. That still leaves plenty of safe pins: GPIO4, 13, 14, 16 to 19, 21 to 23, 25 to 27, 32 and 33 on most modules, and 34 to 39 with external resistors.
Contacts bounce
When metal contacts close, they do not make one clean connection. Jack Ganssle tested 18 switches, actuating each one 300 times and measuring the bounce on a scope. Leaving out two badly behaved switches, the rest bounced for 1557 µs on average, with a maximum of 6200 µs. One of the two outliers bounced for 157 ms when it opened. He also found that a lot of the "bounce" was really the voltage wandering slowly between logic levels while contacts wiped across each other, which the logic reads as a burst of random ones and zeros.
The ESP32 easily sees those transitions, so one press turns into several events. Software debouncing is the usual fix: accept a new state only after it has been stable for a set time.
- The Arduino Debounce example ignores changes until the reading has been stable for 50 ms (
debounceDelay = 50). - ESPHome has binary sensor filters for this.
delayed_onpublishes ON only after the input has stayed ON for the given time,delayed_offdoes the same for OFF, anddelayed_on_offsets both at once. Thesettlefilter publishes a change immediately and then ignores further changes until the input has been stable for the given time, so the first edge is not delayed.
For pushbuttons, values between 10 and 50 ms are typical: ESPHome's documentation uses 10 ms in its example, the Arduino sketch 50 ms, and DigiKey's article mentions industrial practice of waiting 20 ms. All of them are well above the 6.2 ms Ganssle measured on ordinary switches. A door contact or a float switch can use longer values, because nobody notices a 200 ms delay on a door. settle is the better choice where reaction time matters, such as a light switch.
Debouncing in hardware
A resistor and capacitor can filter bounce before it reaches the pin. The problem is the slow edge the capacitor produces. The ESP32 reads anything below 0.25 × VDD (0.825 V at 3.3 V) as low and anything above 0.75 × VDD (2.475 V) as high. In between the level is undefined, and a slowly rising voltage spends a long time there, which can produce exactly the random transitions you were trying to remove. The classic hardware solution, described in DigiKey's article on debouncing, feeds the RC network into a gate with a Schmitt trigger input, such as the 74HC14, which switches cleanly once.
In practice, for an ESP32 that already runs software, keep any capacitor at the pin small. It should suppress fast spikes, not replace the debounce, and the software filter should do the rest.
Long cables
A wire of several metres between the ESP32 and a doorbell button, a gate contact or a sensor in another room behaves like an antenna. Combined with the weak internal pull-up, it produces presses nobody made. Advice from an Electronics Stack Exchange thread about a 15 m doorbell wire that kept ringing on its own sums up what helps:
- Use a much stronger pull-up. Replace the internal 45 kΩ with an external resistor of 1 to 10 kΩ. With 4.7 kΩ, about 0.7 mA flows when the contact closes, ten times more than through the internal pull-up, and induced noise has a much harder time pulling the line across the threshold.
- Put a small capacitor at the input, in the range of 1 to 10 nF, to short high-frequency interference.
- Use a twisted pair for the two wires to the contact, and keep the run away from mains cables where you can.
- Require the contact to stay closed for longer in software. The same thread suggests around 100 ms for a doorbell.
- Add ferrite beads on both wires if the noise persists.
If the cable leaves the building, runs next to mains wiring for a long distance, or connects to equipment with its own power supply, do not connect it directly to the ESP32 at all. Use an optocoupler.
Optocouplers: signals from 12 V, 24 V and other supplies
An optocoupler has an LED on one side and a phototransistor on the other, with no electrical connection between them. The signal side can run at 12 V, 24 V or come from a separate supply, and the ESP32 side sees only a transistor that pulls its input to ground.
The PC817 is the usual cheap choice. Its datasheet gives these values:
- LED forward current: 50 mA maximum,
- LED forward voltage: 1.2 V typical, 1.4 V maximum at 20 mA,
- LED reverse voltage: 6 V maximum,
- current transfer ratio (CTR): at least 50% at 5 mA LED current,
- isolation voltage: 5 kV rms.
To read a 12 V signal, size the series resistor for about 5 mA: (12 V - 1.2 V) / 5 mA = 2160 Ω, so a standard 2.2 kΩ resistor gives just under 5 mA. For 24 V the same calculation gives about 4.6 kΩ, so 4.7 kΩ. On the ESP32 side, connect the phototransistor's emitter to ground and its collector to the GPIO with a 10 kΩ pull-up to 3.3 V. Pulling that pull-up low takes only about 0.33 mA, and with a CTR of at least 50% the transistor can sink 2.5 mA at 5 mA LED current, a wide margin. The logic is the same as with a button: signal present reads LOW.
Two details that catch people out:
- Reverse polarity and AC. The LED tolerates only 6 V in reverse. If the signal can be reversed, or is AC (for example from a doorbell transformer), put an ordinary diode in reverse parallel with the LED.
- AC gives pulses, not a steady level. The LED lights on only one half-cycle, so at 50 Hz the output turns off for about 10 ms in every 20 ms cycle. Add a
delayed_offfilter longer than that, such as 50 ms, or the input will chatter as long as the bell is pressed.
Mains voltage is a separate story. The isolation rating of an optocoupler on a breadboard or a cheap module does not make a hobby circuit safe for 230 V. To detect whether a mains circuit is live, use an installation relay with a 230 V coil, fitted in the distribution board by someone qualified, and read its dry contact like any other button.
Common mistakes
- Input left floating because the pin is GPIO34 to 39 and the internal pull-up was assumed. Fit an external resistor.
- Button on GPIO0, 2, 12 or 15 and a board that sometimes will not boot. Move it to a safe pin.
- Debounce time too short for the contact. A cheap button or an old wall switch may need more than 10 ms. Look at the raw events in the log and increase the time.
- Internal pull-up on a long cable. Switch to an external 4.7 kΩ resistor and a small capacitor before anything else.
- A 12 V signal wired straight to a GPIO. The pin is rated for at most 3.6 V. Use an optocoupler or, for a slow signal from the same supply, a voltage divider.