Exercises — GPIO — input - output, pull-up - pull-down, interrupt on pin change
5.5.2 · D4· Coding › Embedded Systems & Real-Time Software › GPIO — input - output, pull-up - pull-down, interrupt on pin
Do symbols baar baar aate hain, toh unhe ek baar plain words mein pin kar lete hain:

Upar ki figure dekho: yeh almost har problem ke liye reference circuit hai — ek internal pull-up resistor pin se tak, ek button pin aur ke beech, aur CPU ka sense buffer pin par "sunne" wala. Red element wahi hai jo har problem mein poochha jaata hai.
Level 1 — Recognition
L1.1 Ek pin is tarah configure ki gayi hai ki jab kuch connected ya pressed nahi hota, CPU logic 1 (HIGH) read karta hai. Kaun sa resistor enable hai — pull-up ya pull-down?
Recall Solution
Pull-up. Definition ke hisaab se pull-up pin ko se ek weak resistor ke through tie karta hai, isliye idle (undriven) read HIGH = logic 1 hoti hai. Pull-down use karein toh woh se tie hota aur idle LOW = logic 0 hota.
L1.2 Ek button active-LOW wire kiya gaya hai. CPU pin ko 0 read karta hai. Kya button pressed hai ya released?
Recall Solution
Pressed. "Active-LOW" ka matlab hai ki active (pressed) state LOW = 0 produce karta hai. Released → pull-up → HIGH = 1. Toh 0 ⇒ pressed.
L1.3 Almost har MCU par ek GPIO pin use karne ke liye aap jinhe touch karte hain, un teen registers ke naam batao, aur batao har ek kya control karta hai.
Recall Solution
- Direction register (
DDRx/MODER/TRIS) — input vs output set karta hai. - Output-data register (
PORTx/ODR) — woh HIGH/LOW jo aap output hone par drive karte ho. - Input-data register (
PINx/IDR) — woh level jo aap input hone par read karte ho.
Level 2 — Application
L2.1 . Ek internal pull-up of use kiya jaata hai. Jab button pressed hota hai (pin GND se button ke through short ho jaata hai), pull-up se kitna current flow karta hai? Apna jawab mein do.
Recall Solution
Jab pressed hota hai toh pin par hoti hai, toh poora , ke across drop hota hai. Poora kyun? Kyunki button short ke ek end ko GND par hold karta hai aur doosre end ko, isliye resistor ke across voltage hoti hai.
L2.2 Aapko ke saath pressed-state current ya us se neeche rakhni hai. Minimum pull-up resistance kya hai?
Recall Solution
Chhota ⇒ bada current, toh ek maximum current ek minimum deta hai. ke liye Ohm's law rearrange karo: Toh ≥ 100 kΩ use karo. Isse chhota kuch bhi button hold hone par allowed se zyada waste karega.
L2.3 Ek real output transistor pin ko LOW drive karta hai on-resistance ke saath, ek pull-up se tak fight karta hua. Pin actually kis voltage par settle hoti hai? Kya yeh valid LOW hai?
Recall Solution
Pull-up aur driver ek voltage divider form karte hain: se neeche push karta hai, driver GND ki taraf se pull karta hai. Divider kyun? Kyunki dono resistors se GND tak series mein hain pin as the middle tap ke saath, aur pin voltage woh fraction of hai jo bottom resistor ke across land hoti hai: Yeh essentially hai — ek rock-solid LOW. Yahi poora reason hai ki "weak" pull-ups kaam karte hain: strong driver unhe overwhelm kar deta hai.
Level 3 — Analysis
L3.1 Ek input pin mein koi pull resistor enable nahi hai aur kuch bhi drive nahi kar raha. Ek colleague kehta hai "yeh bas 0 read karta hai, yeh theek hai." Circuit terms mein explain karo ki read actually unreliable kyun hai, aur stray voltage physically kahan store hoti hai.
Recall Solution
Input ek high-impedance sense buffer hai — yeh almost koi current draw nahi karta, isliye yeh pin ko kahin nahi pull kar sakta. Pin aur nearby traces ek tiny capacitor (charge store) form karte hain. Kuch bhi voltage fix karne ke liye nahi hai, toh woh capacitor mains hum, nearby switching wires, yahan tak ki aapki ungli ke field se charge pick up karta hai. Toh read wahi hoti hai jo noise ne node ko last charge kiya tha — kabhi 0, kabhi 1, randomly. Ek weak pull resistor ya ke liye ek defined path supply karta hai, isliye capacitor hamesha ek known level par relax karta hai.
L3.2 Ek active-LOW button ke liye aapko interrupt ka trigger choose karna hai. Rising, falling, ya level? Justify karo ki baki do kyun galat hain.
Recall Solution
Falling edge. Active-LOW idle HIGH rehta hai aur press par LOW ho jaata hai, yaani ek transition = falling edge. Yeh exactly ek baar per press fire karta hai — woh event jis ki hume parwah hai.
- Rising edge release () par fire karega, press par nahi — galat moment.
- Level (LOW) continuously re-fire karta rahega jab tak button held hai, CPU ko flood kar dega. Ek edge ek one-shot event hai; ek level ek sustained state hai.
L3.3 Ek pressed button ek burst of ~14 edge interrupts 6 ms mein generate karta hai jabki aapne sirf ek baar press kiya. Is phenomenon ka naam batao, physical cause explain karo, aur ek concrete time window ke saath software fix do.
Recall Solution
Yeh switch bounce hai. Metal contacts physically slam karte hain, spring apart hote hain, aur ~1–10 ms mein settle hone se pehle kai baar re-touch karte hain, toh har micro-contact hardware ke liye ek real edge hai. Fix = debounce: pehle edge par, ek timestamp record karo aur aage ke edges ko ignore karo jab tak, maano, 20 ms elapsed nahi ho jaata (20 ms ~6 ms bounce window se comfortably zyada hai). Alternatively line ko ek RC network se filter karo taaki fast wiggles pin tak kabhi pahunche hi nahi. Switch Debouncing dekho.
Level 4 — Synthesis
L4.1 Ek keypad line ke liye pull-up design karo. Constraints: ; pressed-state current se neeche rehni chahiye; pull-up itna strong hona chahiye ki pin tak tak ka leakage LOW ko se upar na utha sake. Ek valid resistance range do.
Recall Solution
Upper bound (current cap). Bada = kam current, toh current limit ek minimum set karta hai: Lower bound... ruko, leakage ek maximum set karta hai. Jab button LOW pull karta hai, ek leakage current pull-up se bhi flow karta hai aur pin ko lift karta hai. GND ke upar pin voltage hai (pull-up ke across Ohm's law, as current ke saath). rakhne ke liye: Toh mein koi bhi kaam karega. Ek classic 100 kΩ us range mein safely baith jaata hai. (Related sizing logic: Pull-up Resistor Sizing & Ohm's Law.)
L4.2 Pin 3 par ek active-LOW button ke liye ek correct interrupt-driven skeleton (pseudo-C) likho jo (a) internal pull-up use kare, (b) per press ek baar trigger kare, (c) 20 ms se debounce kiya gaya ho, aur (d) -O2 optimization ke under safe ho. Batao kyun har safety choice wahan hai.
Recall Solution
volatile bool pressed = false; // shared ISR<->main => volatile
volatile uint32_t last_edge_ms = 0;
void ISR_PIN3() { // hardware jumps here on falling edge
uint32_t now = millis();
if (now - last_edge_ms >= 20) // debounce: ignore bounce burst
pressed = true;
last_edge_ms = now;
clear_interrupt_flag(PIN3); // else it re-fires immediately
}
int main() {
pin_mode(3, INPUT_PULLUP); // idle = HIGH (active-LOW)
attach_interrupt(3, FALLING, ISR_PIN3); // once per press
while (1) {
if (pressed) { handle_press(); pressed = false; }
}
}pressedaurlast_edge_msparvolatile: compiler otherwise unhe ek register mein cache kar leta hai aur main loop kabhi ISR ki writes nahi dekhta — exactly woh bug jo "debug mein kaam karta hai,-O2par break karta hai."- Falling trigger: active-LOW ke liye per press ek ISR.
clear_interrupt_flag: hardware ek flag latch karta hai; clear nahi kiya toh ISR forever re-enter karta hai.- 20 ms debounce window: bounce burst swallow karta hai. Tiny ISR body: ek flag set karo, heavy kaam
mainmein karo. Interrupts & ISR Design aur volatile & Memory-Mapped Registers dekho.
Level 5 — Mastery
L5.1 Ek student pin 5 ko ek push-pull output ke roop mein set karta hai 1 drive karte hue, phir usi loop mein pin 5 read karta hai ek button sense karne ki umeed mein jo us se wired hai. Read hamesha 1 hoti hai button se independent. Poori tarah diagnose karo: read stuck kyun hai, button current kya karta hai, aur correct fix kya hai.
Recall Solution
Ek push-pull output line ko actively drive karta hai: 1 drive karna top transistor on karta hai, pin ko se kuch ohms ke through hard-connect karta hai. Input register tab woh level return karta hai jo output force kar raha hai — 1 — button nahi. Isse bhi bura: agar button pressed hai toh yeh button ko us low-ohm transistor ke through short karta hai, ek near-short jo wastefully large current sink kar sakta hai ya pin ko damage kar sakta hai.
Fix: ek button sense karne ke liye aapko pin direction = INPUT pull-up ke saath set karni hai (INPUT_PULLUP), jo drive transistors disconnect karta hai aur world ko level set karne deta hai. Output aur input physically alag paths hain (Push-Pull vs Open-Drain Outputs aur Polling vs Interrupt-Driven I/O dekho).
L5.2 Ek shared line par do devices, ek open-drain output ek external pull-up ke saath tak. Jab ek device line ko LOW pull karta hai (), bus current compute karo. Phir explain karo ki aisi shared bus par open-drain kyun mandatory hai, push-pull kyun nahi.
Recall Solution
Jab ek device line ko LOW hold karta hai, poora pull-up ke across drop hota hai: Open-drain mandatory kyun hai: har device sirf LOW pull ya release kar sakta hai (Hi-Z ho jaao); shared pull-up HIGH provide karta hai. Toh agar do devices ek saath act karein, worst case dono LOW pull karein — harmless. Push-pull ke saath, ek device HIGH drive kare aur doosra LOW drive kare toh directly se do low-ohm transistors ke through connect ho jaata: ek destructive short. Open-drain wired-AND bus ko safe banata hai. Exactly aisa Open-drain & I²C Bus mein hota hai.
Recall Har formula ka ek-line recap jo use hua
- Current through a pull-up when pressed: .
- Minimum from a current cap: .
- Maximum from a leakage/voltage limit: .
- LOW voltage under a real driver (divider): .
Parent GPIO note par wapas jaao.