# Project - Disco Light using RGB LED
In this chapter, we’ll dive into an awesome STEM project: creating a disco light with RGB LEDs that flash in random colors! Get ready to combine electronics, coding, and creativity as we use a microcontroller to power up some dazzling light effects.
# RGB LED
RGB LEDs have three individual LEDs inside—red, green, and blue. These LEDs usually share a common connection, which can either be a cathode (negative terminal) or an anode (positive terminal). This means RGB LEDs are categorized as either common anode (shared positive terminal) or common cathode (shared negative terminal).
In this project, we are using a common cathode (shared negative terminal) RGB LED. For a **common cathode LED**, all three LEDs share a single cathode connection (negative), and the individual anodes (positive terminals) control the red, green, and blue LEDs separately.
## Anode and Cathode
- **Anode:** The positive terminal of an LED, where current flows into the LED.
- **Cathode:** The negative terminal of an LED, where current flows out of the LED.

You might think a single resistor on the common pin is sufficient, but it’s best to use **three separate resistors**, one for each color. This ensures safe operation, consistent brightness, and accurate color mixing. Without individual resistors, the LEDs’ brightness won’t be properly balanced, leading to inaccurate color blending.
# Arduino Code
The following code first displays red, green, and blue to verify that the RGB LED is working properly. In the loop, the RGB LED shows a random color every second.
```cpp
const int redLed = 2;
const int greenLed = 3;
const int blueLed = 4;
void setRGBLed(bool isRedOn, bool isGreenOn, bool isBlueOn) {
digitalWrite(redLed,isRedOn);
digitalWrite(greenLed,isGreenOn);
digitalWrite(blueLed,isBlueOn);
delay(1000); // Delay 1 second
}
void setup() {
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(blueLed,OUTPUT);
setRGBLed(true,false,false); //Red
setRGBLed(false,true,false); //Green
setRGBLed(false,false,true); //Blue
setRGBLed(false,false,false); //off
}
void loop() {
//Random Color
bool isRedOn = random(0,2); // Generates 0 or 1
bool isGreenOn = random(0,2);
bool isBlueOn = random(0,2);
setRGBLed(isRedOn, isGreenOn, isBlueOn);
}
```
# Code Explanation
```cpp
const int redLed = 2;
const int greenLed = 3;
const int blueLed = 4;
```
The variables redLed, greenLed, and blueLed are assigned to the digital pins 2, 3, and 4, respectively. These pins will control the Red, Green, and Blue LEDs of the RGB LED.
```cpp
void setRGBLed(bool isRedOn, bool isGreenOn, bool isBlueOn) {
digitalWrite(redLed, isRedOn);
digitalWrite(greenLed, isGreenOn);
digitalWrite(blueLed, isBlueOn);
delay(1000); // Delay 1 second
}
```
The setRGBLed function is used to control the state of the RGB LED. It takes three parameters: isRedOn, isGreenOn, and isBlueOn, which are bool values representing whether the respective LEDs should be on (true) or off (false).
```cpp
digitalWrite(redLed, isRedOn);
digitalWrite(greenLed, isGreenOn);
digitalWrite(blueLed, isBlueOn);
```
This function controls the corresponding LED based on the values passed (true or false), instead of using HIGH and LOW. In this case, true and HIGH both represent 1, while false and LOW both represent 0.
```cpp
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(blueLed,OUTPUT);
```
In the setup() function, the pinMode function is used to set the pins for the RGB LEDs (redLed, greenLed, and blueLed) as **OUTPUT** so that they can send signals to control the LEDs.
```cpp
setRGBLed(true, false, false); // Red
setRGBLed(false, true, false); // Green
setRGBLed(false, false, true); // Blue
setRGBLed(false, false, false); // Off
```
This is initial LED Test. The setRGBLed function is called four times in a sequence to turn Red, Green ,Blue on and then off. Each of these calls has a 1-second delay between them due to the delay(1000) in the setRGBLed function.
```cpp
random(0, 2);
```
The random() function generates random numbers.
```cpp
long random(long min, long max);
```
**min**: The smallest value in the range (inclusive).
**max**: The largest value in the range (exclusive).
In this case, random(0, 10) generates a random integer between 0 and 9 (inclusive of 0 but exclusive of 10).
```cpp
bool isRedOn = random(0, 2); // Generates 0 or 1
bool isGreenOn = random(0, 2);
bool isBlueOn = random(0, 2);
setRGBLed(isRedOn, isGreenOn, isBlueOn);
```
The loop() function runs repeatedly, generating a random combination of red, green, and blue light. The random(0, 2) function generates either 0 or 1, representing **off** (0) or **on** (1). isRedOn, isGreenOn, and isBlueOn are assigned random values (either 0 or 1). Since loop() runs repeatedly, the RGB LED will change to a new random color every second, as the random values change on each iteration.
Project - Disco Light using RGB LED
RELATED ARTICLES
Photoresistor Sensor
Project - Temperature Measurement
NTC Temperature Sensor
Project - IR Sender
Project - IR Receiver
Remote infrared Sensor
Project - Motion Detection Alarm
- Choosing a selection results in a full page refresh.