Control an RGB LED with an Arduino

FunduinoPublished on

Farben mischen mit Pulsweitenmodulation

Aufgabe: Eine RGB-LED soll in verschiedenen Farben leuchten – erst in den drei Grundfarben nacheinander, danach in gemischten Farben.

RGB LED with three current-limiting resistors on a breadboard connected to the Funduino UNO R3
Test setup: RGB LED on the Funduino UNO R3

You need this

RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode — Common Cathode 0,49 € *Item no. F23106750About the product — RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode — Common Cathode
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 5 parts together 17,97 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

What is an RGB LED?

An RGB LED is an LED that can glow in different colors. RGB stands for the colors red, green, and blue. Inside, the LED consists of three individually controlled LEDs that glow in these three colors. That’s why an RGB LED has so many legs—exactly four.

The longest of the four legs is the common cathode (Common Cathode). It is the negative terminal (GND). You control the three shorter legs via the digital pins—they let you select the individual colors.

By mixing the colors, you can create many more colors. For example, if you control blue and green at the same time, you get turquoise.

Color wheel with the mixed colors of red, green, and blue
RGB LED color spectrum

Understanding pulse-width modulation (PWM)

Microcontrollers only know “voltage on” or “voltage off” at their digital outputs. But to create many different colors with an RGB LED, you need to control each individual color more precisely. To do this, use pulse-width modulation (external link, accessed on 09/01/24).

PWM makes the voltage pulse between 5V and 0V - it is switched on and off in milliseconds. With a high PWM value, the 5V signal is applied to the pin almost continuously; with a low PWM value, it is barely present. This produces a similar effect with LEDs to changing the voltage itself.

On the Arduino UNO, PWM only works on the digital pins marked with a small wave. On other microcontrollers, you can use any digital pin.

This is a very compact summary - if you want to dig deeper, it's worth reading more about pulse-width modulation.

Arduino UNO with marked PWM pins, recognizable by the wave symbol
PWM pins on the Arduino UNO are marked with a wave symbol (~)

Three individual colors one after another

Red connects to pin 5, green to pin 6, and blue to pin 3 – all three are PWM pins.

Circuit diagram with RGB LED connected to pins 3, 5, and 6 of the Arduino UNO.
Circuit diagram: RGB LED on the Arduino UNO
Single-color program code - Arduino UNO
int LEDrot = 5;
int LEDgruen = 6;
int LEDblau = 3;
int Pause = 1000;      // Pause of 1000 ms, or 1 second
int Helligkeit = 150;  // Value between 0 and 255 - indicates the brightness of each color
void setup()
{
  pinMode(LEDblau, OUTPUT);
  pinMode(LEDgruen, OUTPUT);
  pinMode(LEDrot, OUTPUT);
}
void loop()
{
  analogWrite(LEDrot, Helligkeit);   // turn on red
  delay(Pause);                      // pause
  analogWrite(LEDrot, LOW);          // turn off red
  analogWrite(LEDgruen, Helligkeit); // turn on green
  delay(Pause);                      // pause
  analogWrite(LEDgruen, LOW);        // turn off green
  analogWrite(LEDblau, Helligkeit);  // turn on blue
  delay(Pause);                      // pause
  analogWrite(LEDblau, LOW);         // turn off blue
}

Download: single-color-program-code-arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Mix colors

Now you turn on two colors at a time: green and red make yellow, green and blue make turquoise, and red and blue make purple.

Color-mixing program code - Arduino UNO
int LEDrot = 5;
int LEDgruen = 6;
int LEDblau = 3;
int Pause = 1000;      // Pause of 1000 ms, or 1 second
int Helligkeit = 150;  // Value between 0 and 255 - specifies the brightness of each color
void setup()
{
  pinMode(LEDblau, OUTPUT);
  pinMode(LEDgruen, OUTPUT);
  pinMode(LEDrot, OUTPUT);
}
void loop()
{
  // green and red on = yellow
  analogWrite(LEDgruen, Helligkeit);
  analogWrite(LEDrot, Helligkeit);
  delay(Pause);
  // green and red off = yellow off
  analogWrite(LEDgruen, LOW);
  analogWrite(LEDrot, LOW);
  // green and blue on = turquoise
  analogWrite(LEDgruen, Helligkeit);
  analogWrite(LEDblau, Helligkeit);
  delay(Pause);
  // green and blue off = turquoise off
  analogWrite(LEDgruen, LOW);
  analogWrite(LEDblau, LOW);
  // red and blue on = purple
  analogWrite(LEDrot, Helligkeit);
  analogWrite(LEDblau, Helligkeit);
  delay(Pause);
  // red and blue off = purple off
  analogWrite(LEDrot, LOW);
  analogWrite(LEDblau, LOW);
}

Download: color-mixing-program-code-arduino-uno.ino

Rate the instructions

Comments

2 posts
Funduino TeamTeam

Hallo Olgiert, danke für den Hinweis und schön, dass es bei dir läuft! Du hast recht, Schaltplan und Code passten nicht zusammen. Richtig ist die Belegung im Code: Rot an Pin 5, Grün an Pin 6, Blau an Pin 3. Wir haben in der Anleitung einen Hinweis ergänzt und erneuern den Schaltplan.

Funduino Team

Olgiert Emin Yurekliturk

vielen Dank, es funktioniert. Hinweis: im Schaltplann pin 4 ist gezeichnet es soll 5 sein.

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.