RGB LED

A RGB LED is the same as three LEDs all in one rgb_ledcase.  The one included in the kit is of a type called common cathode, meaning it has one ground pin. It has three anode pins (positive), one for each Red, Green and Blue.

CIRCUIT:

RGB Circuit

To build this circuit push in the RGB led and then connect the longest leg to ground. Attach a 220 Ohm resistor to the other legs bridging the breadboard trench. Using wires, connect the other end of the resistors to pins 9, 10 & 11.

CODE:

In this code we use another control structure called a for loop. It loops the code between the brackets until the condition is meet. In our example we set i to be equal to 9, then we tell it to keep looping while i is < or = to 11. The final part i++, makes the code add 1 to i after every loop. Inside our loop we use i as a variable to set the pinMode and then again later to turn on and off that LED.

Did you notice that we connect each anode of the RGB LED to a pin with the symbol “~” on it? Remember this is PWM and allows us to fade a LED. So lets try some colour mixing with the Serial Monitor.

CODE:

After loading this code, open the Serial Monitor and in the first drop down box before the baud rate change it to NewLine. Now Control the RGB LED by send values to it in the format of “R,G,B”, where R,G & B are between 0 and 255.

In this code we introduce a while loop. It will continually loop until it’s condition is meet. Be careful of setting a condition that will never come true and getting stuck in an endless loop.

constrain makes sure the number is in between it’s boundaries, but unlike  map it will make anything outside its limits equal to its limit. eg 500 would become 255.

Serial.parseInt(); reads the next interger followed by a comma.

Leave a Reply

Your email address will not be published. Required fields are marked *