Leonardo fade
Two LEDs off two different kinds of timer: the ten-bit one on pin 13 and an ordinary 16-bit one on pin 9. The whole build, an Arduino Leonardo and 4 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 $30. On Mokxi they're free.
What you'll learn
- Hardware PWM and timers
- Fading an LED with
analogWrite - Reversing a ramp at each end
- The Leonardo's pin 13 has its own timer
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Leonardo
- Parts: 6
How it works
Two LEDs fade in opposite directions. The red one is on pin 13 and the blue one is on pin 9, each through a 220 ohm resistor to ground. When red is at its brightest, blue is off, and the other way around.
Both use hardware PWM, but from two different timers on the Leonardo's ATmega32U4 chip. Pin 13 comes from timer 4, a ten-bit high-speed timer. Pin 9 comes from timer 1. Both run at 490.196 Hz. analogWrite hides the difference, so the sketch looks like an ordinary fade.
Parts list
6 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Leonardo
- 1 × Full-size breadboard
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, blueIn Starter Kit, Halloween Prop Kit, Logic Lab 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 Leonardo pin GND |
| LED, blue pin C | Arduino Leonardo pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Leonardo pin 13 |
| Resistor, 220 Ω (2) pin 1 | Arduino Leonardo pin 9 |
| LED, red pin A | Resistor, 220 Ω (1) pin 2 |
| LED, blue pin A | Resistor, 220 Ω (2) 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: hardware PWM on pin 13, which on this board is timer 4.
//
// Timer 4 is the 32U4's ten-bit high-speed timer and it is unlike anything on
// an Uno: its TOP is a register (OCR4C) rather than a mode, its channels are
// A, B and D with no C, and each channel needs a PWM4x enable of its own on
// top of the COM bits. analogWrite hides all of that (the sketch is the
// ordinary one), but the waveform on pin 13 is coming out of a different kind
// of timer from the one that drives pin 9.
//
// Pin 9 gets the same ramp the other way round, off timer 1. Both run at
// 490.196 Hz, as on a real Leonardo: timer 4 in phase and frequency correct
// mode, timer 1 in phase correct.
int level = 0;
int step = 5;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
analogWrite(LED_BUILTIN, level);
analogWrite(9, 255 - level);
level += step;
if (level <= 0 || level >= 255) {
step = -step;
level += step;
}
delay(12);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets LED_BUILTIN (pin 13) and pin 9 as outputs. The timers are set up by analogWrite the first time it is called.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(9, OUTPUT);
}loop sketch.ino
Writes level to pin 13 and 255 - level to pin 9, so the two LEDs are opposites. Then it adds step (5) to level. When the level reaches 0 or 255, step flips sign and the fade turns around. A 12 ms delay sets the speed.
void loop() {
analogWrite(LED_BUILTIN, level);
analogWrite(9, 255 - level);
level += step;
if (level <= 0 || level >= 255) {
step = -step;
level += step;
}
delay(12);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- An LED does not fade This project
Use PWM pins. Here the LEDs are on pins 13 and 9, each through its own 220 ohm resistor. - Both LEDs fade together This project
They should be opposite. Check that one getsleveland the other255 - level. - 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. - Serial output does not show, or the sketch waits forever. On the Leonardo, Serial is the USB port, and pins 0 and 1 are Serial1. Arduino Leonardo
Use Serial for the monitor, and wait with while (!Serial) if you need the first lines. Use Serial1 for a device on pins 0 and 1. - Uploads fail or the wrong board is chosen. The 32U4 resets its USB port during upload, so the port number changes. Arduino Leonardo
Select Arduino Leonardo as the board. Double-tap reset to enter the bootloader if the port is missing. Keep a pin to about 20 mA, and use 5 V logic.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.