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

138,338

66

22

Published

Introduction: 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

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)

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)

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)

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)

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.

Share

    Recommendations

    • Clocks Contest

      Clocks Contest
    • Woodworking Contest

      Woodworking Contest
    • Planter Challenge

      Planter Challenge
    user

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

    Tips

    Questions

    22 Comments

    Thanks

    nice application

    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.

    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

    1 reply

    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.

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

    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.

    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

    1 reply

    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.


    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?

    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

    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.

    8 replies

    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.

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

    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!

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

    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

    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.........................................................

    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

    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