Sub topic 7
Fading an LED Up and Down
Prerequisites and the basic idea
This chapter assumes that you know the Arduino setup() and loop() functions, can use pinMode() and digitalWrite(), and understand basic breadboard wiring and Ohm’s law.
A digital pin has only two normal output states: LOW and HIGH. It cannot directly produce every voltage between them. An Arduino Uno creates an analog-like brightness control with pulse-width modulation (PWM). PWM switches the pin between LOW and HIGH very quickly.
The percentage of each cycle spent HIGH is the duty cycle. A 0% duty cycle keeps the output LOW. A 100% duty cycle keeps it HIGH. Values between them make an LED appear dimmer or brighter.
For an LED, connect the anode through a current-limiting resistor to PWM pin 9. Connect the cathode to GND. The resistor limits current and protects the LED and Arduino pin.
Changing brightness with `analogWrite()`
On an Arduino Uno, analogWrite() accepts values from 0 to 255 on a PWM-capable pin.
analogWrite(pin, 0)produces 0% duty cycle.analogWrite(pin, 128)produces about 50% duty cycle.analogWrite(pin, 255)produces 100% duty cycle.
The value is an 8-bit level. It has 256 possible settings, counting zero.
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, 64);
delay(1000);
analogWrite(LED_PIN, 192);
delay(1000);
}The LED does not receive a steady 25% or 75% voltage. It receives fast pulses. The LED responds quickly, while your eyes combine the pulses into an apparent brightness.
Building a fade loop
A fade changes the PWM value by a small amount after each short delay. The variable brightness stores the current level. The variable step stores both the size and direction of each change.
When the value reaches an endpoint, reverse step. A positive step increases brightness. A negative step decreases it.
const uint8_t LED_PIN = 9;
int brightness = 0;
int step = 5;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, brightness);
// Reverse direction before leaving the 0..255 range.
if (brightness + step < 0 || brightness + step > 255) {
step = -step;
}
brightness += step;
delay(20);
}The loop writes the current brightness, checks whether the next value would exceed an endpoint, and then updates the value. The check happens before the update, so brightness remains in the safe range from 0 through 255.
With a step of 5 and a delay of 20 milliseconds, each half of the fade takes about 1 second:
255 / 5 × 20 ms = 1020 ms
The exact time can vary slightly because the sketch also spends time executing instructions.
Common errors
A fade may fail or look wrong for several reasons:
- The LED is connected to a pin that does not support PWM. On an Uno, use pins marked with a tilde: 3, 5, 6, 9, 10, or 11.
- The LED is reversed. The longer leg is usually the anode, but check the component markings.
- The resistor is missing. Always include a current-limiting resistor in series with the LED.
- The brightness value leaves the range 0 to 255. That can cause unexpected results, so reverse direction before updating it.
- The delay is too long. A delay of 200 milliseconds makes visible jumps. A shorter delay and smaller step create a smoother fade.
- The code uses
analogWrite()on a non-PWM pin. On such a pin, the result may act like an ordinary LOW or HIGH output rather than a variable brightness level.
Exercise
What happens if step is changed from 5 to 1 while delay(20) remains unchanged?
Show answer
The fade becomes smoother because each brightness change is smaller, but each half of the fade takes about five times longer.