Main article: How to use tricolor LED module with Arduino
The KY-016 is capable of producing wide range of different colors by mixing blue, green and red lights. The RGB LED module will not require any limiting resistors. Those resistors are already integrated in the circuit, and so 5V can be directly used as power input.
Wiring schemaTricolor LED module have four pins: one for each RGB color and GND. Analog inputs are used to control the power of the light and must be connected to PWM pins on Arduino (we used D3, D5 and D6). Non PWM pins can also be used to toggle the lights but we will lose the fade effect.
When the RGB pin receives:
- HIGH or 255 the light is fully ON
- LOW or 0 the light is fully OFF
We've defined two functions to increase and decrease the power of the LED light. The code will sequentially and gradually turn the lights ON and then OFF: first the blue, then the green and finally the red one.
#define BLUE_PIN 3
#define GREEN_PIN 5
#define RED_PIN 6
void setup()
{
pinMode(BLUE_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
}
void loop()
{
increaseLight(BLUE_PIN);
decreaseLight(BLUE_PIN);
increaseLight(GREEN_PIN);
decreaseLight(GREEN_PIN);
increaseLight(RED_PIN);
decreaseLight(RED_PIN);
}
void increaseLight(int ledPin)
{
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10);
}
}
void decreaseLight(int ledPin)
{
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10);
}
}
Note:analog values must be in [0-255] range.
Comments