Digital input and output with GPIO
Configuring general-purpose pins as outputs or inputs, driving an LED safely, reading a switch, and respecting the current limits that keep a chip alive.
Lesson 2 of 6 · 25 min · Self-paced · Free
The microcontroller's fingertips
A robot's microcontroller has to switch on a warning light, sense whether a limit switch has been hit, and enable a motor driver. All three jobs use the simplest peripheral of all: general-purpose input/output, or GPIO. Each GPIO pin is a single wire between the chip and the outside world that can either drive a voltage (output) or sense one (input). Getting GPIO right, including its electrical limits, is the foundation for everything else in this module.
Outputs: driving a pin high or low
When a pin is configured as an output, your program decides its level:
- High (logic 1): the pin is connected internally to the chip's supply, so it sits near that voltage, commonly 3.3 V or 5 V.
- Low (logic 0): the pin is connected to ground, near 0 V.
Inside, a pair of transistors act as switches, one to the supply and one to ground. When the pin pushes current out to a load it is sourcing current; when current flows into it, it is sinking current.
Inputs: reading a level
Configured as an input, a pin becomes a sensor. It draws almost no current (its impedance is very high) and simply compares the voltage on it with thresholds. From the digital electronics module you know that a voltage near the supply reads as 1 and near ground reads as 0, with an uncertain band in between. A pin that is not connected to anything, a floating input, can read either value at random, picking up stray electric fields from your hand or nearby wires. The next lesson shows how to fix that.
Configuring pins in code
Every pin must be told its job, usually once at start-up. Each manufacturer provides a library with its own function names. The code below uses generic illustrative names: gpio_set_mode, gpio_write, gpio_read and delay_ms stand in for whatever your board's library provides. The structure is the same everywhere.
/* Illustrative only: gpio_set_mode, gpio_write and delay_ms
stand in for the functions your board's library provides. */
#define LED_PIN 13
int main(void) {
gpio_set_mode(LED_PIN, GPIO_OUTPUT);
while (1) {
gpio_write(LED_PIN, 1); /* pin goes high: LED on */
delay_ms(500);
gpio_write(LED_PIN, 0); /* pin goes low: LED off */
delay_ms(500);
}
}This is the embedded "hello world": a blinking light, one second per cycle. Reading an input looks similar:
/* Illustrative only: names stand in for your board's library. */
#define LED_PIN 13
#define SWITCH_PIN 2
int main(void) {
gpio_set_mode(LED_PIN, GPIO_OUTPUT);
gpio_set_mode(SWITCH_PIN, GPIO_INPUT);
while (1) {
int level = gpio_read(SWITCH_PIN); /* 0 or 1 */
gpio_write(LED_PIN, level); /* copy the switch to the LED */
}
}Under the hood, each of these calls writes or reads bits in a peripheral register, as described in the previous lesson. You will do that directly in the bitwise operations lesson.
Driving an LED: the series resistor
An LED (light-emitting diode) is not like a resistor. Once the voltage across it reaches its forward voltage, roughly 1.8 to 2.2 V for many red LEDs and around 3 V for many blue and white ones, its current rises very steeply. Connect it straight to a pin and the current is limited only by the pin's internal resistance: the LED, the pin or both can be damaged. So we add a series resistor to set the current.
The resistor has whatever voltage is left over after the LED takes its share. With Ohm's law:
R = (V_supply − V_LED) / IWorked example
A 3.3 V output drives a red LED with a forward voltage of about 2.0 V. We want about 5 mA, which is plenty for an indicator light.
R = (3.3 V − 2.0 V) / 0.005 A = 1.3 V / 0.005 A = 260 ΩResistors come in standard values, so pick the next one up, 270 Ω. Check the actual current:
I = 1.3 V / 270 Ω ≈ 0.0048 A = 4.8 mARounding up the resistance always makes the current slightly smaller, which is the safe direction. The resistor dissipates P = V × I = 1.3 × 0.0048 ≈ 0.006 W, far below what even a tiny resistor can handle.
Current limits: the rule that saves chips
Every datasheet lists an absolute maximum current per pin and a total for the chip or for groups of pins, commonly a few milliamps to a few tens of milliamps per pin. Two practical rules:
- Design well below the maximum. The absolute maximum is the point where damage may begin, not a recommended operating level.
- Add up all your pins. Eight LEDs at 10 mA each is 80 mA, which may exceed the chip's total even if each pin is fine.
A GPIO pin must never power a motor, a relay coil or a speaker directly. Motors need far more current than a pin can give, and when a coil switches off it produces a voltage spike that can destroy the pin. Instead, the pin controls a driver, such as a transistor or a motor driver chip, which switches the large current from a separate supply. The pin only provides a small control signal.
One more trap: voltage levels. A 5 V signal connected to a 3.3 V microcontroller's input can damage it unless the datasheet says that pin is 5 V tolerant. When two boards with different supply voltages must talk, use a level shifter or a voltage divider.
Recap
A GPIO pin is either an output, driving high (near the supply) or low (near ground), or an input that reads a voltage as 0 or 1. Configure each pin before you use it. Always limit LED current with a series resistor, calculated as R = (V_supply − V_LED) / I and rounded up. Respect per-pin and total current limits, use drivers for anything power-hungry, and never feed a pin more voltage than it is rated for.
Project
Calculate LED resistors, then try it in a simulator or on the bench
Using R = (V_supply − V_LED) / I, calculate the series resistor for each case, round up to the next standard value from this list (100, 150, 220, 270, 330, 470, 680, 1000 Ω) and recompute the actual current:
- 5.0 V supply, red LED at 2.0 V, target 10 mA.
- 3.3 V supply, green LED at 2.2 V, target 5 mA.
- 3.3 V supply, white LED at 3.0 V, target 3 mA.
Then write, on paper or in a free online microcontroller simulator, a program using the illustrative GPIO functions that turns on case 1's LED only while a switch input reads 1. If you build it for real, use only a USB-powered board and the resistor you calculated.
Deliverables
- Three resistor calculations with chosen standard values and actual currents
- A short comment on why case 3 leaves so little headroom
- Your input-to-output program, with every pin configured before use
Adult supervision requiredReal-hardware steps must be done with adult supervision, using only a USB-powered or 5 V-or-less board. Always include the series resistor, disconnect power before changing any wiring, and unplug immediately if a part becomes hot.
Knowledge check
Check your understanding
Go further
- General-purpose input/output (Wikipedia) ↗
Background on GPIO pins, their configuration options and typical uses.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.