Analog Illusion: LED Fading
The LED Blink and Fade project introduces a vital concept in digital electronics: Pulse Width Modulation (PWM). A microcontroller is purely digital; it can output 5 Volts (ON) or 0 Volts (OFF). It cannot output 2.5 Volts. So how do we make an LED glow at 50% brightness?

The PWM Concept
We use a trick on the human eye. By rapidly turning the pin ON and OFF thousands of times per second, the eye blends the light together.
- If the pin is ON for 50% of the time and OFF for 50%, the LED appears half as bright.
- If it is ON for 10% and OFF for 90%, it appears very dim.
The analogWrite() Function
You can only use PWM on specific pins on your board (marked with a squiggle ~, like ~3, ~5, ~6, ~9, ~10, ~11 on the Uno).
- The function
analogWrite(pin, value)accepts a value between0(totally off) and255(totally on). - The Breathing Loop: You create a
forloop that rapidly increments a variablebrightnessfrom 0 up to 255 with a tiny 5-millisecond delay. The LED smoothly fades from black to blinding brightness! - The Return Loop: A second
forloop counts backward (brightness--) from 255 back to 0, making the LED smoothly dim back down.
Essential Layout
- Arduino Uno/Nano: The PWM generator.
- Any 5mm LED and a 220-ohm Resistor.
- A Breadboard and Jumper Wires.