Embedded systems

GPIO (general-purpose input/output)

A GPIO is a single pin on a microcontroller that your program can drive high or low (output) or read as on or off (input) — the most basic way a chip touches the outside world. It is a software-controlled light switch and doorbell rolled into one tiny leg of metal: write a 1 to make it 3.3 volts (light an LED, close a relay), write a 0 to ground it, or set it to 'input' and ask whether a button is being pressed. 'General-purpose' means the same pin can do any of these, decided in firmware, not fixed in silicon.

Each GPIO is controlled by registers — a direction register that picks input or output, a data register that sets or reads the level. Inputs often need a pull-up or pull-down resistor so a disconnected pin reads a clean 1 or 0 instead of floating noise; most MCUs have these built in and switchable in software. GPIOs are also where you bit-bang protocols, blink the famous 'hello world' LED, and wire up sensors. Watch the current limit: a typical pin sources only ~20 mA, enough for an LED but not a motor, which needs a transistor to do the heavy lifting.

DDRB |= (1<<PB0); PORTB |= (1<<PB0); // set PB0 as output, drive HIGH

A common beginner bug: a button wired to a GPIO with no pull-up/pull-down resistor leaves the input 'floating', so it reads random 1s and 0s from ambient electrical noise. Enabling the internal pull-up fixes it in one line of code.

Also called
digital I/O pinGPIO 腳位通用 I/O