Elsia All Lessons

Embedded Systems

Arduino: GPIO, ADC and PWM

A microcontroller’s pins are electric fingers you manage in software: pinMode sets the finger’s direction (will it push or will it feel?), digitalWrite/digitalRead sets its state. The Uno runs 5 V logic; one pin can push at most ~20 mA — always put a series resistor on an LED.

analogRead converts a continuous voltage into a number: the 10-bit ADC divides 0–5 V into 1024 steps (the staircase alongside). At 2.5 V you read ≈ 512; V = value·5/1023. A potentiometer’s wiper is the classic analog source.

analogWrite does NOT produce true analog: it outputs ~490 Hz PWM; the LED lets your eye — and the motor its inertia — feel the average. delay(), meanwhile, halts the processor entirely — to do several things at once you switch to the pattern of comparing timestamps with millis().

Formulas

V = adc · 5 / 1023
R_LED = (5 − V_f) / I_LED

⚡ Open this lesson in the simulator — free

Test Yourself

If the ADC reads 512, what is the approximate pin voltage (5 V reference, 10-bit)?
Answer: 2.5 V — V = 512·5/1023 ≈ 2.5 V.
From a 5 V output, what series resistor does a red LED (Vf≈2 V) need at 10 mA?
Answer: 300 Ω — R = (5−2)/0.01 = 300 Ω.