Electronics for Building Things · Lesson 14 · The Microcontroller
The three ways a pin talks to the world — and the clever trick called PWM.
A pin only really knows two states: on and off. Yet you can read a dimmer knob and smoothly fade an LED. This lesson explains the three signal types that make that possible — and clears up the most common beginner confusion: how a digital pin "fakes" analog.
The basic mode. A digital output drives a pin HIGH or LOW (digitalWrite);
a digital input reads whether a pin is HIGH or LOW (digitalRead). Perfect
for on/off things: lighting an LED, switching a relay, reading a button (Lesson 5).
The real world isn't on/off — light, temperature, and knob position vary smoothly. An
analog-to-digital converter (ADC) measures a pin's voltage and hands your code
a number (analogRead): 0–1023 on an Uno, 0–4095 on an ESP32. You met this with the
potentiometer.
Here's the trick that confuses everyone at first. Most pins can't output a true in-between voltage. Instead they use PWM (Pulse-Width Modulation): switch the pin on and off very fast, and vary what fraction of the time it's on. The average comes out like a smaller voltage (SparkFun: PWM).
That fraction is the duty cycle:
In code, analogWrite(pin, 0–255) sets the duty cycle: 0 = always off, 127 ≈ half, 255 =
always on. The switching is far too fast to see flicker.
Note: "analog output" via PWM isn't a steady voltage — it's a fast square wave whose average acts like one. (A few chips, including the ESP32, have one or two true analog-out "DAC" pins, but PWM is what you'll use almost always.)
See also: Glossary · Signals & buses · Rules of thumb