Introduction: Arduino Examples #1 Make an RGB Led Randomly Flash Different Colors

Picture of Arduino Examples #1 Make an RGB Led Randomly Flash Different Colors

In this instructable I will show you how to get an rgb led to randomly flash different colors using an Arduino. If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.

Step 1: Get the Hardware Required

Picture of Get the Hardware Required

Hardware Required
1-arduino
1-330ohm resistor
1-common cathode or common anode rgb led
1-breadboard
5-jumper wires

Step 2: Wire It Up (Common Cathode)

Picture of Wire It Up (Common Cathode)

First connect a 330ohm resistor between the Arduino's ground and the rgb led's ground. Next connect the red pin to pin 11, green pin to pin 12, blue pin to pin 13. Then go to step 2 to program the Arduino.

Step 3: Program the Arduino (Common Cathode)

Picture of Program the Arduino (Common Cathode)

Now using your computer open up the Arduino software. Then connect your Arduino to your computer. Next copy and paste all the text between the //. Then upload the program to your Arduino and try it out.

// 
int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 11; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 13; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}


//

Step 4: Wire It Up (Common Anode)

Picture of Wire It Up (Common Anode)

First connect a 330ohm resistor between the Arduino's 5v pin and the rgb led's positive pin. Next connect the red pin to pin 11, green pin to pin 12, blue pin to pin 13. Then go to step 5 to program the Arduino.

Step 5: Program the Arduino (Common Anode)

Picture of Program the Arduino (Common Anode)

Now using your computer open up the Arduino software. Then connect your Arduino to your computer. Next copy and paste all the text between the //. Then upload the program to your Arduino and try it out.
//
int ledcolor = 0;
int a = 1000; //this sets how long the stays one color for
int red = 11; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 13; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 51);
delay(a);
analogWrite(red, 255);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, LOW);
delay(a);
digitalWrite(green, HIGH);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, LOW);
delay(a);
digitalWrite(blue, HIGH);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 95);
digitalWrite(green, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(green, HIGH);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(blue, HIGH);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(a);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 155);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(a);
analogWrite(red, 255);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
break;
}

}
// 
If you have any questions, comments or suggestions for other Arduino examples please feel free to leave a comment and I will write back as soon as i can.

Comments

Rahnadal (author)2015-12-23

Thanks

nice application

chrwei (author)2015-02-01

the basic explanations here are good, but this is a dangerous way to wire up an RGB LED. each color is technically a separate LED and their forward voltage specs, and sometimes current as well, are different. You need a correctly sized resistor for each color or you risk burning out a color. the only way this instructable works for others at all is if you happen to have an RGB LED of the exact same specs as the author.

also, with the correct resistors, the color values will be more similar to HTML RGB color wheels, where white is 255,255,255.

keithp1 (author)2014-11-11

when i wired every thing up the red worked and the blue worked but the green gave me an odd red color can someone please help me

keith pitts

chrwei (author)keithp12015-02-01

likely your LED has different specs than this one. really this is the wrong way to wire up an RGB. each color needs its own resistor matched to its forward voltage and spec'd current. google "led calculator" for some tool to help you know what the right resistors are.

Kaptia Teramo (author)2014-10-31

This system works quite well and can easily be modified to have more leds.

biliron (author)2014-06-12

Hi, On the common anode version the pins used for ground but we use the command "digitalWrite" as it is a positive. I am a bit confuse.
May you be able to explain about it ? Thanks.

hshocknasty (author)2013-07-16

thanks for this.

03coupr (author)2012-04-17

Hi, great tutorial. Just a quick question could I get this to work with an FSR (Force sensitive resistor) changing the colours to the amount of pressure is given.

Many thanks

lasko25 (author)03coupr2012-10-21

Yes, you can definitely do this.
Just think of a way to map the values from the pressure sensor on color.
Example > red is 0-100, green is 100-200, blue is 200-300.
That would be static switches, if you want more dynamic color smoothing then it will be a bit trickier, but absolutely doable.

geoffb37 (author)2012-10-19


sketch_oct19a.cpp: In function 'void loop()':
sketch_oct19a:42: error: expected `)' before 'if'
sketch_oct19a:43: error: expected `)' before 'if'
sketch_oct19a:44: error: expected `)' before 'analogWrite'


maewert, your code verifys these errors???
can you assist?

learning curve (author)2012-04-11

Hi there, i was hoping to add a potentiometer to this sketch, i am really new to writing code so please bare with me :) i have used potentiometers before but for controlling servo motors and not LED's, any help would be greatly appreciated

maewert (author)2010-12-02

Nice instructable. 
A quick review of the code seems to show there there are no 'break' statements between the 'case's.  So once the random number picks one case all of the following case statements would be executed as well.  Wouldn't this repeat the same random sequence (i.e. after the yellow is shown it is always followed by cyan, megenta, then white)? 
(Also note that random(6) returns numbers from 0 to 5 but does not return 6 itself.)
This might still look nice but an even more colorful more random display may result if you drove each color using a PWM pin and for each you set to random(255) values like so:


////////////////////////////////////////////////////////////////////////////////////////////
int a = 1000; //this sets how long the LED stays one color for
int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

void setup()
{ //this sets the output pins
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop()
{
  analogWrite(blue, random(255);
  analogWrite(red, random(255);
  analogWrite(green, random(255);
  delay(a);
}
////////////////////////////////////////////////////////////////////////////////////////////

(I didn't compile this so it may be buggy)

Just a thought. 

Best Wishes.

qazwsx755 (author)maewert2010-12-02

I just modified this Instructable by adding the break statements and changed that number 6 to 7 in the code even though it worked fine and didn't repeat the same sequence before without the break statements.Thanks for the advice.

maewert (author)qazwsx7552010-12-04

Cool.

You can aso give this one a try.  It slowly fades betweens random colors:

int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{ //this sets the output pins
 pinMode(red, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(blue, OUTPUT);
 redNow = random(255);
 blueNow = random(255);
 greenNow = random(255);
 redNew = redNow;
 blueNew = blueNow;
 greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()
{
 analogWrite(blue, blueNow);
 analogWrite(red, redNow);
 analogWrite(green, greenNow);
 redNew = random(255);
 blueNew = random(255);
 greenNew = random(255);
// fade to new colors
 while ((redNow != redNew) ||
  (blueNow != blueNew) ||
  (greenNow != greenNew))
 {
  fade(redNow,redNew)
  fade(blueNow,blueNew)
  fade(greenNow,greenNew)
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);
  delay(20);
 }
}
 

Djandco (author)maewert2011-11-02

Hi,
I am very new to the Uno, and to programming for that matter!
Could I ask nicely how you could incorporate a flickering LED to Pin 8?
I don't know how to run two routines without causing one to stop :-(
I can read up on things but its a pity there are no local night classes for absolute beginners!

maewert (author)Djandco2011-11-02

Djandco,

Often we can combine two sample programs by adding what each one has in the Setup and the loop. This only works if the two routines do not 'step on' each other, which I mean that each routine uses different pins and variables, etc.

I don't have any code that implements 'flickering' but I'll try to make something up :-). Two ways come to mind, we can use random numbers to set the brightness and the duration of the LED if we use Pulse Width Modulation like we did for setting the colors of the RGB LED. Second way would be to not use PWM but to simply use random numbers to set the duration of on and off for the LED. Pin 8 is one of the Arduino pins that does NOT support PWM. We'd have to use pin 5 or 6 if we wanted to use PWM. Lets try it with pin 8 and not use PWM.

Give this a shot:!

int red = 11; //this sets the red led pin
int green = 10; //this sets the green led pin
int blue = 9; //this sets the blue led pin
int flickerLED = 8; // flicker on pin 8

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;
int flickertime;

void setup()
{ //this sets the output pins
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(flickerLED,OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x
void loop()
{
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);
redNew = random(255);
blueNew = random(255);
greenNew = random(255);
// fade to new colors
while ((redNow != redNew) ||
(blueNow != blueNew) ||
(greenNow != greenNew))
{
fade(redNow,redNew)
fade(blueNow,blueNew)
fade(greenNow,greenNew)
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);

flickertime = random(20);
digitalWrite(flickerLED,HIGH);
delay(flickertime);
digitalWrite(flickerLED,LOW);
delay(20-flickertime);
}
}

Djandco (author)maewert2011-11-04

Although I have just looked at your code again and those lines are not in it?

When you copy text from the forum and paste it you don't seem to get what you think you were going to get!

So, I must appologise because it was not your code that had the fault it was me.

Thanks

Djandco (author)maewert2011-11-04

AH!
No idea why this worked but it did :-)
I just chuffed off the following

redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;

form the Void Loop as it was already in the Void Setup.
Now all I have to do is understand why it worked!

This is a whole new world :-0

And I have found the door, all I need to do now is go through the door and see how far down the rabbit hole goes.........................................................

Djandco (author)maewert2011-11-04

Thanks meawert,

This is where I come unstuck :-(
The code generates the following:

Maewert.cpp: In function 'void loop()':
Maewert:42: error: expected `)' before 'if'
Maewert:43: error: expected `)' before 'if'
Maewert:44: error: expected `)' before 'analogWrite'
Maewert:53: error: expected `}' at end of input

But I have no idea how to fix it.

Just so I can look things up on the net, what is this code exactly? C? C+? or is it specific like Python?

I honestly do apreciate your time trying to show me the way!

Thanks

AzurusNova (author)maewert2011-01-23

Not a bad little sketch there, now I have to figure out how to build a mosfet setup to bridge the PWM into a 12V system so I can control my room lights now. :P

maewert (author)AzurusNova2011-01-24

Have you checked out the other instructibles like this one:
https://www.instructables.com/id/RGB-Color-Controllable-High-Power-LED-Room-%2b-Spot-/step6/circuit-operation/

I have not worked with high power LEDs but I recently purchased 60 RGB LEDs and I have lots of superbright LEDs left over from other projects.  I'm thinking about doing something like this as well only using a number of individual color LEDs.  Maybe something that would shine behind my flat panel monitors on the rather stark white walls of my office would be nice.  Six LEDs of each color would be 18 LEDs, each taking about 20mA will still be less than 1/2 amp, easily handled with three 2N2222A transistors.  Could easily double the number of LEDs. 

Might be nice to add a bit of color :-)

Yakumo (author)maewert2011-01-01

thanks maewert thats what i was looking for ^^

About This Instructable

134,948views

66favorites

More by qazwsx755:Arduino Examples #2 Use an Arduino as a FTDI ProgrammerArduino Examples #1 Make An RGB Led Randomly Flash Different ColorsHow To Build a simple 1-25 VDC, SPST NO, 0.05A 1-70 VDC  Relay For Under $4
Add instructable to: