GPIO: Digital and Analog I/O
Pins, voltage levels, pull-up resistors, and ADC
Skills taught: embedded.gpio
General Purpose Input/Output
GPIO pins are the bridge between your microcontroller and the physical world. Every pin can be configured as an output (drive voltage high or low) or input (read external voltage). Arduino Uno has 14 digital pins (D0–D13) and 6 analog pins (A0–A5) that also work as digital GPIO.
Digital Output: Driving LEDs and Relays
// Digital output: control an LED
const int LED_PIN = 9; // any digital pin
void setup() {
pinMode(LED_PIN, OUTPUT); // configure as output
}
void loop() {
digitalWrite(LED_PIN, HIGH); // 5V on pin
delay(500);
digitalWrite(LED_PIN, LOW); // 0V on pin
delay(500);
}
Warning: Always use a current-limiting resistor (220Ω–1kΩ) in series with an LED. Without it, the LED draws too much current and either burns out instantly or damages the GPIO pin. Formula: R = (Vcc - Vf) / If = (5V - 2V) / 20mA = 150Ω minimum.
Digital Input: Reading Buttons with Pull-Up
An unconnected (floating) input pin picks up electrical noise and reads random HIGH or LOW values. You need to bias it to a known state using a pull-up or pull-down resistor. Arduino has a convenient built-in INPUT_PULLUP mode that connects an internal ~50 kΩ resistor to Vcc.
const int LED_PIN = 13;
const int BTN_PIN = 2;
void setup() {
pinMode(LED_PIN, OUTPUT);
// INPUT_PULLUP: pin reads HIGH when button not pressed
// When button connects pin to GND, it reads LOW
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
int state = digitalRead(BTN_PIN);
// Logic is INVERTED with pull-up: LOW = pressed
if (state == LOW) {
digitalWrite(LED_PIN, HIGH); // button pressed: LED on
Serial.println("Button pressed");
} else {
digitalWrite(LED_PIN, LOW); // button released: LED off
}
}
Tip: Always use INPUT_PULLUP instead of INPUT when reading buttons. Floating inputs give random, unpredictable readings. The logic is inverted: LOW means pressed (button pulls pin to GND), HIGH means released.
Analog Input: Reading Sensors with ADC
Arduino's 10-bit ADC converts an analog voltage (0–5V) to a digital number (0–1023). Use analogRead(A0) to read pins A0–A5. This is essential for sensors that output a variable voltage: potentiometers, thermistors, light-dependent resistors (LDR), and soil moisture sensors.
void setup() {
Serial.begin(115200);
// A0-A5 are automatically input — no pinMode needed for analog
}
void loop() {
int raw = analogRead(A0); // 0 to 1023
float voltage = raw * (5.0 / 1023.0); // convert to volts
float percent = raw / 1023.0 * 100.0; // 0–100%
Serial.print("Raw: "); Serial.print(raw);
Serial.print(" Voltage: "); Serial.print(voltage, 2); Serial.print("V");
Serial.print(" Pct: "); Serial.print(percent, 1); Serial.println("%");
delay(200);
}
Analog Output: PWM
PWM (Pulse Width Modulation) simulates analog output by rapidly switching a digital pin between HIGH and LOW at a fixed frequency. The duty cycle (percentage of time HIGH) controls the perceived voltage. analogWrite(pin, 0–255) sets the duty cycle on PWM-capable pins (marked ~ on Arduino Uno: 3, 5, 6, 9, 10, 11).
const int LED_PIN = 9; // must be a PWM pin (~)
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Fade in
for (int brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(LED_PIN, brightness); // 0=off, 255=full on
delay(10);
}
// Fade out
for (int brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(LED_PIN, brightness);
delay(10);
}
}
3.3V vs 5V Logic
- 5V Logic (Arduino Uno / Nano): GPIO operates at 5V HIGH, 0V LOW
- 3.3V Logic (ESP32 / Raspberry Pi / Arduino Zero): GPIO operates at 3.3V HIGH, 0V LOW
Warning: Never connect a 5V signal to a 3.3V GPIO pin (ESP32, Raspberry Pi, Arduino Zero/MKR). It will permanently damage the input protection diodes or the GPIO controller. Use a voltage divider (two resistors) or a dedicated logic-level converter IC.
Frequently Asked Questions
Can I connect a 5V sensor to a Raspberry Pi GPIO?
No! Raspberry Pi GPIO is 3.3V. A 5V signal will damage it. Use a voltage divider (10kΩ + 20kΩ resistors) or a dedicated bidirectional logic-level converter module.
My LED is very dim even at full analogWrite(255). Why?
Your resistor value may be too high, or the LED Vf (forward voltage) is higher than expected. Try a 220Ω resistor. Blue and white LEDs have ~3.2V Vf vs ~2V for red/green, so they're dimmer on 5V Arduino.
Why does my button cause multiple triggers?
Mechanical switch bounce — the contacts physically bounce on contact, generating multiple edges. Debounce in software with a 10–20 ms delay after first trigger, or use the Bounce2 library.