Arduino - Fading LED
Arduino - Fading an LED with PWM
Section titled “Arduino - Fading an LED with PWM”Beyond simply turning an LED on and off, Arduino can control its brightness. This example demonstrates how to create a smooth fading effect using the analogWrite() function and a technique called Pulse Width Modulation (PWM). This is a fundamental skill for creating more dynamic and expressive outputs.
Core Concept: Pulse Width Modulation (PWM)
Section titled “Core Concept: Pulse Width Modulation (PWM)”A microcontroller’s digital pins are binary; they can only be fully ON or fully OFF. So how do we create intermediate brightness levels? The answer is PWM. Imagine flicking a light switch on and off extremely fast. If you leave it on for half the time and off for the other half, the light will appear to be at 50% brightness. This is the essence of PWM.
The analogWrite(pin, value) function does this for you. The value can be any integer from 0 (always off) to 255 (always on). A value of 127 would correspond to a 50% duty cycle (on half the time), making the LED appear at half brightness.
Important: Not all digital pins on an Arduino support PWM. On most boards like the Uno, PWM-capable pins are marked with a tilde symbol (~), for example, ~3, ~5, ~6, ~9, ~10, and ~11.
Components Required
Section titled “Components Required”- 1 × Breadboard
- 1 × Arduino Uno R3
- 1 × LED (any color)
- 1 × 220Ω or 330Ω Resistor
- 2 × Jumper Wires
Circuit and Wiring
Section titled “Circuit and Wiring”The circuit is nearly identical to a basic ‘Blink’ sketch, but the LED must be connected to a PWM pin.
- Connect the long leg (the anode, positive) of the LED to one end of the resistor.
- Connect the other end of the resistor to a PWM-capable pin on the Arduino, for example, pin ~9.
- Connect the short leg (the cathode, negative) of the LED to the GND pin on the Arduino.
The resistor is crucial to limit the current flowing through the LED and prevent it from burning out.
Arduino Sketch
Section titled “Arduino Sketch”Open a new sketch in the Arduino IDE and enter the following code. This version uses two for loops, which makes the logic of fading up and fading down very clear.
/** * @file led_fade_clean.ino * @brief Demonstrates fading an LED up and down using PWM with clear for-loops. */
// --- Constants ---// Use a PWM-capable pin (marked with ~ on the board)const int LED_PIN = 9;
// A small delay to control the speed of the fadeconst int FADE_DELAY = 10; // in milliseconds
void setup() { // Declare the LED pin as an output. This is required for analogWrite() as well. pinMode(LED_PIN, OUTPUT);}
void loop() { // --- Fade Up --- // Loop from 0 to 255 (min to max brightness) for (int brightness = 0; brightness <= 255; brightness++) { analogWrite(LED_PIN, brightness); delay(FADE_DELAY); }
// --- Fade Down --- // Loop from 255 down to 0 (max to min brightness) for (int brightness = 255; brightness >= 0; brightness--) { analogWrite(LED_PIN, brightness); delay(FADE_DELAY); }}Code Explanation
Section titled “Code Explanation”const int LED_PIN = 9;: We define our LED pin as a constant to make the code clean and easy to modify.pinMode(LED_PIN, OUTPUT);: Even though we’re usinganalogWrite(), we still need to configure the pin as anOUTPUTin thesetup()function.- Fade Up
forloop: This loop starts thebrightnessvariable at 0, and on each iteration, it increases it by one (brightness++). It sets the LED’s brightness to this new value and then pauses briefly. It continues untilbrightnessreaches 255. - Fade Down
forloop: This loop does the reverse, starting at 255 and decreasing the brightness on each iteration (brightness--) until it reaches 0. delay(FADE_DELAY);: This small delay is what makes the fade visible to the human eye. Try changing its value! A smaller delay will make the fade faster, while a larger one will slow it down.
Debugging and Common Errors
Section titled “Debugging and Common Errors”- LED is on but doesn’t fade: You have likely connected the LED to a non-PWM pin. Make sure you are using a pin with a
~symbol next to it and that this pin number matches theLED_PINconstant in your code. - LED doesn’t turn on at all: Check the LED’s polarity. The long leg must be connected to the Arduino pin (via the resistor) and the short leg to GND. Also, ensure the resistor and wires are firmly seated in the breadboard.
- LED is very dim: You might be using a resistor with too high a value (e.g., 10kΩ). A value between 220Ω and 330Ω is ideal for most standard LEDs with a 5V Arduino.
Next Steps: Beyond a Single LED
Section titled “Next Steps: Beyond a Single LED”Now that you’ve mastered fading, try a more advanced project:
Mini-Project: RGB Mood Light. Get an RGB LED (which is like three LEDs—Red, Green, and Blue—in one package). Connect each of its three color pins to a separate PWM pin on your Arduino. Write a sketch that fades between different colors by varying the brightness of the red, green, and blue components independently. This is the basis for millions of colors and effects!