How to interface KY-016 RGB LED & Create different colors

How to use RGB led with Arduino and how RGB led works are some questions that may arise in your mind while programming an Arduino.

Hello guys! Welcome back to my blog today. We are going to see about RGB LED along with KY 016 RGB LED module.

What is RGB led??

rgb led Arduino

RGB led are a combination of red, green, and blue in one package used to produce millions of different varieties of colors. As we know RED, BLUE, and GREEN are primary colors, by mixing these three colors at different intensities we can achieve our desired color.

By using primary color (ADDITIVE COLOR) we can obtain secondary color

RED + GREEN = YELLOW

RED + BLUE = MAGENTA

BLUE + GREEN = CYAN

RED + BLUE + GREEN = WHITE

Similarly, when using secondary color (SUBTRACTIVE COLOR) we can obtain secondary color

MAGENTA + YELLOW = RED

MAGENTA + CYAN = BLUE

CYAN + YELLOW = GREEN

MAGENTA + YELLOW + CYAN = BLACK

For example – if we want red, then we will keep the pin of red led to high intensity and the other two pins to low intensity. If we want yellow then we will adjust the light intensity of red and green pins to the appropriate proportion to produce yellow color as yellow color is a combination of red and green color.

Common Cathode RGB LED & Common Anode RGB LED

We commonly use a common cathode configuration, which means all negative pins should have common ground while changing positive voltage at the desired proportion of each LED pin to get various different shades of color patterns. Here to achieve different combinations of color we use the analog write function, the problem with digital write is that we can only give 0v to 5v means HIGH or LOW. So we use analog Write to mix different primary colors (red, green, blue) and create new thousands of color combinations.

Parts Required

  •  KY-016 (RGB sensor module)
  • Arduino
  • Jumper wires

Schematic Diagram

rgb sensor Arduino

Basic code for RGB led with Arduino

Code -

int RED_LED=3;          // set red pin of led to pin 3
int Green_LED=5;        // set green pin of led to pin 5
int Blue_LED=6;         // set blue pin of led to pin 6
void setup() {
 pinMode(RED_LED,OUTPUT);          // declare red pin of led as output
 pinMode(Green_LED,OUTPUT);        // declare green pin of led as output
 pinMode(Blue_LED,OUTPUT);         // declare blue pin of led as output
}
void loop() {
 red();                          // calling red led function
 delay(300);                     // delay of 300 millisecond
 green();                        // calling green led function
 delay(300);      
 blue();                         // calling blue led function
 delay(300); 
}
 
void red()                       // creating function to glow red light
{
 digitalWrite(RED_LED,HIGH);      // red light turn on
 digitalWrite(Green_LED,LOW);     // green light turn off
 digitalWrite(Blue_LED,LOW);      // blue light turn off 
  }
 void green()                       // creating function to glow green light
{
 digitalWrite(RED_LED,LOW);        // red light turn off
 digitalWrite(Green_LED,HIGH);     // green light turn on
 digitalWrite(Blue_LED,LOW);       // blue light turn off 
  } 
              
  void blue()                      // creating function to glow blue light
{
 digitalWrite(RED_LED,LOW);        // red light turn off
 digitalWrite(Green_LED,LOW);      // green light turn off
 digitalWrite(Blue_LED,HIGH);      // blue light turn on 
  }  

First initialize pins of RGB led module Connect ground pin of RGB to ground pin of Arduino, VCC pin to 5v and red pin to 3, green pin to 5, blue pin to 6. We will declare led pins as OUTPUT. Also, we configure the baud rate to 115200.

In the red function, we have written code to only turn on the red led pin keeping the other two pins green and blue led off. Similarly in green () and blue (). We have written code to turn green and blue led on.

ky016 rgb led Arduino

Controlling RGB led using user input in serial monitor??

In this, we are using input from users to change the color of the RGB led. First, we have to print the text “choose the color”. Here we have used the while loop to see if the user has entered any text. If the user has put no input at serial monitoring then while loop is not broken until there is an input from the user.

We will get the value entered by the user using Serial.read() function and store it in variable name color. Here we are using the switch case conditions to compare the conditions with user input. If the user enters ‘r’ then the red light will glow, if the user enters ‘g’ then the green light will glow, light glowing, if the user enters ‘b’ then the blue light will glow, if the user enters ‘m’ then magenta light will glow, if the user enters ‘y’ then yellow light will glow, if the user enters ‘c’ then cyan light will glow, if the user enters ‘w’ then white light will glow.

Code -

int RED_LED=3;           // set red pin of led to pin 3
int Green_LED=5;         // set green pin of led to pin 5
int Blue_LED=6;          // set blue pin of led to pin 6
char color;
void setup()
{
 pinMode(RED_LED,OUTPUT);          // declare red pin of led as output
 pinMode(Green_LED,OUTPUT);       // declare green pin of led as output
 pinMode(Blue_LED,OUTPUT);        // declare blue pin of led as output
 Serial.begin(115200);            // set a baud rate of 115200
}
void loop()
{
Serial.println("Choose the color");  // print the message 
while(Serial.available()==0)         // while loop will not break until there is user input
{
  
}
color = Serial.read();             // Read values from user input and store it in variable  
//Serial.print(color);
switch (color) {                  // using switch case condition to check user input
      case 'r':                    // if user presses r then led will emit red color 
        analogWrite(RED_LED,255);
        analogWrite(Green_LED,0);
        analogWrite(Blue_LED,0);
        Serial.println("red");
        break;                     // breaks the condition if satisfies
      case 'g':                      // if the user presses g then led will emit green color
         analogWrite(RED_LED,0);
        analogWrite(Green_LED,255);
         analogWrite(Blue_LED,0);
         Serial.println("green");
        break;
      case 'b':                    // if the user presses b then led will emit blue color
         analogWrite(RED_LED,0);
        analogWrite(Green_LED,0);
         analogWrite(Blue_LED,255);
         Serial.println("blue");
        break;
      case 'w':                     // if the user presses w then led will emit white color
        analogWrite(RED_LED,255);
        analogWrite(Green_LED,255);
         analogWrite(Blue_LED,255);
         Serial.println("White");
        break;


     case 'y':                              // if the user presses y then led will emit yellow color
        analogWrite(RED_LED,240);
       analogWrite(Green_LED,200);
      analogWrite(Blue_LED,0);
       Serial.println("yellow");
        break;
      case 'm':                          //, if user presses m then led, will emit a magenta color
         analogWrite(RED_LED,240);
          analogWrite(Green_LED,0);
          analogWrite(Blue_LED,200);
          Serial.println("magenta");
        break;

        case 'c':                      // if the user presses c then led will emit a cyan color
        analogWrite(RED_LED,0);
        analogWrite(Green_LED,240);
        analogWrite(Blue_LED,200);
        Serial.println("cyan");
        break;  
      default:                          // by default all Led will be turned off
        analogWrite(RED_LED,0);
        analogWrite(Green_LED,0);
         analogWrite(Blue_LED,0);
        }
delay(2000);                            // delay of 2 sec

}

Output -


Controlling RGB led using 3 push buttons for different colors??

First initialize pins of RGB led module Connect ground pin of RGB to ground pin of Arduino, VCC pin to 5v and red pin to 3, green pin to 5, blue pin to 6. We have set the red button pin to 8, we have set the green button pin to 9, and we have set the blue button pin to 10. We have also declared variables for reading push button values.


Code -

int RED_LED=3;                          // set red pin of led to pin 3
int Green_LED=5;                        // set green pin of led to pin 5
int Blue_LED=6;                        // set blue pin of led to pin 6
int button_red_pin=8;                  // set button pin to pin 8
int button_green_pin=9;                // set button pin to pin 9
int button_blue_pin=10;                // set button pin to pin 10
int redcolor;                          // declaring variables
int greencolor;
int bluecolor;
int R = 0;                             // initialising variable
int G = 0;
int B = 0;
void setup()
{
 pinMode(RED_LED,OUTPUT);                // declare red pin of led as output
 pinMode(Green_LED,OUTPUT);              // declare green pin of led as output
 pinMode(Blue_LED,OUTPUT);               // declare blue pin of led as output
 pinMode(button_red_pin,INPUT);          // declare red button pin of led as input
 pinMode(button_green_pin,INPUT);        // declare green button pin of led as input
 pinMode(button_blue_pin,INPUT);         // declare blue button pin of led as input
 Serial.begin(115200);                    // set a baud rate of 115200
}
void loop()
{
 redcolor=digitalRead(button_red_pin);       // reading values of red button and storing in variable
 greencolor=digitalRead(button_green_pin);    // reading values of green button and storing in variable
 bluecolor=digitalRead(button_blue_pin);     // reading values of blue button and storing in variable
 if(redcolor == HIGH)          // check the condition if red button pressed
 {
  delay(300);                 
  R = R + 5;                  // increment the value of r by 5
  if(R>255){                  // increment till value of r reaches 255
    R= R - 5;                 // decrement the value by 5
    }
 }
if(greencolor == HIGH)      // check the condition if green button pressed
 {
 delay(300);               
 G = G + 5;                   // increment the value of g by 5
 if(G>255){                  // increment till value of g reaches 255
    G = G - 5;               // decrement the value by 5
    }
 }
 if(bluecolor == HIGH)           // check the condition if blue button pressed
 {
 delay(300);
 B = B + 5;                       // increment the value of b by 5
 if(B>255){                       // increment till value of b reaches 255
    B =  B - 5;                   // decrement the value by 5
    }
 }
 
analogWrite(RED_LED,R);            // passing value of r in analog write function
Serial.println(R);
analogWrite(Green_LED,G);          // passing value of g in analog write function
Serial.println(G);
analogWrite(Blue_LED,B);          // passing value of b in analog write function
Serial.println(B);
}

We will declare led pins as OUTPUT & push buttons as INPUT. Also, we configure the baud rate to 115200.

We are reading the status of red, blue, and green buttons and storing them in variable names red color, green color & blue color.

We have to use the if condition to check whether the red button, blue button, or green button is pressed.

If conditions are satisfied we will increment the value of r by 5 each time when the user presses the red button.

The value of r will get increment until the value of r reaches 255. If the value of r exceeds 255 then the value of r will begin to get decrement by 5.

Similarly, the value of g & b will increment by 5 each time when the user presses the button. The value will g and b continue to increment until it reaches 255. When it reaches 255 the value of g and b starts to decrement by 5.

Conclusion – 

Today in this blog we learn about RGB led, how RGB led works, the concept of additive and subtractive color, and how we can generate different colors using primary colors. We also learn how to use RGB led sensor module with Arduino.


"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu