bclose

11 – THE RGB LED DIODES

Objectives

 

 

    • Enhance the experience with the PWM pins.
    • Introduce the RGB LED diodes.
    • Introduce the random() function.

 

Bill of materials

Arduino Uno  Arduino Uno or equivalentWe can use any other Arduino board in this chapter.


We will also need a PC computer with the Arduino IDE properly installed and set up.

Breadboard  A solderless Breadboard.
RGB LED diode

RGB LED diode in a Keyes mount

A stand alone LED RGB or in a Keyes module
330 Ohm Resistor A 330 Ohm resistor
Jumper wires Some jumper wires

The RGB LED diodes

 

So far we have used LED diodes, but always in a defined color. Usually the reds and yellows are the easiest to get, but can also be purchased in blue, green and even white color. There are usually no major differences between them except in color.

But sometimes it is interesting to have a pilot light that changes color depending on some conditions. For example, a green light is usually identified with an OK signal, while a red light indicates problems and a yellow light may indicate something in between.

Using multiple diodes to do this is cumbersome and complicates design, so it would be nice to have a LED diode to which we can indicate what color we want to show. This is what a RGB LED diode in fact does.

Those accustomed to computer graphic design will be already familiar with the idea that any color can be created on the screen with the mixture of three basic colors:

     Red

     Green

     Blue

This is exactly what RGB stands for, Red Green Blue. It is one of these acronyms that continually arise in image, TV, etc.

An RGB LED diode is in fact the union of three LED diodes from the basic colors in a common encapsulation that share the ground terminal or common cathode (cathode is another name for the negative terminal).

Depending on the voltage applied on each lead we can get the desired color mixture with relative simplicity.

 
  • Those who have drawn with crayons or watercolors, will find it strange the mixtures of colors showed above. This is because when painted on white paper, color mixing is subtractive: If we mix the three colors we get black, or at least something dark.
  • However when we paint with light directly, the color mixing is additive and we obtain white by mixing the three basic colors. The rules of color mixing in both cases are opposite.

 

We are going to assemble a little prototype that let us choose the color emitted by one of these RGD LED diodes.

CIRCUIT SCHEMATIC DIAGRAM

 

Chapter 11. RGB LED diode Fritzing diagram

The assembly simply involves connecting the negative lead (the longest lead) to ground via a resistor to limit the current intensity and then identify which lead corresponds to each color:

 
  • The longest lead in these RGB LED diodes is ground, GND.
  • Next to GND there are two leads at one side and one alone at the other side. Normally the lonely lead is the red lead, R.
  • So the pin-out of an RGB LED diode is usually R, GND, G, B.

 

Anyway we should make sure of reading the manufacturer’s specifications or identifying each lead. To identify them just connect the GND lead to our Arduino and test each lead independently to see which color produces.

 
  • If the RGB LED diode is assembled in a Keyes module, you won’t have to do that because all the leads are labeled and the GND lead will be labeled as “-“.

 

Chapter 11, RGB LED wiring diagram

Attention, contrary to the norm, in this case the red wire does not indicate Vcc voltage, that is, +5V but the red LED lead.

In this schematic we have used the pins 9, 10 and 11 of our Arduino. We can use others, but assure first they can use PWM (those with the tilde, ~) to apply different intensities.

RGB CONTROL PROGRAM

 

Since our idea is to mix the tonalities of the RGB components to generate different shades of colors, it seems to be a good idea to write a function to do this mixture so we can use it in an abstract and practical way (in addition to encapsulating a curious utility , which we can use in future examples and on the way insist on the concept of function).

Sketch 11.1

First we should define inside the setup() function which pins are we going to use:

void setup()
   {
       for (int i =9 ; i<12 ; i++)
            pinMode(i, OUTPUT);
   }

And after that we could type a function like this:

void Color(int R, int G, int B)
    {     
        analogWrite(9 , R) ;   // Red    
        analogWrite(10, G) ;   // Green 
        analogWrite(11, B) ;   // Blue 
    }

This way it would be easy to call Color(0,255,0) to obtain the green color. In fact let’s start by making sure that we have correctly identified all the leads, writing the following sketch:

void loop()
   {    Color(255 ,0 ,0) ;
        delay(1000); 
        Color(0,255 ,0) ;
        delay(1000);
        Color(0 ,0 ,255) ;
        delay(1000);
        Color(0,0,0);
        delay(2000);
   }

This sketch should produce the following sequence: red, green, blue, off and back again.

We should make sure that we have correctly identified the RGB LED leads, because otherwise, subsequent mixtures of colors will not be what we expect.

Let’s see how to find out which mixture of RGB colors do we need to obtain a certain color. The Windows users have the Paint application located in the menu Accessories, and Mac or Linux users have similar tools to do it.

If you run Paint (or the equivalent) we can use the color editor:

Editing colours in Microsoft Paint

As you click on the button you will see something similar to this:

Edit colours window
If you click on the color area on the right, in the vertical bar will appear the hues next to the one you have selected and you can choose the one you like. Below, on the right, you can see the accurate RGB values to get the desired color.

So to obtain that bluish hue from the picture above we just need to call the Color() function using the following values:

Color(13, 227, 201) ;
 
  • Since Arduino allows us to write values from 0 to 255 in the digital pins, when we use the analogWrite() function, in practice we will have 255 x 255 x 255 different colors or what is the same: 16,581,375 possible colors.
 

The Color() function we have created in this chapter is pretty simple but it is added to others that we have created in previous chapters, so we are creating a small collection of them.

The Arduino development group has also created a lot of functions that are available to be used in our own sketches, but they are so many that it is almost impossible to see all of them in depth.

Only as an example we will introduce the random() function. The random(N) function returns, surprisingly, a random value between 0 and N and it is very useful to generate random colors in our RGB LED diode. Try this out!

Sketch 11.2
    void setup()           
       {
          for (int i =9 ; i<12 ; i++)
               pinMode(i, OUTPUT);
       }

     void loop()
    { 
          Color(random(255), random(255), random(255)) ;
          delay(500);
    }

void Color(int R, int G, int B)
    {
          analogWrite(9 , R) ;   // Rojo
          analogWrite(10, G) ;   // Green 
          analogWrite(11, B) ;   // Blue 
    }

This code will generate a series of pretty psychodelic random colors.

Summary

 

 

    • We already know the utility of a RGB LED diode and how to work with it.
    • We have gone on programming Arduino pins, this time as analog outputs (PWM).
    • We have introduced the random() function which is surprisingly useful in a lot of different situations.