Introduction: Arduino: LED Fading

This beginner tutorial demonstrates how to vary the brightness of an LED using Pulse Width Modulation (PWM), a technique used to create pseudo-analog signals from digital signals in an Arduino digital output pin. If you don't already have a basic understanding of PWM, you can learn about it here:

Step 1: The LED Circuit

The circuit used for this instructable is shown above. In order for the LED to draw an optimal current of approximately 20 milliamps from the 5V Arduino power source (labeled "Vs"), a 200-Ohm resistor (labeled "R1") must be connected to the Arduino and to the LED. Using Ohm’s Law, V = IR, we can see that the LED actually draws 23 milliamps from the Arduino and stays well within the output pin's 40 milliamp limit. A brief overview of Ohm's law can be found here:

Step 2: Select the Right Output Pin

Select a digital output pin marked with a tilde as your power source (pin 11 is used in this instructable). Tildes designate Arduino pins capable of PWM. By varying the power supplied by the PWM output, we’ll be able to adjust the brightness of the LED.

Step 3: Supply Power to a Breadboard

Connect power from the selected digital output pin as well as ground to a breadboard.

Step 4: Connect a Resistor

3. Connect one lead of the 200-Ohm resistor to the power source. Insert the remaining lead into any node electrically isolated from the hot or neutral wire.

Step 5: Connect an LED

Connect the longer, positive lead of an LED in series with the resistor by inserting it into the same node. Insert the remaining lead into any node electrically isolated from the rest of the circuit.

Step 6: Complete the Circuit

Connect the LED anode to ground to complete the circuit.

Step 7: Download the Arduino Software

If you do not already have the Arduino IDE downloaded and installed, you can find it using the following link:

Open the Arduino IDE once it is installed.

Step 8: Code Your Arduino

This program varies the brightness of an LED using Pulse Width Modulation. The LED is off when the program begins, but will increase in brightness as the amplitude of the PWM signal increases after each loop iteration. Once the signal reaches either extreme (fully bright or OFF), the direction of brightness changes, creating a gradual cycle from OFF to fully bright and vice versa. After each loop iteration, the Arduino is delayed to allow for adequate time to detect the subtle change brightness after each voltage step.

Copy the following code into a blank sketch:

Arduino: LED Fading sample code


Author: Billy Anderson


int ledPin = 11; //pin associated with LED

int brightness = 0; // initial brightness

int deltaFade = 5; // change in brightness

void setup() {

pinMode(ledPin, OUTPUT); // initialize pin as output


void loop() {

analogWrite(ledPin, brightness); // set initial LED brightness

brightness += deltaFade; // change brightness for next loop execution

// reverse fading direction at either end of fade

if (brightness == 0 || brightness == 255) {

deltaFade = -deltaFade; }

delay(50); // delay for 50ms to see dimming effect


Step 9: Upload the Program

Connect your Arduino to a computer via USB, then select the right-pointing arrow in the upper-left corner of the sketch window (circled in red) to upload the sketch and watch your LED fade!