Fade an LED on an Arduino Uno
An LED on pin 9 breathes up and down with analogWrite: 490 Hz hardware PWM from the Uno’s timer, through a 220 ohm resistor. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- PWM and duty cycle make brightness
- Pin 9 supports hardware PWM
- Lookup tables for smooth curves
- The eye sees brightness on a curve, not a straight line
- Why an LED needs a series resistor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The LED on pin 9 breathes: it fades up smoothly, then down, and repeats. The sketch uses analogWrite, which makes the pin switch on and off 490 times a second. The longer it stays on in each cycle, the brighter the LED looks.
A table named breath holds 64 brightness values from 0 to 250 that follow a smooth curve. Stepping through it every 30 ms, instead of counting up in a straight line, looks natural to the eye.
The 220 ohm resistor limits the current so the LED is safe. Pin 9 is driven by one of the Uno's hardware timers, so the CPU only changes the value and sleeps.
Parts list
4 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED, red pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 9 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Fade: a breathing LED on pin 9, from the timer, not from the CPU.
//
// Pin 9 is OC1A, timer 1's compare output. analogWrite() puts timer 1 into
// 8-bit phase-correct PWM with a 64 prescaler, which is 16 MHz / (64 x 510) =
// 490 Hz, and after that the hardware holds the duty cycle on its own: the
// sketch only changes OCR1A thirty times a second and sleeps in between.
//
// Wire an LED and a 220 ohm resistor from pin 9 to GND.
//
// The table is one cycle of (1 - cos)/2, which is what makes the light look
// like it is breathing rather than ramping: the eye reads brightness roughly
// logarithmically, so a linear ramp looks like it rushes the top end.
const int LED_PIN = 9;
const int STEPS = 64;
const int MS_PER_STEP = 30;
const uint8_t breath[STEPS] = {
0, 1, 2, 5, 10, 15, 21, 28,
37, 46, 56, 66, 77, 89, 101, 113,
125, 137, 149, 161, 173, 184, 194, 204,
213, 222, 229, 235, 240, 245, 248, 249,
250, 249, 248, 245, 240, 235, 229, 222,
213, 204, 194, 184, 173, 161, 149, 137,
125, 113, 101, 89, 77, 66, 56, 46,
37, 28, 21, 15, 10, 5, 2, 1,
};
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (int step = 0; step < STEPS; step++) {
analogWrite(LED_PIN, breath[step]);
delay(MS_PER_STEP);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes LED_PIN (pin 9) an output. That is all it needs, because analogWrite sets up the timer for PWM by itself.
void setup() {
pinMode(LED_PIN, OUTPUT);
}loop sketch.ino
Steps through the 64 values of breath. Each one goes to analogWrite, then delay(MS_PER_STEP) waits 30 ms. The table covers one full up and down cycle, so when the loop restarts the fade begins again.
void loop() {
for (int step = 0; step < STEPS; step++) {
analogWrite(LED_PIN, breath[step]);
delay(MS_PER_STEP);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED does not light This project
LEDs have a direction. The long leg (anode) goes toward the resistor and pin 9. - It blinks instead of fading This project
Make sure the LED is on pin 9. Only certain pins have PWM. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.