The RGB LED can emit different colors by mixing the 3 basic colors red, green and blue. So, it actually consists of 3 separate red, green and blue LEDs, packaged in one box. This is why it has 4 leads, one for each of the 3 colors and a common cathode or anode depending on the type of RGB LED. In this tutorial I will use a common cathode.
Components required for this Project :
int ledPin = 9;
int ledPin1 = 10;
int ledPin2 = 11;
void setup() {}
void loop()
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
}
The code has used the fundamental properties of mixing RGB Colors and Producing different Colors.
Here is the table of RGB color mixing.
Components required for this Project :
Circuit Diagram
Above is the circuit diagram of the RGB Led and Arduino Uno,
As you can see the ground pin of the RGB LED is connected to the ground of the Arduino and other 3 pins of RGB Led are connected to the PWM Pins of Arduino Uno (9, 10, 11).
Code
int ledPin = 9;
int ledPin1 = 10;
int ledPin2 = 11;
void setup() {}
void loop()
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
analogWrite(ledPin, fadeValue);
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
delay(20);
}
}
The code has used the fundamental properties of mixing RGB Colors and Producing different Colors.
Here is the table of RGB color mixing.
By changing the value in the variable fadevalue we can create various colors by simultaneously turning ON leds together.
Hope you Like This post.
Comments
Post a Comment