What are the GPIO modes, and what are drive strength and slew rate?
Short answer: A GPIO pin can be configured as an input (floating, with pull-up, or with pull-down), an output (push-pull or open-drain), an alternate function (controlled by a peripheral such as UART TX or a timer PWM output), or analog (digital buffer disconnected, for ADC/DAC). Outputs also have configurable drive strength (how much current the pin can source or sink while keeping valid logic levels) and slew rate/speed (how fast the edges are).
| Mode | Use | Notes |
|---|---|---|
| Input floating | signals driven by another device | never leave unconnected inputs floating (noise, extra current) |
| Input pull-up / pull-down | buttons, open-drain signals, defined default state | internal pulls are weak (typically 20–50 kΩ) |
| Output push-pull | LEDs, chip selects, enables | drives high and low actively |
| Output open-drain | I2C, shared interrupt lines, level shifting with an external pull-up | only drives low |
| Alternate function | UART, SPI, I2C, timers, USB | the peripheral controls the pin; select the AF number in the pin mux |
| Analog | ADC inputs, DAC outputs, unused pins | lowest leakage; no Schmitt trigger input current |
Drive strength
- Rated in mA (e.g. 4, 8, 20 mA per pin) and limited per port and per chip. Exceeding it drops the output voltage below valid levels and can damage the pin.
- An LED needs a series resistor sized for the pin's capability; larger loads (relays, motors, LED strips) need a transistor or MOSFET driver.
Slew rate / speed setting
- Faster edges are needed for high-frequency signals (SPI at 50 MHz, SDIO, Ethernet RMII).
- Faster edges also cause more ringing, crosstalk, EMI and higher switching current. Use the slowest setting that meets timing, especially for long traces or cables.
Other configuration details
- Schmitt trigger inputs (hysteresis) reject noise on slow edges.
- Interrupts on edges (EXTI) for wake-up and events.
- Lock registers on some MCUs prevent accidental reconfiguration of critical pins.
- Reset state: most pins are inputs (often floating or analog) after reset; outputs that control hardware (motor enables, power switches) need external pull resistors so they are safe before firmware configures them.
Power tip: configure unused pins as analog (or output low) to minimise current; a single floating input can add tens or hundreds of µA in sleep.