# RGB LED (Full-Color Light Emitting Diode)

## What is RGB LED
RGB LED is an LED that can emit red (Red), green (Green), and blue (Blue) light. By mixing these three colors, various colors of light can be produced.
### RGB LED Features
- **Full-Color Display**: Can mix any color
- **Independent Control**: Each color channel can be independently controlled for brightness
- **PWM Dimming**: Smooth color transitions can be achieved through PWM control
- **Wide Applications**: Commonly used for decorative lighting, status indication, displays, etc.
## Pin Description
RGB LED typically has 4 pins, divided into two types:

### Common Anode
- **Common Anode (+)**: 1 pin, connected to power positive (VCC)
- **R (Red)**: Controls the cathode of red LED
- **G (Green)**: Controls the cathode of green LED
- **B (Blue)**: Controls the cathode of blue LED
### Common Cathode
- **Common Cathode (-)**: 1 pin, connected to GND
- **R (Red)**: Controls the anode of red LED
- **G (Green)**: Controls the anode of green LED
- **B (Blue)**: Controls the anode of blue LED
**Note**: The RGB LED in this kit is **Common Cathode** type.
## Current Limiting Resistor
Each color channel requires an **independent current limiting resistor** (typically 220Ω - 1KΩ), cannot be shared.
### Why Current Limiting Resistor is Needed
RGB LED internally contains three independent LEDs, each needs current limiting resistor protection, same principle as regular LED.
### Current Limiting Resistor Values
- **5V Power Supply**: Usually use **220Ω - 330Ω** resistor
- **3.3V Power Supply**: Usually use **100Ω - 220Ω** resistor
## Connection Method
### Breadboard Connection Diagram (Common Cathode, Direct Visual Effect)

### Connection Instructions (Common Cathode, Direct Visual Effect)
According to the diagram above, connection order is:
```
RGB LED Common Cathode → GND
VCC (5V) → Switch1 → 220Ω resistor → R pin
VCC (5V) → Switch2 → 220Ω resistor → G pin
VCC (5V) → Switch3 → 220Ω resistor → B pin
```
**Working Principle**:
- Close switch1, red LED lights up
- Close switch2, green LED lights up
- Close switch3, blue LED lights up
- Close multiple switches simultaneously to mix colors (e.g., close R and G together to display yellow)
**Key Points**:
- Common cathode connected to GND
- Each color channel (R, G, B) needs independent current limiting resistor
- VCC connects to R/G/B pins through switches and resistors
- Control each color channel on/off through switches to directly see color effects
## Arduino Example Code
### Basic Example: Display Different Colors
```cpp
// Define pins connected to RGB LED (Common Cathode)
const int redPin = 9; // R pin
const int greenPin = 10; // G pin
const int bluePin = 11; // B pin
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Red
setColor(255, 0, 0);
delay(1000);
// Green
setColor(0, 255, 0);
delay(1000);
// Blue
setColor(0, 0, 255);
delay(1000);
// White (all colors mixed)
setColor(255, 255, 255);
delay(1000);
}
// Set color function (Common Cathode: larger value = brighter)
void setColor(int red, int green, int blue) {
analogWrite(redPin, red); // Common cathode uses directly
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
```
### Connection Method
- **RGB LED Common Cathode** → **GND**
- **Arduino Pin 9** → 220Ω resistor → **R pin**
- **Arduino Pin 10** → 220Ω resistor → **G pin**
- **Arduino Pin 11** → 220Ω resistor → **B pin**
### Code Explanation
- `analogWrite(pin, value)`: PWM output, value range 0-255
- Common Cathode RGB LED: larger value = brighter, use `analogWrite(pin, color)` directly
- 0 = darkest (off), 255 = brightest
### Advanced Example: Rainbow Gradient Effect
```cpp
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// Rainbow gradient effect
for (int i = 0; i < 256; i++) {
setRainbowColor(i);
delay(10);
}
}
void setRainbowColor(int hue) {
int r, g, b;
if (hue < 85) {
r = 255 - hue * 3;
g = hue * 3;
b = 0;
} else if (hue < 170) {
r = 0;
g = 255 - (hue - 85) * 3;
b = (hue - 85) * 3;
} else {
r = (hue - 170) * 3;
g = 0;
b = 255 - (hue - 170) * 3;
}
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
```
## Common Questions and Answers
### Q1: RGB LED Not Lighting Up?
- Check if common pin is correctly connected to GND (Common Cathode)
- Check if current limiting resistor for each color channel is connected
- Check if R, G, B pins are correctly connected to Arduino
- Confirm using PWM pins (pins marked with `~`)
### Q2: Color Display Incorrect?
- Check if R, G, B pins are connected incorrectly
- RGB LED in kit is Common Cathode type, code uses `analogWrite(pin, color)` directly
- If using Common Anode RGB LED, need to invert: `analogWrite(pin, 255 - color)`
### Q3: How to Determine Common Anode or Common Cathode?
- **Common Anode**: Common pin connected to VCC, R/G/B pins connected to low level (0) when LED lights up
- **Common Cathode**: Common pin connected to GND, R/G/B pins connected to high level (255) when LED lights up (type in kit)
- Can test with multimeter or check datasheet
## Safety Precautions
⚠️ **Important Notes**:
- Each color channel must use current limiting resistor
- Do not exceed maximum current (typically 20mA per channel)
- Use PWM pins for control to achieve color mixing and brightness adjustment
## Practical Applications
RGB LEDs are commonly used for decorative lighting, status indication, displays, ambient lighting, etc. With Arduino, you can create rainbow lights, breathing lights, music visualization, color following, and other creative projects.