loading
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.
<p>Unfortunately, it won't work on SAMD/Arduino M0 boards :(</p>
<p>thanks !!</p><p>it was very usefull</p>
<p>thank mr</p>
<p>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.</p><p> #include &lt;IRremote.h&gt;</p><p>int RECV_PIN = 7;</p><p>IRrecv irrecv(RECV_PIN);</p><p>decode_results results;</p><p>int redPin = 11;</p><p>int greenPin = 10;</p><p>int bluePin = 9;</p><p>#define delayTime 20</p><p>void setup()</p><p>{</p><p> Serial.begin(9600);</p><p> pinMode(redPin, OUTPUT);</p><p> pinMode(greenPin, OUTPUT);</p><p> pinMode(bluePin, OUTPUT);</p><p> irrecv.enableIRIn();</p><p>}</p><p>int redVal;</p><p>int blueVal;</p><p>int greenVal;</p><p>void loop() {</p><p>if (irrecv.decode(&amp;results)) {</p><p>Serial.println(results.value, DEC);</p><p>if (results.value == 16724175)</p><p>{setColor(255, 0, 0);}</p><p>if (results.value == 16718055)</p><p>{setColor(0, 255, 0);}</p><p>if (results.value == 16743045)</p><p>{setColor(0, 0, 255);}</p><p>if (results.value == 16738455)</p><p>{setColor(0, 0, 0);}</p><p>if (results.value == 16748655)</p><p>{setColor(255, 255, 255);}</p><p>if (results.value == 16761405)</p><p> {int redVal = 255;</p><p> int blueVal = 0;</p><p> int greenVal = 0;</p><p> for( int i = 0 ; i &lt; 255 ; i += 1 ){</p><p> greenVal += 1;</p><p> redVal -= 1;</p><p> analogWrite( greenPin, 255 - greenVal );</p><p> analogWrite( redPin, 255 - redVal );</p><p> delay( delayTime );</p><p> }</p><p> redVal = 0;</p><p> blueVal = 0;</p><p> greenVal = 255;</p><p> for( int i = 0 ; i &lt; 255 ; i += 1 ){</p><p> blueVal += 1;</p><p> greenVal -= 1;</p><p> analogWrite( bluePin, 255 - blueVal );</p><p> analogWrite( greenPin, 255 - greenVal );</p><p> delay( delayTime );</p><p> }</p><p> redVal = 0;</p><p> blueVal = 255;</p><p> greenVal = 0;</p><p> for( int i = 0 ; i &lt; 255 ; i += 1 ){</p><p> redVal += 1;</p><p> blueVal -= 1;</p><p> analogWrite( redPin, 255 - redVal );</p><p> analogWrite( bluePin, 255 - blueVal );</p><p> delay( delayTime );</p><p> }</p><p> }</p><p>irrecv.resume();</p><p>}</p><p>}</p><p>void setColor(int red, int green, int blue)</p><p>{</p><p> analogWrite(redPin, red);</p><p> analogWrite(greenPin, green);</p><p> analogWrite(bluePin, blue); </p><p>}</p>
<p>Been working on something similar lately, I got a cleaner way :)</p><p>for (int i = 0; i &lt;= 510; i += 1)</p><p>{</p><p>int blue = -fabs(i - 255) + 255; // There is a minus here</p><p>int red = fabs(i - 255) + 255; // No minus here</p><p>..</p><p>delay(delayTime);</p><p>}</p>
<p>Nice job</p>
<p>HOW TO USE SAME FUNCTION USING IR REMOTE PL HELP ME AS SOON AS POSSIBLE</p>
<p>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?</p>
<p>Thanx for sharing;</p><p>Yep, confirming it's working with common cathodes by deleting the (255-) for the value of each color.</p>
<p>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?</p><p>Is it some kind of &quot;if&quot; function connected to the output of A0 (what the sound sensor is connected to? </p><p>here is the sketch i'm using (thanks to Alejandro Taracido Cano):</p><p>int sensorValue = analogRead(A0); // pin that the mic is attached to<br><br>int redPin = 9; // pins that the cathodes of LED are attached to <br>int greenPin = 10;<br>int bluePin = 11;<br><br>int MicValue = 0; // the Microphone value<br><br>void setup() {<br> //Serial.begin(9600); //for test the input value initialize serial<br> pinMode(redPin, OUTPUT);<br> pinMode(greenPin, OUTPUT);<br> pinMode(bluePin, OUTPUT);<br><br> analogWrite(redPin, 50); //turn off all LEDs<br> analogWrite(greenPin, 50);<br> analogWrite(bluePin, 50);<br>}<br><br>void loop() {<br><br> MicValue = analogRead(A0); //read the value of the microphone<br> <br> //Serial.println(MicValue); //for test the input value<br><br> if (MicValue &gt; 400) { //adjust this value to the desired sensitivity<br> analogWrite(bluePin,0); //lights up blue<br> delay(15); //small delay for quick response at low noise levels<br> }<br> <br> if (MicValue &gt; 500) { //adjust this value to the desired sensitivity<br> analogWrite(bluePin,255); //lights up green and turn off blue<br> analogWrite(greenPin, 0);<br> delay(60); //mid delay for response at mid noise levels<br> }<br> <br> if (MicValue &gt; 650) { //adjust this value to the desired sensitivity<br> analogWrite(greenPin,255); //lights up red and turn off green<br> analogWrite(redPin, 0);<br> delay(140); //high delay for response at high noise levels<br> }<br><br>analogWrite(greenPin, 255); //Turn off all LEDs<br>analogWrite(redPin, 255);<br>analogWrite(bluePin,50); <br><br>}</p><p>I am looking at your code now so may find a solution in the meantime, but any help gratefully received!</p>
<p>This is exactly what I was looking for to realize a simple prototype. Thanks!! Here you can see the results so far. :)</p><p><a href="http://youtu.be/TpiX0-XKCU8" rel="nofollow">http://youtu.be/TpiX0-XKCU8</a></p>
<p>#define RED 3</p><p>#define GREEN 5</p><p>#define BLUE 6</p><p>#define delayTime 5</p><p>byte redVal=255;</p><p>byte greenVal=0;</p><p>byte blueVal=0;</p><p>void setup()</p><p>{</p><p> pinMode(RED, OUTPUT);</p><p> pinMode(GREEN, OUTPUT);</p><p> pinMode(BLUE, OUTPUT);</p><p> digitalWrite(RED, 1);</p><p> digitalWrite(GREEN, 0);</p><p> digitalWrite(BLUE, 0);</p><p>}</p><p>void loop() </p><p>{</p><p>for( byte i = 0 ; i &lt; 255 ; i++ ){</p><p> redVal--;</p><p> greenVal++;</p><p> analogWrite( RED, redVal );</p><p> analogWrite( GREEN, greenVal );</p><p> delay( delayTime );</p><p> }</p><p>for( byte i = 0 ; i &lt; 255 ; i++ ){</p><p> greenVal--;</p><p> blueVal++;</p><p> analogWrite( GREEN, greenVal );</p><p> analogWrite( BLUE, blueVal );</p><p> delay( delayTime );</p><p> }</p><p>for( byte i = 0 ; i &lt; 255 ; i++ ){</p><p> blueVal--;</p><p> redVal++; </p><p> analogWrite( BLUE, blueVal );</p><p> analogWrite( RED, redVal );</p><p> delay( delayTime );</p><p> }</p><p>}</p>
<p>I made this (with Arduino Compatible board ) eNTuino ...<br>I used 2 resistors (220E + 100E) between the PWM pin and the RGB pins ..<br>and then there is a 100E resistor between 5v pin and the common anode of RGB <br>(NOTE: the led flickers when you use 220E is a diffused RGB led like mine)<br><br>Also,<br>What i dont understand is , theres no wire going back to ground..(there is no ground wire between my breadboard and eNTuino..) <br>Can someone explain me what am i missing?<br><br>Sorry i dont own a nice camera soo pics are little blured <br><br><br></p>
<p>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?</p>
In the schematic it appears the LEDs have a common anode. but I've read that RGB LEDs have a common cathode.
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!
you have both common anode and common cathode rgb-leds.<br>in my schematic i used a common anode one because i only have common anode ones.
Okay. thank you.
No Problem :)<br>What do you think of my instructable ?<br>Any ways i can improve it ?
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.
I just made it, and it works great.
:D
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 &quot;fade&quot; function. I gave you credit in a comment
How would I install (in circuit and in code) a photosensor that only turns this on when it is dark out?
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? <br> <br> <br> <br>#define GREEN 3 <br>#define BLUE 5 <br>#define RED 6 <br>#define delayTime 120 <br>int ldr = 1; //Define photosensor in Analog port 1 <br>int lightinput = 0; //Default value for lightinput is zero <br>int trigger = 300; //Threshold value of light to trigger activation. <br>//I have found between 200 and 600 works best. You don't want it so <br>//sensitive that it detects itself and flickers at the end of it's <br>//cycle. <br> <br>void setup() { <br> <br> pinMode(GREEN, OUTPUT); //define as digital outputs <br> pinMode(BLUE, OUTPUT); <br> pinMode(RED, OUTPUT); <br> digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value <br> digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness. <br> digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs <br>} <br> <br>int redVal; <br>int blueVal; <br>int greenVal; <br> <br>void loop() { <br> lightinput = analogRead(ldr); //difine lightinput as value from photosensor <br> if (lightinput &lt; trigger) //compare value to threshold trigger <br> { //open if statement (if DARK) <br> int redVal = 255; <br> int blueVal = 0; <br> int greenVal = 0; <br> for( int i = 0 ; i &lt; 255 ; i += 1 ){ <br> greenVal += 1; <br> redVal -= 1; <br> analogWrite( GREEN, 255 - greenVal ); <br> analogWrite( RED, 255 - redVal ); <br> <br> delay( delayTime ); <br> } <br> <br> redVal = 0; <br> blueVal = 0; <br> greenVal = 255; <br> for( int i = 0 ; i &lt; 255 ; i += 1 ){ <br> blueVal += 1; <br> greenVal -= 1; <br> analogWrite( BLUE, 255 - blueVal ); <br> analogWrite( GREEN, 255 - greenVal ); <br> <br> delay( delayTime ); <br> } <br> <br> redVal = 0; <br> blueVal = 255; <br> greenVal = 0; <br> for( int i = 0 ; i &lt; 255 ; i += 1 ){ <br> redVal += 1; <br> blueVal -= 1; <br> analogWrite( RED, 255 - redVal ); <br> analogWrite( BLUE, 255 - blueVal ); <br> <br> delay( delayTime ); <br> } <br> } //close if statement <br> else <br> { //open else (if LIGHT) <br> digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value <br> digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness. <br> digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs <br> delay(12000); //This code is supposed to save energy by only detecting <br> //light every two minutes as opposed to constantly, however it does not <br> //work. <br> } //close else <br>} //close void loop
Bigger pic
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.
Sweet 'able. I got it working. Thank you very much :)
Hi, your tutorial looks great and I'm going to try it soon. <br>I don't have the components yet, but for now I'm trying to understand the code. <br><br>In the first for loop, the LED slowly fades from green to red right?<br>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?<br><br>In other words, shouldn't the 3rd and 2nd for loops need to switch places so that the color fading is in order?<br><br>This is what I understand from the code. Correct me if i'm wrong.<br><br><br>
oh wait nevermind i got it now. i didn't realize that all three pins are initially set to high
Great!!!!!
Thanks !
OK, but your program is short and verry efficient.<br>Realy smooth color changing.<br>Realy nice.<br>Best WE,<br>Jean.<br>
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.<br><br><br>
Thanks !

About This Instructable

198,560views

89favorites

License:

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