HW4: Multiple LEDs fading with PWM pins

As extention of the last project we will see how to have a multiple LEDs fading effect by connecting 4 LEDs using the PWM pins.

What we need (from now on I will not mention Arduino, breadboard and wires):

  • 4x 5mm LEDs
  • 4x 220Ω resistors

To connect the 4 LEDs is very easy, we only need to do 4 times what we did in the previous project.

Let’s see how it looks with an image (click on it to zoom).

As you can see it is not so different from the previous project, we have 4 times the same pattern.

We have one wire connecting the pin 3 to the positive lead of the bottom LED, a resistor with one end connected to the negative lead and the other to the – column.

The same pattern is repeated for the other 3 LEDs, the only difference is that their positive leads are connected to different pins, in order from the bottom: 5, 6 and 9.

One thing to notice is that we have 4 LEDs and each of them has to be connected to the ground, but Arduino has only 3 GND pins. That is not a problem. We can connect one GND pin to the – column of the breadboard and as we know from a previous article the whole column is connected to the ground. To connect to the ground the LEDs is enough to put a wire from the free end of each resistor to the – column.

The circuit is now complete, let’s see the code

At the beginning we give a name to each pin, this time also because it will be easier to know which LED will be affected by the code.

Into the setup()function we declare how we will use the pins, we will use them as output.

Line 15: we have a for cycle which will do 256 iterations, from 0 to 255

Line 16: we are using digital pins, but we read analogWrite(LED1, i). We can use that function because we are using PWM pins. analogWrite()takes 2 parameters, the first is the pin number, the second is an integer number from 0 to 255.  The second parameter says the duty cycle on the pin, you can think about it as the intensity of the signal sent from the pin. Each iteration increments the intensity by one. You can also change the step to increment the i variable, that will affect the speed of the fading effect. If you want to learn more about this, I suggest you to read these links: PWM and analogWrite().

Line 17: only a pause at the end of each iteration. Removing the pause, the fading would be so fast that all the LEDs would look lit at the same time. Increasing it instead we would see a slower fading effect.

Line 19 to 21: we do the same thing as above, but this time backwards, the for cycle goes from 255 (max intensity) to 0 (LED off).

Then we repeat the code for each other LED, the code is the same, the only thing which changes is the pin number.

Thinking to the LEDs numbered from 1 to 4 from the bottom to the top of the image, you should see the LEDs to fade in and out one at time.

You can experiment by yourself the fading effect trying to make it faster or slower or changing the order to turn on the LEDs and so on. You could also try to add another LED to have 5 of them.

In the next article we will learn how to change an LED brightness using a potentiometer.


I’ve tested the circuit and the code posted in this article on my equipment and it works properly, anyway I do not assume any responsibility for any damage which your components could suffer.