Arduino Starter’s Guide  (5/7): Colorful LED (RGB LED)

Colorful Light

We’ve blinked an LED and controlled eight in sequence now it’s time to control color. Using an RGB LED (actual 3 LEDs in a single housing) we can generate any color our heart desires. We do this through color mixing, what’s required is delving back to your elementary art days of playing with colored cellophane to produce different colors (if you can’t remember that far back don’t worry here’s a color wheel to help you out).

Hardware Used:

  • 1 – RGB LED
  • 3 – 100 ohms

  • 1 – Arduino UNO
  • 
    
    

    You can buy all this Hardware at Createlabz.

    Software Used:

    Arduino IDE

    Set up the Hardware:

    Code:

     

    int ledDigitalOne[] = {9, 10, 11};
    
    const boolean ON = LOW;     
    const boolean OFF = HIGH;   
    
    
    const boolean RED[] = {ON, OFF, OFF};    
    const boolean GREEN[] = {OFF, ON, OFF}; 
    const boolean BLUE[] = {OFF, OFF, ON}; 
    const boolean YELLOW[] = {ON, ON, OFF}; 
    const boolean CYAN[] = {OFF, ON, ON}; 
    const boolean MAGENTA[] = {ON, OFF, ON}; 
    const boolean WHITE[] = {ON, ON, ON}; 
    const boolean BLACK[] = {OFF, OFF, OFF}; 
    
    
    
    void setup(){
      for(int i = 0; i < 3; i++){
       pinMode(ledDigitalOne[i], OUTPUT);   
      }
    }
    
    void loop(){
    
    
       setColor(ledDigitalOne, CYAN);   
    
       //randomColor();
    
    }
    
    void randomColor(){
      int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
      setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
      delay(1000);
    }
    
    void setColor(int* led, boolean* color){
     for(int i = 0; i < 3; i++){
       digitalWrite(led[i], color[i]);
     }
    }
    
    void setColor(int* led, const boolean* color){
      boolean tempColor[] = {color[0], color[1], color[2]};
      setColor(led, tempColor);
    }

     

    Code Breakdown:

     

    int ledDigitalOne[] = {9, 10, 11};

    pinouts for the RGB LED.

    const boolean RED[] = {ON, OFF, OFF};

    The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable “read-only“. This means that the variable can be used just as any other variable of its type, but its value cannot be changed.

    for(int i = 0; i < 3; i++){
       pinMode(ledDigitalOne[i], OUTPUT);

    that’s a loop that says, okay, for every time that i is smaller than 3, I’m going to do whatever is in the code block. Whenever i reaches 3, It will stop.

    Note : Some codes were explained in the previous activity go and check it out!

    Try More!!!

    More Colors:

    I imagine you are less than impressed by the cyan glowing LED before you. To display different color change the color in the code to one of the others.

    setColor(ledDigitalOne, CYAN); ------> setColor(ledDigitalOne, --NEW COLOR--);

     

    Display a Random Color:

    Of course we can do more than display a constant color to see how we cycle through random colors change the loop() code to.

    void loop(){   
       //setColor(ledDigitalOne, CYAN);       
         randomColor() 
    }

     

     

    The post Arduino Starter’s Guide (5/7): Colorful LED (RGB LED) appeared first on Createlabz.

    ArduinoArduino kitBasicColorColor ledColorfulKnowledgebaseLedRgbStarter kit

    Leave a comment

    All comments are moderated before being published