Fading RGB LED (Arduino)

210,973

93

34

Posted in TechnologyArduino

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.

2 People Made This Project!

Recommendations

  • Spotless Contest

    Spotless Contest
  • Microcontroller Contest

    Microcontroller Contest
  • Space Challenge

    Space Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

34 Comments

int red = 6;
int green = 3;
int blue = 5;

void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
digitalWrite(red,HIGH);
digitalWrite(green,HIGH);
digitalWrite(blue,HIGH);
}

void loop()
{
change(red, blue);
change(blue, green);
change(green, red);
}

void change(int color1 , int color2 )
{
for (int i =0; i<255; i++)
{
analogWrite(color1, i);
analogWrite(color2, 255 - i);
delay(20);
}
}

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

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);

}

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);

}

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

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?

Thanx for sharing;

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

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!

#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 );

}

}

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

6 replies

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.
in my schematic i used a common anode one because i only have common anode ones.

No Problem :)
What do you think of my instructable ?
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.