Fade an LED

Featured Video Play Icon

Lets expand the repertoire of output that we can use by looking at the function analogWrite(). I experienced much confusion with analogWrite(), because I suspected that it had to do with the analog pins on the Arduino. The function, however, has nothing to do with the analog pins. There are 5 pins on most Arduino boards marked with an ‘~’ next to the pin number – these pins can be invoked to rapidly change the power being applied at the pin – this is a technique called pulse width modulation (PWM).

You Will Need

LED – any color is fine
220 Ohm Resistor
Alligator Clip
Glacial ice cubes
Step-by-Step Instructions

Take the short leg of your LED and stick it in the GND pin.
Take either leg of the resistor and place it in pin 9.
Connect the long leg of the LED with the other leg of the resistor using an alligator clip
Plug the Ardunio into your computer with the USB cable
Open up the Arduino IDE
Go to File, Examples, 01.Basics, Fade
Click the Verify button (Top Left). The button will turn orange and then blue once finished.
Click the Upload button. The button will turn orange and then blue when finished.
Watch in mesmerizing amazement as the LED fades in and out.

The sketch starts with the usual multiline comment describing the sketch and how to set up the circuit. The first block of code we encounter is the declaration and initialization of 3 int variables…

int led = 9;           // the pin that the LED is attached to

int brightness = 0;    // how bright the LED is

int fadeAmount = 5;    // how many points to fade the LED by

The variable names and comments are both descriptive and helpful – remember this when naming and commenting your own code – it is a pillar of success! The “brightness” variable will store the value of the current brightness of the LED. “fadeAmount” is the rate at which the LED will fade and brighten. And of course, as the comments explain, “led” is simply the pin number where we have attached our LED (through a 220 Ohm resistor).

Now that we have declared and initialized our variables, we move on to setting up the board with the setup() function…

void setup()  {

// declare pin 9 to be an output:

pinMode(led, OUTPUT);


The only thing we do here is set the mode of pin 9 as an OUTPUT using the pinMode() function. Recall that pinMode() takes two arguments – the pin number and the mode. In this case we assign the pin number using our variable “led”, which we previously initialized as the number 9. By now you know that setup() only runs once – the code inside the setup() curly bracket will only be executed a single time by the Arduino. Where the real action happens is in loop().

The first function we encounter is analogWrite(). This function invokes the Pulse Width Modulation capabilities of our Arduino board. Pulse Width Modulation basically adjusts the power output at the pin. So you can have a lot of power or a little power applied at the pin, its your call, you just tell the analogWrite() function which pin to modulate and how much power you want applied. The scale is from 0 to 255 with zero being the lowest power setting and 255 being the highest. For a discussion of what is actually happening with pulse width modulation check out the further reading section.

As alluded to above, analogWrite() takes two arguments..

analogWrite(pin, value)

You can utilize pins 3, 5, 6, 10 and 11 with analogWrite() on most Arduino boards (there is a “PWM” or ” ~” next to the pin number on the board). The value range is from 0 to 255.

In this sketch we use the arguments:

analogWrite(led, brightness);

So the first thing we do in the program is we write a value to pin 9 (recall that “led” holds the number 9) where we have our LED attached (through a resistor) – and we set the value to 0. This will keep our LED dark to start with.

Leave a Reply

Your email address will not be published. Required fields are marked *