Electronics for Building Things · Lesson 5

Buttons & Inputs

Why a button needs a resistor, and the "floating pin" gremlin.

So far we've pushed current out to light an LED. Now we read the world in — the first half of any home-automation gadget (a button, a switch, a motion sensor). It seems like you should just wire a button to a pin… and that's exactly the trap this lesson defuses.

The one win You understand why a bare button gives garbage readings, and you can wire one that reads reliably every time — using a pull-up or pull-down resistor.

The floating pin problem

A microcontroller input pin reads voltage: near the supply voltage it reads HIGH, near ground it reads LOW. The danger is in between. If a pin isn't firmly connected to either, it's floating — and it'll drift, picking up electrical noise from the air and reading randomly HIGH or LOW (SparkFun: Pull-up Resistors).

A simple button is just a gap that closes when pressed. Wire one between a pin and 3.3V and you've only defined one state: pressed connects the pin to 3.3V (HIGH). Released leaves the pin connected to nothing — floating. Your code can't trust it.

The fix: a pull resistor gives a default

A pull-up (or pull-down) resistor weakly ties the pin to a known voltage, so it has a definite state when the button isn't pressed.

3.3V 10kΩ pull-up → to input pin button
Pull-up wiring: the resistor holds the pin HIGH by default; pressing the button connects it to ground, pulling it LOW. The pin is never floating.
Rule of thumb (and a shortcut) Most microcontrollers, including the ESP32, have built-in pull-up resistors you enable in code (INPUT_PULLUP). Use them — wire the button between the pin and GND, enable the internal pull-up, and you skip the external resistor entirely.

One more gremlin: bounce

Mechanical buttons don't make clean contact — the metal physically bounces for a few milliseconds, so one press can register as several. The cure, called debouncing, is usually a few lines of code (ignore further changes for ~20–50 ms after a press) or a small capacitor. Just know the word — when a button "double-fires," bounce is the culprit.

Check yourself

Recall first.

Read this next (primary source) SparkFun — Pull-up Resistors. The definitive beginner explanation of floating pins and how a pull resistor fixes them.
I'm your teacher — ask me anything. "Pull-up or pull-down? Internal or external?" is worth asking for your specific button or sensor — bring it to me.

See also: Glossary · Rules of thumb