Worked examples — GPIO — input - output, pull-up - pull-down, interrupt on pin change
5.5.2 · D3· Coding › Embedded Systems & Real-Time Software › GPIO — input - output, pull-up - pull-down, interrupt on pin
Kuch bhi aur karne se pehle, teen plain-word reminders taaki koi bhi symbol bina explain ke use na ho:
Scenario matrix
Har GPIO exercise inhi cells mein se kisi ek mein aata hai. Neeche ke examples us cell ke saath labelled hain jo woh cover karte hain, isliye milake woh sab ko hit karte hain.
| Cell | Axis | The case | Example |
|---|---|---|---|
| A | Logic level | Pull-up + open switch → reads HIGH | Ex 1 |
| B | Logic level | Pull-up + pressed switch → reads LOW (active-LOW) | Ex 1 |
| C | Logic level | Pull-down + pressed-to- → reads HIGH (active-HIGH) | Ex 2 |
| D | Degenerate | Koi pull nahi → floating → undefined read | Ex 3 |
| E | Sizing | choose karo taaki wasted current cap ho (normal range) | Ex 4 |
| F | Limiting | small (few ) → huge current, wasted power | Ex 4 |
| G | Limiting | large (M) → noise wins → flaky | Ex 4 |
| H | Fight/divider | Strong driver LOW vs pull-up → kaun jeeta hai? (voltage divider) | Ex 5 |
| I | Timing | Bounce → spurious interrupts count karo; debounce window | Ex 6 |
| J | Word problem | Real device: current budget over many pins | Ex 7 |
| K | Exam twist | Pin OUTPUT set karo phir read karo → written value milti hai, button nahi | Ex 8 |
Example 1 — Cells A & B: pull-up + active-LOW button
Neeche ki figure wohi circuit do baar draw karti hai — open left par, pressed right par. (upar lavender rail) se neeche butter-yellow resistor ke through black "pin" dot tak, phir button tak wire trace karo. Dekho neeche ka coloured badge mint HIGH se coral LOW mein flip hota hai.

- Open button, current trace karo. Left circuit mein pin se ek hi path hai upar ke through tak; button ka lever utha hua hai (open), toh kuch bhi se connect nahi hai. Sense-buffer (upar define kiya) essentially zero current draw karta hai. Yeh step kyun? Ohm's law kehta hai ke paas drop hone wali voltage hai. Agar , toh , isliye resistor ke paas koi voltage nahi khoti.
- Level conclude karo. ke paas koi drop nahi hone par, pin par baith jaati hai → HIGH = 1. Left badge mint hai. (Cell A)
- Pressed button, current trace karo. Right circuit mein button (coral bar) pin ko seedha se short karta hai (near-zero-ohm path). Pin par pin ho jaati hai. Yeh step kyun? Ek hard ground connection weak pull-up ko completely dominate karta hai — Ex 5 mein divider argument dekho.
- Level conclude karo. Pin par → LOW = 0. Right badge coral hai. (Cell B)
Toh pressed 0 read karta hai — iska matlab yahi hai "active-LOW". Press ke dauran kya hota hai uske liye dekho Switch Debouncing.
Recall Check yourself
Pull-up + button-to-GND ke saath, OPEN state kya read karta hai? ::: HIGH (1) Wohi wiring mein, PRESSED kya read karta hai? ::: LOW (0), yaani active-LOW
Verify: open → (HIGH). Pressed → (LOW). ✓
Example 2 — Cell C: pull-down + active-HIGH button
- Open button. Ek hi path hai neeche ke through tak. Sense-buffer ~0 draw karta hai, isliye ke paas koi drop nahi → pin par baith jaati hai → LOW = 0. Yeh step kyun? Wohi "no current ⇒ no voltage lost" logic jaise Ex 1 mein, lekin ab resistor hume ground ki taraf tie karta hai supply ki taraf nahi.
- Pressed button. Button pin ko se short karta hai → pin par → HIGH = 1. (Cell C) Yeh step kyun? se seedha low-resistance connection weak pull-down ko overwhelm karta hai — wohi "hard connection weak pull ko jeet laata hai" idea jaise Ex 1 ka pressed case, bas upar ki taraf point karta hua neeche ki taraf nahi. Hume yeh half explicitly dikhana hai taaki reader dekhe ki pressed genuinely HIGH hai, sirf symmetry se assume nahi kiya gaya.
Yeh wiring active-HIGH hai (pressed = 1). Yeh zyaada naturally padhti hai lekin kam use hoti hai kyunki zyaadatar MCUs pull-ups bundle karte hain aur grounding electrically cleaner hai (Ex 1 ki wajah).
Verify: open → (LOW); pressed → (HIGH). Levels exactly Ex 1 ke versus swapped hain. ✓
Example 3 — Cell D: floating (degenerate) input
Example se pehle, ek naya idea jo humhe chahiye:
Figure floating pin ki voltage ko time ke saath plot karta hai (lavender wiggle). Coral dashed line sense-buffer ka decision threshold hai par; har coral dot wahan mark karta hai jahan wandering voltage cross karti hai aur read "reads 1" (mint zone, upar) aur "reads 0" (neeche) ke beech flip hoti hai.

- Pin ko model karo. Ek undriven input woh tiny capacitor hai (kuch pF, upar define kiya) jo ek super-high-impedance sense-buffer se attached hai. Koi bhi resistor isse kahin bhi nahi tie karta. Yeh step kyun? Koi defined path nahi hone par, Ohm's law ke paas koi fixed nahi hai jo hume de sake — aur node capacitance charge hold karne ke saath, wahan jo bhi voltage land kare woh simply stick karti hai. Node electrically "orphaned" hai.
- Phir voltage kya set karta hai? Stray coupling: nearby switching wires, tumhare haath ki capacitance, 50/60 Hz mains hum. Yeh par tiny charges push karte hain, aur se voltage wander karti hai aur ke beech kahin bhi (lavender curve).
- Read result. Jab bhi wandering voltage buffer ke threshold ko cross kare (≈ , coral line), logic value flip ho jaati hai. Isliye read undefined / random hai — kabhi 0, kabhi 1. (Cell D)
Yeh #1 beginner bug hai ("button pressed reads karta hai jab main touch nahi karta"). Ilaaj hai koi bhi pull resistor, internal ya external.
Recall Degenerate case
Ek floating input read karta hai ::: na ek stable 0 na 1 — yeh undefined hai aur noise pick up karta hai.
Verify (conceptual): koi pull nahi matlab deterministic nahi; result ek single value nahi hai, isliye uspar branch karne wala koi bhi code ek bug hai. (Kuch bhi numeric check karne ko nahi — point yeh hai ki koi number exist nahi karta.)
Example 4 — Cells E, F, G: sizing karna (normal + dono limits)
- Current set up karo (Cell E). Jab pressed, current flow karti hai . Ohm's law se . Yeh step kyun? Pressed state mein yeh ek hi current path hai; ise cap karna wasted power ko cap karta hai.
- solve karo. Hume chahiye , isliye 33 k–47 k choose karo. (Cell E) Yeh step kyun? Hume aur current budget pata hai, lekin unknown hai . Ohm's law mein neeche hai, isliye hum ise algebraically flip karte hain — dono sides ko se multiply karo, se divide karo — taaki mile, woh quantity isolate karo jo hum actually choose kar sakte hain. Aur kyunki bada chota deta hai, current par "" par "" ban jaata hai.
- Limit F — (too small). Yeh tumhare budget ka 150× hai — button held down 15 mA ground par hamesha ke liye dump karta hai. Battery ke liye bura. (Cell F)
- Limit G — (too big). Barely koi waste — lekin itna weak ki pico-amp noise currents pin ko shift kar dete hain: "override" feeble hai, isliye read flaky ho jaata hai. (Cell G)
Verify: (a) ✓. (b) ✓. (c) ✓.
Example 5 — Cell H: fight (voltage divider)
Figure dono resistors stack karta hai: butter-yellow upar se, mint neeche tak, coral "pin (tap)" dot dono ke beech squeeze hua. Coral current arrow single current ko top-to-bottom dikhata hai.

- Ise voltage divider ki tarah dekho. Current chalti hai (pin) . Do resistors series mein ke paas; pin dono ke beech tap hai. Yeh step kyun? Dono paths simultaneously conduct karte hain, isliye pin exactly ya nahi ho sakti — yeh divider point hai. Divider formula batata hai kahan.
- Divider apply karo. Tap voltage supply ka bottom resistor over total ke barabar hai: Yeh form kyun? Voltage resistance ke proportion mein split hoti hai; bottom leg ka share ground ke upar pin ki voltage hai.
- Calculate karo. Yeh step kyun? Humhe actual number chahiye, sirf formula nahi, kyunki poora sawaal hai "kya yeh ke paas hai ya ke paas?" Sirf fraction evaluate karne se (tiny bottom resistor over huge total ⇒ tiny fraction) hum dekhte hain ki pin microscopically ground ke upar land karti hai.
- Conclude karo. → pin LOW reads karti hai. Strong (5 Ω) driver weak (33 kΩ) pull-up ke against overwhelmingly jeetta hai. (Cell H) Yeh step kyun? Ek logic input "LOW" ya "HIGH" decide karta hai ek threshold se compare karke (3.3 V parts ke liye, ≈ se kuch bhi LOW hai guaranteed). Kyunki us threshold se ~2000× neeche hai, zero ambiguity hai — yahi wajah hai ki weak pulls safe hote hain: ek real driver hamesha unhe crush karta hai.
Verify: , safely below any LOW threshold (~ for 3.3 V logic). ✓
Example 6 — Cell I: bounce aur debounce window
Figure bouncing pin voltage (lavender) LOW drop hoti dikhata hai; har coral down-triangle ek raw falling edge mark karta hai, par butter-outlined triangle woh ek accepted press hai, aur mint band woh 20 ms lockout hai jo baaki sab edges ko swallow karta hai.

- Raw edges count karo. Paanch listed edges → 5 ISR calls bina debounce ke. Har spurious call
pressed=truere-run karti hai — app sochta hai button 5 baar hit hua. (Cell I) Yeh step kyun? Hardware har electrical transition par fire karta hai; woh "real press" aur "contact chatter" mein fark nahi kar sakta. - 20 ms lockout apply karo. par edge accept karo; 20 ms window start karo. Edges at 1.5, 3, 4.5, 7 sab andar hain → ignored. Agla accept chahiye; koi nahi hai. Yeh step kyun? Saara bounce kuch ms ke andar hota hai; ek 20 ms window poora burst comfortably swallow karta hai jabki ek insaan ko instant feel karta hai.
- Conclude karo. Debounce ke saath: 1 accepted press. Sahi! ✓
RC-filter aur state-machine variants ke liye dekho Switch Debouncing, aur ISR ko short rakhne ke liye dekho Interrupts & ISR Design taaki woh pehle edge kabhi miss na kare.
Verify: raw count ; edges inside ms (4 suppressed), accepted . ✓
Example 7 — Cell J: real-world current budget (word problem)
- Per-button current. Pressed = pin button ke through grounded, isliye . Yeh step kyun? Har pull-up apna independent path hai jab uska button neeche ho.
- Total. Aath identical paths parallel mein add up hote hain: . (Cell J) Yeh step kyun? Parallel current sources sum hote hain; woh koi resistor share nahi karte.
- Is leak se battery life. Capacity current: din. Yeh step kyun? mAh mA hours mein cancel ho jaata hai — ek direct units check ki humne yeh sahi set up kiya.
Toh even ek "wasteful" all-pressed case ~2.6 mA hai — usually fine, lekin coin cell par tum ko 47 k tak bump karoge taaki ~5× cut ho.
Verify: ✓; ✓; life ✓.
Example 8 — Cell K: exam twist (output pin read karna)
- Push-pull output yaad karo. OUTPUT-HIGH ke roop mein, top transistor ON hai, pin ko se sirf kuch ohms ke through hard-connect karta hai. Dekho Push-Pull vs Open-Drain Outputs. Yeh step kyun? Ek output pin actively drive kar raha hai, sun nahi raha — isliye ek input ki tarah nahi, yeh ek passive sense-buffer nahi hai jo koi level bataye jaane ka wait kar raha ho.
- Button pressed = fight, lekin driver jeetta hai. Button ka path GND tak kuch resistance hai (say ) versus driver ka . Yeh exactly Ex 5 jaisa voltage divider hai, lekin ab strong driver upar baith hua hai ( tak) aur button ka bada resistance neeche hai: Yeh step kyun? HIGH threshold se kaafi upar hai (~ for 3.3 V logic), isliye agar tum wire sense bhi kar sako, phir bhi yeh 1 reads karta hai, woh 0 nahi jo tum press se umeed karte. Strong driver ek mere button ke against neeche khinchne se mana kar deta hai.
- Deeper read-path point. Kai MCUs par, ek push-pull output read karna output register se seedha written value return karta hai, bilkul physical sense nahi — toh
read(pin2)1 return karta hai button se regardless. Dono mechanisms wohi galat answer dete hain: press invisible hai. (Cell K) - Fix. Pin ko sense karne se pehle
INPUT_PULLUPmein set karo. Phir idle = HIGH, pressed = LOW (Ex 1). Bonus wajah: as configured, pressing bhi button ke through ek strong driver seedha GND tak short karta hai — ek fault current jo pin ko damage kar sakta hai.
Verify: divider (HIGH, above ~ threshold) ✓ — press kabhi 0 nahi dikhta.
Recall Which cells did we cover?
A–K, sabhi gyarah — levels (A,B,C), degenerate float (D), sizing + dono limits (E,F,G), divider fight (H), timing/bounce (I), word-problem current budget (J), aur output-read exam trap (K). ::: ✓ har cell mein kam se kam ek worked example hai.
Related deep dives: Polling vs Interrupt-Driven I/O · volatile & Memory-Mapped Registers · Interrupts & ISR Design.