Learn / Microcontrollers / GPIO: inputs and outputs

Digital Inputs and Outputs

ReviewedBeginnerFreeAbout 5 min
ESP32
In short

Digital Inputs and Outputs: the short answer

Digital pins operate in defined logic states. Limits are specific to the microcontroller and development board; do not connect higher-voltage signals directly. Example: No external resistor needed and no floating input. Remember the logic is reversed: LOW means pressed.

Level
Beginner
Time
About 5 min
Topic
Microcontrollers
You will learn to

Read a button reliably and drive an LED from a microcontroller pin.

Before this: LEDs and Current Limiting

Digital pins operate in defined logic states. Limits are specific to the microcontroller and development board; do not connect higher-voltage signals directly.

Logic-high and logic-low thresholds are part-specific and are not interchangeable with maximum allowed input voltages. Pins can have bootstrapping, analog, interrupt or special-function limitations that depend on the board.

Sketch a button input using a defined pull-up or pull-down strategy; then explain what an open input means. Never assume a 5 V output can be wired directly to a 3.3 V-only input.

What high and low really mean

A digital input decides whether a voltage is high or low by comparing it to two thresholds. On a 3.3 V ESP32 an input reads high above roughly 0.75 of VDD (about 2.5 V) and low below about 0.25 of VDD (about 0.8 V). Between those numbers the result is not guaranteed. A slowly changing voltage, for example from a touch or a weak sensor, can read as either, or flicker.

An output swings close to the supply (3.3 V or 5 V) when high and close to 0 V when low, but only when it supplies a small current. Pins are for signals. A typical limit is about 20 mA a pin on an Uno and 40 mA absolute maximum, and an ESP32 pin is happier at 20 mA or less. Use a transistor or driver for anything bigger.

The floating input problem

An input connected to nothing is not low. It is undefined, and picks up mains hum and the hand that approaches it, so it reads random values. A button that connects the pin to 3.3 V only when pressed leaves the pin floating when it is released.

The cure is a resistor that gives the pin a default level: a pull-down to ground (so the pin reads low until the button pulls it high) or a pull-up to the supply (so it reads high until the button pulls it low). Most microcontrollers have built-in pull-ups of 20 to 50 kΩ you can switch on in software with INPUT_PULLUP, which is why a button to GND needs no extra resistor.

Bounce: one press, many edges

A mechanical contact does not close cleanly. For a few milliseconds it makes and breaks, so one press can read as five to twenty edges. If you count presses, you will count too many. Fix it in software by ignoring changes for about 20 to 50 ms after the first one, or in hardware with a small capacitor (100 nF) across the switch.

Reading the button once per loop and acting only when the state changes from the last read, rather than on every high reading, also stops a held button from repeating.

Pins are not all equal

On an ESP32 some pins affect boot if something pulls them the wrong way at power-up (GPIO0, 2, 5, 12 and 15 have boot roles), some are input-only (GPIO34 to 39 have no pull-ups), and some are used by the flash chip and must be left alone (GPIO6 to 11 on many boards). On an Uno, pins 0 and 1 are shared with the USB serial link.

Before wiring, look up your exact board's pinout, and keep a note of which pins you used. Choosing a safe pin at the start saves hours of strange behaviour later.

Mixing 5 V and 3.3 V

A 5 V output into a 3.3 V input can damage it, quietly and slowly. In the other direction, a 3.3 V output into a 5 V input often works only by luck, since 3.3 V sits just above the 5 V CMOS part's high threshold of about 3.0 to 3.5 V, and it fails when the supply sags. Use a level shifter, a 2-resistor divider on a slow signal, or pick parts that run at the same voltage.

Common mistakes
  • Leaving a button input floating, then blaming the code when it triggers on its own.
  • Using INPUT_PULLUP and then reading the button as if high meant pressed. With a pull-up, pressed reads low.
  • Driving a relay or buzzer directly from a pin that can supply only 20 mA.
Worked example

Read a button without extra resistors

  1. Wire the button between D2 and GND.
  2. In setup: pinMode(2, INPUT_PULLUP). The pin now reads HIGH when released.
  3. Pressed connects the pin to GND, so it reads LOW.
  4. In loop: if (digitalRead(2) == LOW) turn the LED on.

Result. No external resistor needed and no floating input. Remember the logic is reversed: LOW means pressed.

Try it

Write pseudocode for reading a button and controlling an LED.

Check yourself

3 quick questions

  1. A button input reads random values when the button is not pressed. Why?

  2. With INPUT_PULLUP and the button wired to GND, what does the pin read while pressed?

  3. What logic level do ESP32 GPIO pins use?

Common questions

What will I learn in "Digital Inputs and Outputs"?

Read a button reliably and drive an LED from a microcontroller pin.

How do I read a button without extra resistors?

Wire the button between D2 and GND. In setup: pinMode(2, INPUT_PULLUP). The pin now reads HIGH when released. Pressed connects the pin to GND, so it reads LOW. In loop: if (digitalRead(2) == LOW) turn the LED on. No external resistor needed and no floating input. Remember the logic is reversed: LOW means pressed.

A button input reads random values when the button is not pressed. Why?

The pin is floating; it needs a pull-up or pull-down. An unconnected input picks up noise. A pull-up or pull-down resistor holds it at a known level.

With INPUT_PULLUP and the button wired to GND, what does the pin read while pressed?

LOW. Pressing connects the pin to GND, so it reads LOW. Released, the pull-up holds it HIGH.

What logic level do ESP32 GPIO pins use?

3.3 V, and they are not 5 V tolerant. ESP32 I/O runs at 3.3 V. Putting 5 V on a pin can damage it.