Electronics for Building Things · Lesson 14 · The Microcontroller

Digital, Analog & PWM Signals

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 one win For any task — switch a relay, read a sensor, dim a light, set motor speed — you can name which of the three signal types you need.

1. Digital — on or off

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).

2. Analog input — reading a level

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.

3. PWM — "fake analog" output

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:

25% 50% 75% dim / slow half bright / fast
Same fast on/off cycle, different "on" fractions. More on-time → higher average → brighter LED or faster motor.

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.

Where you'll use PWM Dimming LEDs, setting motor speed (Lesson 16), and generating the control pulse for a servo (Lesson 17). It's the bridge from "on/off" to "how much."

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.)

Check yourself

Read this next (primary source) SparkFun — Pulse Width Modulation. Clear visuals of duty cycle and where PWM shows up.
I'm your teacher — ask me anything. If "PWM isn't really analog" still feels slippery, ask me to walk through it with your specific example (a fading light, a motor).

See also: Glossary · Signals & buses · Rules of thumb