5.5.2 · HinglishEmbedded Systems & Real-Time Software

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

2,281 words10 min readRead in English

5.5.2 · Coding › Embedded Systems & Real-Time Software


GPIO KYA hai?

Har physical pin internal registers se connect hoti hai. Teen registers jo aap hamesha use karte ho:

Register YE kya control karta hai Typical naam
Direction input ya output DDRx, MODER, TRIS
Output data woh HIGH/LOW jo aap drive karte ho PORTx, ODR
Input data woh level jo pin read karti hai PINx, IDR

Ek output pin kaise kaam karta hai (push-pull)

Chip ke andar har output pin ke paas do transistors hote hain jo aur ke beech stack kiye hote hain:

  • HIGH drive karo → upar wala transistor ON → pin se connected.
  • LOW drive karo → neeche wala transistor ON → pin se connected.

Ise push-pull kehte hain kyunki yeh actively current push bhi kar sakta hai (source) aur current pull bhi kar sakta hai (sink).


Ek input pin kaise kaam karta hai + floating problem

Fix: ek weak pull resistor jo pin ko ek known level se gently tie karta hai jab koi aur use drive nahi kar raha.

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

Resistor ki value kyun matter karti hai (current derive karo)

Socho ek button pin aur ke beech hai, aur internal pull-up hai tak.

  • Button open: se koi current nahi bahti (input buffer ~0 draw karta hai). Ohm's law se pin dekhti hai → HIGH read karta hai. ✓
  • Button pressed: pin button ke through GND se short ho jaati hai. Current bahti hai :


Active-LOW buttons (classic wiring)


Pin change par interrupts kaise kaam karte hain

Pin change jaanne ke do tarike:

  1. Polling — loop forever karte hue pin read karo. Simple hai lekin CPU waste karta hai aur fast pulses miss kar sakta hai.
  2. Interrupt — hardware pin watch karta hai aur ek chosen event par, CPU ko ek special function (ISR, Interrupt Service Routine) mein kheench leta hai.

Switch bounce (hidden bug)


Common mistakes


Recall Feynman: 12 saal ke bachche ko samjhao

Socho ek doorbell wire tumhare brain tak jaati hai. Agar tum wire se electricity push karo bahar, yeh output hai — tum ek light ko on hone ke liye bol rahe ho. Agar tum bas feel karo ki wire hot hai ya cold, yeh input hai — tum ek button ke liye sun rahe ho. Lekin kuch connected nahi hone wali wire hawa mein jhule ki tarah hai: randomly hilti rehti hai. To tum ise ek soft rubber band se baandhte ho — chhat se (pull-up = normally "on") ya floor se (pull-down = normally "off") — taaki woh still rahe jab tak koi real push na kare. Ek interrupt tumhare shoulder par ek tap hai: "kya koi aa gaya?" hazaar baar per second poochne ki jagah, wire tumhe usi pal tap karti hai jab change hota hai, taaki baaki kaam kar sako. Ek catch: ek real button press hone par kaanpta hai (bounces), to pehle tap ke baad thodi der ke liye taps ignore karo.


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

GPIO ka full form kya hai aur "general purpose" kyun?
General Purpose Input/Output; pin kisi fixed peripheral ke liye reserved nahi hai, software uska direction/behaviour set karta hai.
Ek floating input pin random reads kyun deta hai?
Yeh tiny capacitance ke saath high-impedance hai, to ambient noise pick up karta hai; kuch uska voltage define nahi karta.
Pull-up vs pull-down: dono ke default read level kya hain?
Pull-up → idle HIGH (1); pull-down → idle LOW (0).
Pull resistor "weak" (large) kyun hona chahiye?
Taaki koi bhi real driver (low resistance) voltage divider ke zariye ise aasaani se override kar sake, ek clean signal deta hai.
Pull-up se current waste hone ka formula jab input grounded ho?
.
Classic button "active-LOW" kyun hai?
Pull-up idle HIGH rakhta hai; press karne par pin GND se connect hoti hai → LOW read karta hai, to pressed = 0.
Push-pull vs open-drain output?
Push-pull HIGH aur LOW dono drive kar sakta hai; open-drain sirf LOW pull karta hai ya Hi-Z hota hai aur external pull-up chahiye.
Edge vs level interrupt trigger — button ke liye kaunsa aur kyun?
Edge (active-LOW ke liye falling): ek ISR per press; level-trigger continuously re-fire karta jab tak button hold karo.
ISR-shared flag volatile kyun hona chahiye?
Memory se har baar dobara read force karta hai; warna compiler ise register mein cache kar sakta hai aur kabhi ISR ka update nahi dekhta.
ISRs short kyun rakhein?
Doosre interrupts block ho sakte hain; lambi ISRs missed events aur timing jitter cause karti hain — bas ek flag set karo aur main loop mein handle karo.
Switch bounce kya hai aur fix kaise karein?
Mechanical contacts ~1–10 ms tak edges ka burst produce karte hain; fix: debouncing (pehle edge ke baad ~20 ms tak agle edges ignore karo) ya RC filtering.
3.3 V par 33 kΩ pull-up press hone par kitna current waste karta hai?
.

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