3 LED Crossfade with PWM and Arduino

arduino 3led crossfade with pwm

This ones quite simple, just mix/ crossfade 3 separate LEDs using Phase Width Modulation (PWM) and Arduino.

You will need 3 LEDs, I’m using a 5mm Red, Green and Blue so I can make the full light colour spectrum (When all combined they make white light). If you look at an LED you will see one pin is slightly longer of the 2, this is the positive (+) terminal, the shorter being negative. Make sure you get the LED the right way round.

You will also need 3 220 Ohm resistors to ensure that the LEDs dont get too much voltage/current. these have the following colour code: Red, Red, Brown, Gold. If you use a higher value such as 1 Kiloohm (1ooo Ohms) the LEDs will appear dim as they won’t get enough power. Unlike the LED it doesn’t matter which way round you use the resistors. I generally would recommend getting a kit of different types of resistors like this one here as its useful in the long run.

You will also need some jumper wires and you also need to have an Arduino board and know how to upload a sketch.

So a quick look at this basic circuit:

The Arduino board supplies the power to the LED via the digital PWM pins, meaning for this we don’t need to use the 5v pin. By using the chip and PWM to regulate the power, basically its a fancy way of saying we’re blinking the LED, we can effectively alter the brightness of the LED. So alot of the hardwork and potential coding we would need to do is taken care of for us.

I connect each digital pin (9,10 & 11) to an LED – I’ve used corresponding coloured wiring for this, e.g. red wire, red LED. I’ve placed a resistor between the wire and the LED to ensure that it doesn’t get too much juice. I then connect the negative pin of each LED to the negative rail on the breadboard and then that goes to the GND pin on the Arduino board completing the circuit.

Here’s my final circuit below, I’ve added a few labels to show you where thigns are connected and the +/- for the LEDs I’ve also added a few lines so you see where things line up.

[ad#Google Ad in content]
The only thing left is to upload the following sketch, this sets a value for each LED, assigning it to a pin, the code then loops through incrementing the values between 1 and 255 for each LED. The counter in the code starts at 1 and increments up to 762 (254×3 – 254 values for each LED) before reseting to 1 to start the loop again.

Arduino PWM LED code

// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

// Program variables set as integer (number) type
int redLEDValue = 255; // Variables to store the values to send to the pins
int greenLEDValue = 1; // Initial values are Red full, Green and Blue off
int blueLEDValue
= 1; // These values get passed to the analogWrite() function.
int i = 0; // Loop counter

//setup the pins/ inputs & outputs
void setup()
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

// Main program, count to 763, a third of the way switch the incrementing of the LEDs
void loop()
i += 1; // Increment counter
if (i < 255) // First phase of fades { redLEDValue -= 1; // Red down greenLEDValue += 1; // Green up blue
LEDValue = 1; // Blue low
else if (i < 509) // Second phase of fades { red
LEDValue = 1; // Red low
LEDValue -= 1; // Green down
LEDValue += 1; // Blue up
else if (i < 763) // Third phase of fades { red
LEDValue += 1; // Red up
LEDValue = 1; // Green low
LEDValue -= 1; // Blue down
else // Re-set the counter, and start the fades again
i = 1;
// analogWrite() expects 2 parameters, the object/pin and a value between 0 (off) and 255 (full on)
analogWrite(redPin, redLEDValue); // Write current values to LED pins
analogWrite(greenPin, green
analogWrite(bluePin, blue

delay(50); // Pause for xx milliseconds before resuming the loop

[ad#Google Ad in content]
And thats it! If you’ve got it right you should see each of your LEDs graduating between on and off. I found the initial sketch for this here but I’ve stripped out all the debug and altered it slightly to make it easier to understand and follow.

One Comment

  • what if I wanted to run more LEDs? I thought the voltage drop was 2-3 volts for an LED. If the arduino can only supply 5 volts how does it have enough voltage to run all the LEDs? thanks, im just learning…

You must be logged in to post a comment.