EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 5
GPIO: General-Purpose Input/Output
The pins that let firmware touch the real world.
Each GPIO pin can be an input or an output. As an output it drives a high or low voltage, enough for a small LED through a resistor. As an input it reads whether the voltage is above or below a threshold.
A floating input picks up noise and reads randomly. Fix this with a pull-up or pull-down resistor, often built into the chip and enabled in software.
Pins have current limits, so drive motors and relays through a transistor, never directly.
EXAMPLE
/* hypothetical register names for illustration */
DIR_REG |= (1u << 3); /* pin 3 becomes an output */
OUT_REG |= (1u << 3); /* drive it high */
OUT_REG &= ~(1u << 3); /* drive it low */
if (IN_REG & (1u << 5)) { /* pin 5 reads high */ }