Skip to main content

Fade an LED Using PWM - Beginner Tutorial

Learn how to gradually increase and decrease an LED's brightness using an Arduino Uno R3 and Pulse Width Modulation (PWM).

In our previous project, we learned how to turn an LED ON and OFF using an Arduino digital output.

Now let's take the next step.

Instead of simply switching the LED between ON and OFF, we will make it gradually increase and decrease in brightness. To accomplish this, we will use a technique called Pulse Width Modulation (PWM).

This project introduces an important Arduino concept that will be useful in many future projects involving LEDs, motors, and other electronic components.

What You Will Learn

After completing this project, you will understand:
  • What Pulse Width Modulation (PWM) is.
  • Why a PWM-capable pin is required for this project.
  • How analogWrite() controls PWM output.
  • How PWM values from 0 to 255 affect the output.
  • How variables can control LED brightness.
  • How an if statement can reverse the direction of the fade.
  • How to create a continuous fade-in and fade-out effect.

Supplies

The hardware components are the same as those used in the previous project.

You will need:
  • Arduino Uno R3
  • 1 × LED
  • 1 × 220 Ω resistor
  • Solderless breadboard
  • Jumper wires
  • USB cable
  • Computer with the Arduino IDE
The main difference in this project is that the LED must be connected to a PWM-capable pin.

Setting Up Your Hardware

What Is a PWM Pin?

PWM stands for Pulse Width Modulation.

On the Arduino Uno R3, PWM-capable digital pins are marked with a ~ symbol.

The PWM pins are:
  • D3
  • D5
  • D6
  • D9
  • D10
  • D11
For this project, we will use D9.

Arduino's current documentation identifies pins 3, 5, 6, 9, 10, and 11 as PWM-capable pins on the Arduino Uno R3.

Why do we need a PWM pin?

In the previous Blink project, we used a digital output to switch the LED between two states:

ON or OFF.

For this project, we want to control the LED's apparent brightness.
A PWM-capable pin allows the Arduino to rapidly switch the output between HIGH and LOW while controlling the duty cycle of the signal. This allows the LED to appear brighter or dimmer.

Connections

Connect the LED as follows:
  • Arduino D9 (~) → 220 Ω resistor
  • 220 Ω resistor → LED Anode (+)
  • LED Cathode (-) → Arduino GND


Therefore:

D9 → 220 Ω resistor → LED anode → LED cathode → GND

Make sure the LED is connected with the correct polarity.

For a typical LED:
  • Longer leg → Anode (+)
  • Shorter leg → Cathode (-)
  • Flat edge of the LED body → Usually indicates the cathode
The 220 Ω resistor is used to limit the current flowing through the LED.

Understanding PWM

Before writing the program, let's understand how PWM allows us to control the LED's brightness.

A normal digital output has two states:
  • HIGH → ON 
  • LOW → OFF
PWM rapidly switches between these two states.

The important factor is the duty cycle, which represents the proportion of each PWM cycle during which the signal is HIGH.

For example:

0% duty cycle 
LOW LOW LOW LOW LOW 
LED → OFF

50% duty cycle 
HIGH HIGH LOW LOW HIGH 
LED → Approximately medium brightness

100% duty cycle 
HIGH HIGH HIGH HIGH HIGH 
LED → Maximum output


The Arduino Uno uses an 8-bit PWM value ranging from 0 to 255 with analogWrite().

Coding

You can find Arduino's built-in Fade example through:

File → Examples → 01.Basics → Fade

The built-in example is designed to demonstrate LED fading using PWM. Arduino includes the Fade example among its basic built-in examples.

Because our external LED is connected to D9, the example needs to be configured for our hardware.

Complete Code

const int ledPin = 9; 
int brightness = 0; 
int fadeAmount = 5; 

void setup() { 
pinMode(ledPin, OUTPUT); 
} 

void loop() { 
analogWrite(ledPin, brightness); 
brightness = brightness + fadeAmount; 
if (brightness <= 0 || brightness >= 255) { 
fadeAmount = -fadeAmount; 
} 
delay(30); 
}


You can also keep your original code screenshot below the code block as a visual reference.

Understanding the Code

Let's examine the program step by step.

1. Defining the LED Pin

const int ledPin = 9;

This tells the Arduino that our LED is connected to digital pin 9.
Using the name ledPin instead of repeatedly writing 9 makes the program easier to understand and modify.

2. Creating the Brightness Variable

int brightness = 0;

The brightness variable stores the current PWM value.

It begins at:

brightness = 0

A value of 0 means that the LED starts OFF.

3. Creating the Fade Amount

int fadeAmount = 5;

This variable determines how much the brightness changes during each loop.

Initially, the value is positive:

fadeAmount = 5

Therefore, the brightness increases:

0 → 5 → 10 → 15 → 20 → ...

When the LED reaches its maximum brightness, the program changes fadeAmount to a negative value.

The brightness then decreases:

255 → 250 → 245 → 240 → ...

4. Configuring the Pin

void setup() { 
pinMode(ledPin, OUTPUT); 
}

The setup() function runs once when the Arduino starts or resets.
pinMode() configures the LED pin as an output.

5. Setting the Brightness

analogWrite(ledPin, brightness);

The analogWrite() function generates the PWM output.
The current value stored in brightness determines the PWM duty cycle.

For example:

analogWrite(ledPin, 0);

turns the LED OFF.

While:

analogWrite(ledPin, 255);

sets the PWM output to its maximum value.

6. Increasing or Decreasing the Brightness

brightness = brightness + fadeAmount;

This changes the brightness value each time the loop() runs.

When fadeAmount is 5:
0 5 10 15 20 ... 255

When fadeAmount becomes -5:
255 250 245 240 ... 0

7. Reversing the Direction

The following code checks whether the brightness has reached either end of its range:

if (brightness <= 0 || brightness >= 255) { 
fadeAmount = -fadeAmount; 
}

The || operator means OR.

Therefore, the condition becomes true when:

brightness is less than or equal to 0, or
brightness is greater than or equal to 255.

When either boundary is reached, the sign of fadeAmount is reversed.

For example:

fadeAmount = 5

becomes:

fadeAmount = -5

This causes the LED to fade in the opposite direction.

8. Controlling the Fade Speed

delay(30);

The Arduino waits approximately 30 milliseconds between brightness updates.
Changing this value changes the apparent speed of the fade.
A smaller delay generally produces faster updates, while a larger delay produces slower updates.

How the Program Works

The complete process is:
Brightness = 0 
↓ 
Increase brightness 
↓ 
LED becomes brighter 
↓ 
Brightness reaches 255 
↓ 
Reverse direction 
↓ 
Decrease brightness 
↓ 
LED becomes dimmer 
↓ 
Brightness reaches 0 
↓ 
Reverse direction 
↓ 
Repeat

The final effect is:

OFF → gradually brighter → maximum brightness → gradually dimmer → OFF → repeat

Uploading the Program

Once you have entered the code into the Arduino IDE:
  1. Connect the Arduino Uno R3 to your computer.
  2. Select the correct Arduino board.
  3. Select the correct port.
  4. Click Verify to compile the sketch.
  5. If there are no errors, click Upload.
  6. Wait for the upload to complete.
  7. Observe the LED connected to D9.
Make sure the Arduino is connected to your computer before uploading the sketch.

Final Result

After the program has been uploaded successfully, the LED should:

Fade IN → reach maximum brightness → Fade OUT → reach minimum brightness → repeat continuously.

Unlike the previous Blink project, the LED should not simply switch between ON and OFF. Its brightness should change gradually.

Experiment 1: Change the Fade Amount

Try changing:

int fadeAmount = 5;

to:

int fadeAmount = 1;

The brightness will change in smaller steps.

Now try:

int fadeAmount = 10;

The brightness will change more quickly.
Experiment with different values and observe how they affect the fading effect.

Experiment 2: Change the Fade Speed

Try changing:

delay(30);

to:

delay(10);

Then try:

delay(100);

Observe how the different delay values affect the speed of the fade.

Challenge

Can you find a combination of fadeAmount and delay() that produces the smoothest-looking fade?

Troubleshooting

The LED does not fade

Check that:
  • The LED is connected to D9.
  • D9 is a PWM-capable pin.
  • The LED has the correct polarity.
  • The resistor is connected in series with the LED.
  • The cathode is connected to GND.
  • The correct board is selected in the Arduino IDE.
  • The correct port is selected.
  • The sketch was successfully uploaded.

The LED only turns ON and OFF

Make sure your program uses:

analogWrite(ledPin, brightness);

rather than:

digitalWrite(ledPin, brightness);

Also verify that the LED is connected to a PWM-capable pin.

The LED does not turn on at all

Check the LED's polarity and make sure the circuit is connected correctly.

Remember:

Anode → resistor → D9

Cathode → GND

What You Learned

In this project, you learned how to control an LED's apparent brightness using PWM.

You learned:
  • What PWM means.
  • Why PWM-capable pins are required.
  • How analogWrite() works.
  • How PWM values from 0 to 255 affect the output.
  • How variables can store brightness values.
  • How an if statement can detect boundary conditions.
  • How changing the sign of a variable can reverse the direction of a process.
  • How delay() affects the speed of the fading effect.
These concepts will be useful in many future Arduino and electronics projects.

Going Further

The fading LED project introduces one of the most useful concepts in Arduino programming: PWM.

PWM can be used for more than LED brightness. Depending on the hardware and application, it can also be used for tasks such as controlling certain motor speeds.

Arduino also provides a Blink Without Delay example that demonstrates a different approach to timing without blocking the program with delay().

As your projects become more advanced, learning these techniques will allow your Arduino to perform multiple tasks more efficiently.

Conclusion

In our previous project, we learned how to turn an LED ON and OFF.

In this project, we took the next step by learning how to gradually control the LED's brightness using PWM.

The key concept is that the Arduino rapidly switches the digital output between HIGH and LOW while changing the PWM duty cycle. This creates the appearance of different brightness levels.

You have now learned another important building block for Arduino programming.

This is only the beginning of your Arduino journey!

Comments

Popular posts from this blog

Arduino Serial Communication Beginner Guide (UART Explained)

Serial communication is the backbone of most Arduino projects. It allows your Arduino to talk to your computer, Bluetooth modules, Wi-Fi modules, sensors, and even other Arduinos. If you have ever used the Serial Monitor, you have already used serial communication - now you will understand how it really works. By the end of this guide, you will learn: What serial communication is What UART means How Arduino communicates with devices How to send and receive data How baud rate works Common mistakes beginners make What is Serial Communication? Serial communication is a method of sending data one bit at a time over a single wire. Instead of sending many bits simultaneously (parallel communication), serial sends bits sequentially: Example: The letter A in binary = 01000001 These bits are sent one after another very quickly. This method is: Simple Reliable Perfect for microcontrollers What is UART? UART stands for: Universal Asynchronous Receiver Transmitter It is the hardware ins...

How to Connect HC-06 Bluetooth Module with Arduino (Step-by-Step Beginner Guide)

Bluetooth communication is one of the easiest ways to make Arduino projects wireless. Once you understand how to use a Bluetooth module, you can build robots, home automation systems, and smartphone-controlled devices. In this tutorial, you will learn how to interface the HC-06 Bluetooth module with Arduino and send data wirelessly using your phone.    By the end of this guide, you will be able to: Understand how the HC-06 Bluetooth module works Configure the module using AT commands Safely connect it to an Arduino Send and receive serial data over Bluetooth Build the foundation for wireless Arduino projects Watch the Project Overview What is the HC-06 Bluetooth Module? The HC-06 is a Bluetooth-to-Serial module designed to add wireless communication to microcontrollers such as Arduino. It uses Bluetooth 2.0 (SPP - Serial Port Protocol), which means it behaves like a wireless serial cable. Whatever is sent from the phone appears in Arduino’s Serial Monitor and vice versa. ...

How to Use SoftwareSerial on Arduino (Connect Multiple Serial Devices)

In the previous tutorial, you learned how Arduino uses hardware serial communication through pins 0 (RX) and 1 (TX). That built-in serial port is mainly used to communicate with your computer through the USB cable and Serial Monitor . But here is the problem beginners quickly face: Many popular modules use serial communication, such as: Bluetooth modules ( HC-05 / HC-06 ) GPS modules GSM modules Wi-Fi modules ( ESP8266 ) Arduino Uno has only one hardware serial port. So how can we connect these modules while still using the Serial Monitor? The solution is SoftwareSerial . In this tutorial you will learn how to create additional serial ports using normal digital pins. By the end, your Arduino will be able to talk to multiple serial devices. What is SoftwareSerial? SoftwareSerial is a special Arduino library that allows digital pins to behave like RX and TX pins. This means Arduino can create extra serial ports using software instead of hardware. Hardware Serial vs Software Seri...