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.
You will need:
On the Arduino Uno R3, PWM-capable digital pins are marked with a ~ symbol.
The PWM pins are:
Arduino's current documentation identifies pins 3, 5, 6, 9, 10, and 11 as PWM-capable pins on the Arduino Uno R3.
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.
Therefore:
D9 → 220 Ω resistor → LED anode → LED cathode → GND
Make sure the LED is connected with the correct polarity.
For a typical LED:
A normal digital output has two states:
50% duty cycle
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.
The brightness variable stores the current PWM value.
It begins at:
This variable determines how much the brightness changes during each loop.
Initially, the value is positive:
The final effect is:
OFF → gradually brighter → maximum brightness → gradually dimmer → OFF → repeat
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.
You learned:
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.
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!
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
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
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
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
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:
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().
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.
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.
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.
int fadeAmount = 5;A value of 0 means that the LED starts OFF.
3. Creating the Fade Amount
This variable determines how much the brightness changes during each loop.
Initially, the value is positive:
fadeAmount = 5
Therefore, the brightness increases:
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:
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.
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:
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:
turns the LED OFF.
While:
analogWrite(ledPin, 255);
sets the PWM output to its maximum value.
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:
This changes the brightness value each time the loop() runs.
When fadeAmount is 5:
0
5
10
15
20
...
255
When fadeAmount becomes -5:
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:
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:
brightness is greater than or equal to 255.
When either boundary is reached, the sign of fadeAmount is reversed.
For example:
fadeAmount = 5
becomes:
becomes:
fadeAmount = -5
This causes the LED to fade in the opposite direction.
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.
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:- Connect the Arduino Uno R3 to your computer.
- Select the correct Arduino board.
- Select the correct port.
- Click Verify to compile the sketch.
- If there are no errors, click Upload.
- Wait for the upload to complete.
- Observe the LED connected to D9.
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:
to:
int fadeAmount = 1;
The brightness will change in smaller steps.
Now try:
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.
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:
to:
delay(10);
Then try:
Then try:
delay(100);
Observe how the different delay values affect the speed of the fade.
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:
rather than:
digitalWrite(ledPin, brightness);
Also verify that the LED is connected to a PWM-capable pin.
Remember:
Anode → resistor → D9
Cathode → GND
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.
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
Post a Comment