Testing RGB LEDs and the KY009 Three Colour LED.

The code is available on my GitHub page here.

Some versions of this board have resisistors in series with the LEDs, some do not. If resistors are not present then use a 390R resistor in series to limit the current.

I was not very impressed with this RGB LED board. It is nothing more than a three colour LED on a PCB, which is not a problem, however neither the Blue or the Red LEDs would work with PWM. This means that it’s not possible to achieve full colour mixing, although some limited mixing can be achieved. I tried a second board and on this one the Red and Blue LEDs would dim with PWM, but the green LED simply switched on and off.

I decided to write a sketch that could be used to test RGB LEDs with an Arduino. This sketch has two parts, the first switches LEDs on and off to provide a rainbow of colours, Red, Green, Yellow, Cyan, Blue and Violet. The second part tests the LEDs ability to fade in and out and each LED is tested separately. Either of the two routines can be switched off by setting either Of the variables rainbow or fader to false.

The LED in its naked form does not give very good colour mixing, so I made a simply diffuser by first cutting a ping pong ball in half then glueing some thin greasproof paper onto the open part of the ball. Place this dome (paper side down) over the RGB LED. An example of the diffuser is shown here.

Connect the common pin to pin 2, the Red LED to pin 3, the Green LED to pin 4 and the Blue LED to pin 5.



/*
RGB LED Test
This sketch checks a RGB LED to ensure that PWM can be used
to produce a complete range of colours
For best colour mixing results use a diffuser
*/
int gndPin = 2; // the RGB LED common connection
int redLed = 3; // the Red LED
int greenLed = 4; // the green LED
int blueLed = 5; // the blue LED
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
//
// set either of these to false to disable the effect
boolean rainbow = true; // enable the rainbow display
boolean fader = true; // enable the fade display
//
void setup() {
// declare pin 2 and 3 to be an output:
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}

void loop(){
if (rainbow){
// Rainbow

for(int r=0; r<3; r++){
analogWrite(redLed, 255); // Red
delay(2000);
analogWrite(greenLed, 255); // Yellow
delay(2000);
analogWrite(redLed, 0); // Green
delay(2000);
analogWrite(blueLed, 128); //Blue/Green
delay(2000);
analogWrite(blueLed, 255); //Blue/Green
analogWrite(greenLed, 0); //Blue
delay(2000);
analogWrite(redLed, 255); // Violet
delay(2000);
analogWrite(redLed, 0); // LEDs all OFF
analogWrite(blueLed, 0);
}
}
// the fader effect
if(fader){
brightness = 0;
fadeAmount = 5;
redLedLoop();
analogWrite(redLed, 0);
//
brightness = 0;
fadeAmount = 5;
greenLedLoop();
//
analogWrite(greenLed, 0);
//
brightness = 0;
fadeAmount = 5;
blueLedLoop();
analogWrite(blueLed, 0);
}
}

// the fade routines
//
void redLedLoop() {
for(int i=0; i<255; i++){
analogWrite(redLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

void greenLedLoop(){
//
for(int i=0; i<255; i++){
analogWrite(greenLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

void blueLedLoop(){
for(int i=0; i<255; i++){
analogWrite(blueLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s