An RGB led is basically 3 LEDs in the one package, a red, green and blue one which either share a common cathode or a common cathode. Using one of these LEDs you can create a variety of different colours simply by switching on and off various LEDs, PWM which we will discuss later also works well here. Lets look at an RGB LED
You will need the following, I used a low cost RGB LED breakout.
1 Arduino UNO
1 RGB LED
1 1k resistor
Some connection wire
1 Breadboard
The connection is fairly straightforward we simply connect the common anode to VCC via the resistor and the cathodes are connected to 3 digital pins , you can see these clearly in the code example below
[codesyntax lang=”cpp”]
int redPin = 11; int greenPin = 10; int bluePin = 9;
[/codesyntax]
Here is the schematic
Now as this is a common anode type, a high on the digital pin will switch the LED off and a low will switch the LED on. If you had a common cathode you would need to reverse the values. Lets show you what I mean.
We use the following
[codesyntax lang=”cpp”]
setColour(0, 255, 255); // red
[/codesyntax]
Which in turn calls the following function
[codesyntax lang=”cpp”]
void setColour(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }
[/codesyntax]
As you can see from the example above the red LED is on (low), the green and blue LED is off. So you will see red. You can in fact put a variety of values in to see different colours on the RGB led
Code
[codesyntax lang=”cpp”]
int redPin = 11; int greenPin = 10; int bluePin = 9; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { setColour(0, 255, 255); // red delay(1000); setColour(255, 0, 255); // green delay(1000); setColour(255, 255, 0); // blue delay(1000); setColour(0, 80, 0); // purple delay(1000); setColour(255, 0, 0); // aqua delay(1000); setColour(0, 0, 0); // white delay(1000); } void setColour(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }
[/codesyntax]
Links
10PCS/LOT WS2812 RGB LED Breakout Module For Arduino
100 x Emitting Diodes RGB Led 5mm led diode