Introduction: Fading RGB LED (Arduino)

In this Instructable I'm going to show you how to make a simple fading RGB Led.
It can be used as a nice night lamp or as mood lighting.

Step 1: Parts

To make it you'll need:
- an Arduino ( I'm using an Arduino UNO R3, but others may work aswell )
- an RGB Led ( I used a common anode one, common cathode will work too )
- a Breadboard
- some wire
- 2 resistors 180 ohm  ( Brown, Grey, Brown, Gold )
- 1 resistor 330 ohm ( Orange, Orange, Brown, Gold )

Step 2: Schematic

The schematic is very simple.
If you have a commone anode led (like i do) just connect it to +5v, if you have a common cathode led connect it to 0v.
Connect the 3 other leads to 3 PWM pins on your arduino with a resistor between (the value depends on the led).

Step 3: Code

The code is a little bit more difficult but still fairly simple.
This code only works for common anode leds.
If you want to use it for common cathode leds you'll have to change all  the "analogWrite( COLOR, 255 - colorVal );" lines to "analogWrite( COLOR, colorVal );" (without the "255 - "), then it should work (i didn't test it).

#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 20

void setup() {

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
}

int redVal;
int blueVal;
int greenVal;
 
void loop() {
 
  int redVal = 255;
  int blueVal = 0;
  int greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    greenVal += 1;
    redVal -= 1;
    analogWrite( GREEN, 255 - greenVal );
    analogWrite( RED, 255 - redVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 0;
  greenVal = 255;
  for( int i = 0 ; i < 255 ; i += 1 ){
    blueVal += 1;
    greenVal -= 1;
    analogWrite( BLUE, 255 - blueVal );
    analogWrite( GREEN, 255 - greenVal );

    delay( delayTime );
  }
 
  redVal = 0;
  blueVal = 255;
  greenVal = 0;
  for( int i = 0 ; i < 255 ; i += 1 ){
    redVal += 1;
    blueVal -= 1;
    analogWrite( RED, 255 - redVal );
    analogWrite( BLUE, 255 - blueVal );

    delay( delayTime );
  }
}

Step 4: DONE !

Use your imagination to extend / modify / improve / ... the project.
Show your creations in the comments and please tell me what you think about this instructable, because it's my first one.
Also, Please correct me if I made any mistakes.

Comments

author
thespawnkid made it!(author)2016-09-06

Unfortunately, it won't work on SAMD/Arduino M0 boards :(

author
Debanjan+das made it!(author)2016-06-08

thanks !!

it was very usefull

author
marsekal made it!(author)2016-06-07

thank mr

author
MarinoF made it!(author)2015-12-29

So i'm making an rgb led that is controlled by an IR remote and I intend to use your wonderful code as one of the functions. My problem is that after its faded for a while and stops when I want it to stop, it doesn't resume receiving IR signals. I've had my Arduino Uno R3 for about a week now, and have only very little knowledge in C, although I've worked with some other languages before. Here is the code im using, any help would be greatly appreciated.

#include <IRremote.h>

int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);

decode_results results;

int redPin = 11;

int greenPin = 10;

int bluePin = 9;

#define delayTime 20

void setup()

{

Serial.begin(9600);

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

irrecv.enableIRIn();

}

int redVal;

int blueVal;

int greenVal;

void loop() {

if (irrecv.decode(&results)) {

Serial.println(results.value, DEC);

if (results.value == 16724175)

{setColor(255, 0, 0);}

if (results.value == 16718055)

{setColor(0, 255, 0);}

if (results.value == 16743045)

{setColor(0, 0, 255);}

if (results.value == 16738455)

{setColor(0, 0, 0);}

if (results.value == 16748655)

{setColor(255, 255, 255);}

if (results.value == 16761405)

{int redVal = 255;

int blueVal = 0;

int greenVal = 0;

for( int i = 0 ; i < 255 ; i += 1 ){

greenVal += 1;

redVal -= 1;

analogWrite( greenPin, 255 - greenVal );

analogWrite( redPin, 255 - redVal );

delay( delayTime );

}

redVal = 0;

blueVal = 0;

greenVal = 255;

for( int i = 0 ; i < 255 ; i += 1 ){

blueVal += 1;

greenVal -= 1;

analogWrite( bluePin, 255 - blueVal );

analogWrite( greenPin, 255 - greenVal );

delay( delayTime );

}

redVal = 0;

blueVal = 255;

greenVal = 0;

for( int i = 0 ; i < 255 ; i += 1 ){

redVal += 1;

blueVal -= 1;

analogWrite( redPin, 255 - redVal );

analogWrite( bluePin, 255 - blueVal );

delay( delayTime );

}

}

irrecv.resume();

}

}

void setColor(int red, int green, int blue)

{

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue);

}

author
georgekate made it!(author)2015-09-26

Been working on something similar lately, I got a cleaner way :)

for (int i = 0; i <= 510; i += 1)

{

int blue = -fabs(i - 255) + 255; // There is a minus here

int red = fabs(i - 255) + 255; // No minus here

..

delay(delayTime);

}

author
zbot473 made it!(author)2015-08-21

Nice job

author
nitin4212 made it!(author)2015-07-22

HOW TO USE SAME FUNCTION USING IR REMOTE PL HELP ME AS SOON AS POSSIBLE

author
mokoenarn made it!(author)2015-06-06

author
MarkoSallinen made it!(author)2015-02-03

Hey! This one is really helpful for me, as I've just gotten in to this. So I have a couple of questions of things I'm a bit unsure of. First of all, if I want multiple RGB LED's being the same color, can I just put them together and just use the one resistor per color in between the Arduino and the first led? The second question is can I just replace the delay function to a button press to change the colors? And how would that look like in the code?

author
Sid+AhmedB made it!(author)2015-01-10

Thanx for sharing;

Yep, confirming it's working with common cathodes by deleting the (255-) for the value of each color.

author
Mexica72 made it!(author)2014-11-24

Hi I have an rgb set up to react to sound with red triggering at highest levels and green lowest. at present when no detecable sound I can set each LED to shine or not between 0 and 255. What I would like is, when I have no sound, for the lights to fade through the colours like your sketch I presume? I am a couple of weeks into Arduino with no programming background. Could I tag your sketch onto the existing sketch I am using, so instead of just static light when no noise, the rgb reacts to your sketch, but when there is sufficient noise the original sketch reacts whilst yours switches off?

Is it some kind of "if" function connected to the output of A0 (what the sound sensor is connected to?

here is the sketch i'm using (thanks to Alejandro Taracido Cano):

int sensorValue = analogRead(A0); // pin that the mic is attached to

int redPin = 9; // pins that the cathodes of LED are attached to
int greenPin = 10;
int bluePin = 11;

int MicValue = 0; // the Microphone value

void setup() {
//Serial.begin(9600); //for test the input value initialize serial
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

analogWrite(redPin, 50); //turn off all LEDs
analogWrite(greenPin, 50);
analogWrite(bluePin, 50);
}

void loop() {

MicValue = analogRead(A0); //read the value of the microphone

//Serial.println(MicValue); //for test the input value

if (MicValue > 400) { //adjust this value to the desired sensitivity
analogWrite(bluePin,0); //lights up blue
delay(15); //small delay for quick response at low noise levels
}

if (MicValue > 500) { //adjust this value to the desired sensitivity
analogWrite(bluePin,255); //lights up green and turn off blue
analogWrite(greenPin, 0);
delay(60); //mid delay for response at mid noise levels
}

if (MicValue > 650) { //adjust this value to the desired sensitivity
analogWrite(greenPin,255); //lights up red and turn off green
analogWrite(redPin, 0);
delay(140); //high delay for response at high noise levels
}

analogWrite(greenPin, 255); //Turn off all LEDs
analogWrite(redPin, 255);
analogWrite(bluePin,50);

}

I am looking at your code now so may find a solution in the meantime, but any help gratefully received!

author
mephisztoe made it!(author)2014-10-21

This is exactly what I was looking for to realize a simple prototype. Thanks!! Here you can see the results so far. :)

http://youtu.be/TpiX0-XKCU8

Arduino RGB Fader Lamp.png
author
Jerry19 made it!(author)2014-10-17

#define RED 3

#define GREEN 5

#define BLUE 6

#define delayTime 5

byte redVal=255;

byte greenVal=0;

byte blueVal=0;

void setup()

{

pinMode(RED, OUTPUT);

pinMode(GREEN, OUTPUT);

pinMode(BLUE, OUTPUT);

digitalWrite(RED, 1);

digitalWrite(GREEN, 0);

digitalWrite(BLUE, 0);

}

void loop()

{

for( byte i = 0 ; i < 255 ; i++ ){

redVal--;

greenVal++;

analogWrite( RED, redVal );

analogWrite( GREEN, greenVal );

delay( delayTime );

}

for( byte i = 0 ; i < 255 ; i++ ){

greenVal--;

blueVal++;

analogWrite( GREEN, greenVal );

analogWrite( BLUE, blueVal );

delay( delayTime );

}

for( byte i = 0 ; i < 255 ; i++ ){

blueVal--;

redVal++;

analogWrite( BLUE, blueVal );

analogWrite( RED, redVal );

delay( delayTime );

}

}

author
mhrsolanki2020 made it!(author)2014-03-12

I made this (with Arduino Compatible board ) eNTuino ...
I used 2 resistors (220E + 100E) between the PWM pin and the RGB pins ..
and then there is a 100E resistor between 5v pin and the common anode of RGB
(NOTE: the led flickers when you use 220E is a diffused RGB led like mine)

Also,
What i dont understand is , theres no wire going back to ground..(there is no ground wire between my breadboard and eNTuino..)
Can someone explain me what am i missing?

Sorry i dont own a nice camera soo pics are little blured


1.jpg2.jpg3.jpg4.jpg
author
nnycapcadet made it!(author)2014-06-05

Nothing seems wrong with your wiring if that's what you're asking. The reason you don't need a ground wire is because it's a anode, so you're controlling how much power is going to the other led leads. If 255 is being given to one lead, it wont be lit because the 5 volts going to the anode and the 5 volts going to the lead cancel each other out, kind of. but when the lead is getting 0, then it's basically a ground pin, so the led is lit at full power. I think thats what you're wondering, right?

author
Higgs+Boson made it!(author)2012-01-11

In the schematic it appears the LEDs have a common anode. but I've read that RGB LEDs have a common cathode.

author
bodie made it!(author)2013-08-08

I'm grateful that you pointed out this discrepancy. I found a similar tutorial in a book I was using but couldn't make it work. The author failed to mention this potential difference in hardware. When I switched pin 2 on the LED from ground to +5V, it worked like a champ. I'm glad I found your 'ible!

author
cyragia made it!(author)2012-01-12

you have both common anode and common cathode rgb-leds.
in my schematic i used a common anode one because i only have common anode ones.

author
Higgs+Boson made it!(author)2012-01-12

Okay. thank you.

author
cyragia made it!(author)2012-01-12

No Problem :)
What do you think of my instructable ?
Any ways i can improve it ?

author
Higgs+Boson made it!(author)2012-01-12

It seems to have everything you would need to make this project. I think it is very good. I'll probably make it sometime soon.

author
Higgs+Boson made it!(author)2012-01-16

I just made it, and it works great.

author
cyragia made it!(author)2012-01-16

:D

author
jamster545 made it!(author)2013-01-01

I'm designing a RGB (plus white and blacklight) lighting controller for my Home Automation project, and I used this code (slightly modified) for my "fade" function. I gave you credit in a comment

author
ponynicker53 made it!(author)2012-10-06

How would I install (in circuit and in code) a photosensor that only turns this on when it is dark out?

author
ponynicker53 made it!(author)2012-10-07

Nevermind, figured it out. Added a 10kOhm resistor and photosensor. See diagram. I have also pasted in the code. I wanted to add a delay so that if it is light out, the program is not constantly checking for darkness, but rather sampling the photo sensor every few minutes or so to conserve battery. I put a delay() in the else statement (that tells the LEDs to stay off), but it does not seem to work. I tried testing it by letting it run in the dark, durning on the light (the cycle stops at RED) giving it a few seconds and then turning off the light. It should start again in about 2 minutes, but it starts immediately with the light off. Any suggestions?



#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 120
int ldr = 1; //Define photosensor in Analog port 1
int lightinput = 0; //Default value for lightinput is zero
int trigger = 300; //Threshold value of light to trigger activation.
//I have found between 200 and 600 works best. You don't want it so
//sensitive that it detects itself and flickers at the end of it's
//cycle.

void setup() {

pinMode(GREEN, OUTPUT); //define as digital outputs
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
}

int redVal;
int blueVal;
int greenVal;

void loop() {
lightinput = analogRead(ldr); //difine lightinput as value from photosensor
if (lightinput < trigger) //compare value to threshold trigger
{ //open if statement (if DARK)
int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );

delay( delayTime );
}

redVal = 0;
blueVal = 0;
greenVal = 255;
for( int i = 0 ; i < 255 ; i += 1 ){
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );

delay( delayTime );
}

redVal = 0;
blueVal = 255;
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );

delay( delayTime );
}
} //close if statement
else
{ //open else (if LIGHT)
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
delay(12000); //This code is supposed to save energy by only detecting
//light every two minutes as opposed to constantly, however it does not
//work.
} //close else
} //close void loop

color light diagram_bb.jpg
author
ponynicker53 made it!(author)2012-10-07

Bigger pic

color light diagram_bb.jpg
author
cityboy1 made it!(author)2012-09-18

It Worked the first time, all I have to do now is to learn to read/write code. Anybody know of any good code tutorials.

author
EET1982 made it!(author)2012-09-05

Sweet 'able. I got it working. Thank you very much :)

author
adammary made it!(author)2012-04-12

Hi, your tutorial looks great and I'm going to try it soon.
I don't have the components yet, but for now I'm trying to understand the code.

In the first for loop, the LED slowly fades from green to red right?
But from what i understand in the second for loop, the LED fades from blue to green. Don't you need a for loop that fades the LED from red to blue before starting the second for loop to make sure smooth fading?

In other words, shouldn't the 3rd and 2nd for loops need to switch places so that the color fading is in order?

This is what I understand from the code. Correct me if i'm wrong.


author
adammary made it!(author)2012-04-12

oh wait nevermind i got it now. i didn't realize that all three pins are initially set to high

author
on6mj made it!(author)2012-04-06

Great!!!!!

author
cyragia made it!(author)2012-04-06

Thanks !

author
on6mj made it!(author)2012-04-07

OK, but your program is short and verry efficient.
Realy smooth color changing.
Realy nice.
Best WE,
Jean.

author
jborgne made it!(author)2012-01-22

this is a good instructable. Very clear and easy todo, and does not require a ton of parts to complete. Keep up the good tut's.


author
cyragia made it!(author)2012-01-22

Thanks !

About This Instructable

201,760views

92favorites

License:

More by cyragia:Fading RGB LED (Arduino)
Add instructable to: