# Project 6 : Led Fading With PWM

1,593

27

Published

## Introduction: Project 6 : Led Fading With PWM

Today we are going to learn how to fade an led with a PWM pin. First we will see what is PWM. PWM stands for Pulse W

## Step 1: What You Require?

1. Arduino Uno or Arduino Mega 2560 (They are the beginning boards of Arduino)
2. Jumper Wires (Male to Male)
3. 1 Led's
5. 10 K (more preferable) or 1k resistors

## Step 2: Build Your Circuit and Know Why Is It Designed So?

The above diagram shows blue lines which represents the Jumper Wires. Jumper Wires are connective units. They transfer power and do a lot of jobs. In this program they are only transferring power. An Led requires 2 types of power to light it up. Positive and Negative. Positive Energy is supplied by Digital Pins which is distinct than the other Pin giving Positive Energy by its name. It's vice versa for identifying the source of power and where is it transferred. An led when directly supplied by positive energy fuses up that's why we use a resistor. Before me move forward let's discuss about the positive negative rods of the led. The longer is positive and the shorter is negative. The resistor is connected with the positive rod which goes in the same row. In the same column but different row of the other end of the resistor comes the jumper wire connecting it with the digital pin.

Negative energy is noted as GND and is directly supplied to the shorter rod of the Led using a Jumper Wire. That completes the circuit diagram and why is it placed or designed in this way.

The digital pin that is providing positive energy is also a PWM pin and a PWM pin is able to perform analog activities.

## Step 3: Now for the Software and Understanding It

1. int led1 = 9;
2. int brightness = 0;
4. void setup()
5. {pinMode(led1, OUTPUT);}
6. void loop()
7. {analogWrite(led1, brightness);
8. brightness = brightness + fade;
9. if (brightness == 0){
11. if (brightness == 255){
13. delay(30);}

First we take an integer, led which we set as 9. Then we take an another integer brightness and set it as 0. An another integer, fade is set to 5. In the void setup section we define led as output through pinMode function. In the loop section we use a function analogWrite in which we first mention the led integer, led1 to the frequency integer, brightness which is set to 5. It's set to 5 because then it can equally go from 0-255 and vice versa. We can also use 3 as the value for brightness because it can also equally go from 0-255 and vice versa. Then we say that brightness [integer] is equal to brightness [the same integer] + fade [integer equal to 5]. So the brightness [both integer as well as literally] keeps increasing from 0,5,10,15 ... all the way to 255. Then we use the if statement to tell that if the brightness is 0 then the integer, fade will be 5. If brightness is 255 then the fade will become -5 so it comes down 255, 200, 195, 190 ... and so all the way to 0. We set the delay to 30 milliseconds as the time between each fade. So we can say that if the brightness is increasing from 0-5 then the time period for it's 30 milliseconds.

## Step 4: Enjoy!!!

I hope you understood how to fade an led by using PWM pins. I also hope you understood the function of analogWrite (). I have been trying to make different colors by using 3 led's, red-blue-green by PWM and RGB functions. But I have not been able to do so until now. If anyone has any idea please provide me with suggestions in the comments. Other than that if any one has any doubt just ask it in the comments.

My last instructable was featured and as a result I got a bronze medal. Thanks a lot for all the support and do not forget to share and follow.

## Recommendations

• ### Arduino Class

69,688 Enrolled

• ### Woodworking Contest

We have a be nice policy.