5.5.2Embedded Systems & Real-Time Software

GPIO — input - output, pull-up - pull-down, interrupt on pin change

2,403 words11 min readdifficulty · medium

WHAT is GPIO?

Each physical pin connects to internal registers. The three you always touch:

Register WHAT it controls Typical name
Direction input or output DDRx, MODER, TRIS
Output data the HIGH/LOW you drive PORTx, ODR
Input data the level the pin reads PINx, IDR

HOW an output pin works (push-pull)

Inside the chip each output pin has two transistors stacked between VDDV_{DD} and GNDGND:

  • Drive HIGH → top transistor ON → pin connected to VDDV_{DD}.
  • Drive LOW → bottom transistor ON → pin connected to GNDGND.

This is called push-pull because it can actively push current out (source) and pull current in (sink).


HOW an input pin works + the floating problem

The fix: a weak pull resistor that gently ties the pin to a known level when nothing else drives it.

Figure — GPIO — input - output, pull-up - pull-down, interrupt on pin change

WHY the resistor value matters (derive the current)

Picture a button between the pin and GNDGND, with an internal pull-up RR to VDDV_{DD}.

  • Button open: no current flows through RR (the input buffer draws ~0). By Ohm's law the pin sees VDDV_{DD} → reads HIGH. ✓
  • Button pressed: pin is shorted to GND through the button. Current flows VDDRGNDV_{DD}\to R \to GND:

I=VDDRI = \frac{V_{DD}}{R}


Active-LOW buttons (the classic wiring)


HOW interrupts on pin change work

Two ways to know a pin changed:

  1. Polling — loop forever reading the pin. Simple but wastes CPU and can miss fast pulses.
  2. Interrupt — hardware watches the pin and, on a chosen event, yanks the CPU into a special function (the ISR, Interrupt Service Routine).

Switch bounce (the hidden bug)


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a doorbell wire to your brain. If you push electricity out the wire, that's output — you're telling a light to turn on. If you just feel whether the wire is hot or cold, that's input — you're listening for a button. But a wire with nothing connected is like a swing in the wind: it wobbles randomly. So you tie it with a soft rubber band — to the ceiling (pull-up = normally "on") or to the floor (pull-down = normally "off") — so it sits still until a real push moves it. An interrupt is a tap on your shoulder: instead of asking "did someone ring?" a thousand times a second, the wire taps you the instant it changes, so you can do other stuff meanwhile. Only catch: a real button shivers when pressed (bounces), so you ignore taps for a moment after the first.


Connections

  • Pull-up Resistor Sizing & Ohm's Law
  • Interrupts & ISR Design
  • Polling vs Interrupt-Driven I/O
  • Open-drain & I²C Bus
  • Switch Debouncing
  • volatile & Memory-Mapped Registers
  • Push-Pull vs Open-Drain Outputs

Flashcards

What does GPIO stand for and why "general purpose"?
General Purpose Input/Output; the pin isn't reserved for a fixed peripheral, software sets its direction/behaviour.
Why does a floating input pin give random reads?
It's high-impedance with tiny capacitance, so it picks up ambient noise; nothing defines its voltage.
Pull-up vs pull-down: default read level of each?
Pull-up → idle HIGH (1); pull-down → idle LOW (0).
Why must a pull resistor be "weak" (large)?
So any real driver (low resistance) easily overrides it via the voltage divider, giving a clean signal.
Formula for current wasted through a pull-up when the input is grounded?
I=VDD/RI = V_{DD}/R.
Why is the classic button "active-LOW"?
Pull-up keeps idle HIGH; pressing connects pin to GND → reads LOW, so pressed = 0.
Push-pull vs open-drain output?
Push-pull can drive both HIGH and LOW; open-drain only pulls LOW or goes Hi-Z and needs an external pull-up.
Edge vs level interrupt trigger — which for a button and why?
Edge (falling for active-LOW): one ISR per press; level would re-fire continuously while held.
Why must an ISR-shared flag be volatile?
Forces re-read from memory each time; otherwise the compiler may cache it in a register and never see the ISR's update.
Why keep ISRs short?
Other interrupts may be blocked; long ISRs cause missed events and timing jitter — just set a flag and handle in main loop.
What is switch bounce and how do you fix it?
Mechanical contacts produce a burst of edges for ~1–10 ms; fix by debouncing (ignore further edges for ~20 ms) or RC filtering.
What current does a 33 kΩ pull-up waste at 3.3 V when pressed?
3.3/33000=100 μA3.3/33000 = 100\ \mu A.

Concept Map

configured by

direction via

output via

input via

sets mode

sets mode

default type

alt type

enables shared bus

risks

fixed by

to VDD gives

to GND gives

change signals

GPIO pin

Software registers

DDRx MODER TRIS

PORTx ODR

PINx IDR

Output mode

Input mode

Push-pull two transistors

Open-drain pull LOW or Hi-Z

I2C bus

Floating noise random reads

Weak pull resistor 10-50k

Pull-up default HIGH

Pull-down default LOW

Interrupt on pin change

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, GPIO ka matlab hai ek aisa pin jise tum software se ya to output bana sakte ho (CPU pin pe HIGH/LOW push karta hai, jaise LED jalana) ya input (CPU bas sun raha hai ki pin HIGH hai ya LOW, jaise button check karna). Output me andar do transistor hote hain (push-pull) — ek upar VDD se jodta hai, ek niche GND se. Input me ek high-impedance sense buffer hota hai jo bas voltage padhta hai.

Sabse bada beginner bug: agar input pin pe kuch connected nahi hai to wo float karta hai — matlab random noise uthata hai aur button bina dabaye "pressed" dikhane lagta hai. Iska fix hai ek weak pull resistor: pull-up (pin ko VDD se, idle = HIGH) ya pull-down (pin ko GND se, idle = LOW). "Weak" yaani bada (~33kΩ) taaki jab koi real signal aaye to wo aaram se override kar de. Classic button wiring hoti hai pull-up + button GND se — isliye dabane par 0 milta hai (active-LOW). Current waste = VDD/RV_{DD}/R, isiliye resistor zyada chhota mat rakhna.

Ab interrupt ka jaadu: polling me CPU baar-baar pin padhta rehta hai (CPU time waste, tez pulse miss ho sakta hai). Interrupt me hardware khud pin ko watch karta hai aur jab edge aata hai (falling edge button ke liye), CPU ko tap karke ISR me le jaata hai. Do cheezein yaad rakhna: ISR ko chhota rakho (bas ek flag set karo, baaki kaam main loop me), aur ISR-shared variable ko volatile banao warna compiler use cache kar lega aur change dikhega hi nahi. Aur ek aur cheez — mechanical button dabate waqt bounce karta hai (1–10 ms ke andar kai edges), to ek press 10 baar trigger kar sakta hai; iska solution debounce (~20 ms tak agle edges ignore karo). Bas yahi GPIO ka pura khel hai.

Go deeper — visual, from zero

Test yourself — Embedded Systems & Real-Time Software