Arduino Tutorial: So verwenden Sie eine RGB-LED

What is an RGB LED?

LEDs are small, powerful lights that are used in many different applications. An RGB LED is a combination of 3 LEDs with different colors:

  • 1x red LED
  • 1x green LED
  • 1x blue LED

How are different colors displayed?

An RGB LED can output different colors by mixing the 3 primary colors, red, green and blue, and also changing the intensity. So it actually consists of 3 separate LEDs, red, green and blue, housed in a single package.

Therefore, there are 4 leads, one lead for each of the 3 colors and a common cathode or anode, depending on the RGB LED type.

Mixing colors

To create different colors, you can combine the three colors in different intensities. To adjust the intensity of each LED you can use a PWM signal.

Since the LEDs are very close to each other, our eyes see the result of the color combination and not the three colors individually.

To get an idea of how you can combine the colors, take a look at the diagram below. This is the simplest color mixing diagram. However, it gives an idea of how it works and how different colors are created.

color-mixing

RGB LEDs with common anode and common cathode

There are two types of RGB LEDs: common anode LED and common cathode LED. The following figure shows a common anode and a common cathode LED.

rgb-led-1

In a common cathode RGB LED, all three LEDs have a negative connection (cathode). With a common RGB LED anode, the three LEDs have a positive connection (anode).

This results in an LED that has 4 pins, one for each LED and a common cathode or anode.

In this tutorial, a cathode is used.

rgb-led-pin

RGB LED Pins

RGB LEDs have four connections (4 pins) - one for each LED and one for the common anode or cathode. You can identify each conductor by its length, as shown in the figure below.

If the LED is facing you so that the anode or cathode (the longest lead) is second from the left, the leads should be in the following order: red, anode or cathode, green, and blue.

Control an RGB LED with the Arduino

Circuit diagram

The cathode is connected to ground and the 3 anodes are connected via 220 ohm resistors to 3 digital pins on the Arduino board that can provide a PWM signal.
Arduino-rgb-led-1

We use PWM to simulate the analog output that provides different voltage levels to the LEDs so we can get the colors we want.

Arduino-rgb-led-2

Source code

In the Arduino sketch, pins number 7, 6 and 5 are used and called redPin, greenPin and bluePin. In the setup area, we need to define these as outputs. In the sketch there is a function setColor () which takes three different arguments: redValue, greenValue and blueValue. These arguments represent the brightness of the LEDs or the duty cycle of the PWM signal generated by the analogWrite () function. These values can be between 0 and 255. This corresponds to a duty cycle of 100% of the PWM signal or the maximum LED brightness.

Arduino LED Code:

int redPin= 7; int greenPin = 6; int bluePin = 5; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { setColor(255, 0, 0); // Red Color delay(1000); setColor(0, 255, 0); // Green Color delay(1000); setColor(0, 0, 255); // Blue Color delay(1000); setColor(255, 255, 255); // White Color delay(1000); setColor(170, 0, 255); // Purple Color delay(1000); } void setColor(int redValue, int greenValue, int blueValue) { analogWrite(redPin, redValue); analogWrite(greenPin, greenValue); analogWrite(bluePin, blueValue); }

In the loop function, adjustments must be made so that the color of the LED changes every second. To get red light on the LED, we call the setColor () function and set the value 255 for the redValue argument and 0 for the other two. Accordingly, we can get the other two primary colors green and blue. To get other colors, we have to mix the argument values. For example, if you set all 3 LEDs to maximum brightness, you will get the color white and a violet color if you set the following values for the arguments: 170 redValue, 0 greenValue and 255 blueValue.

Passend dazu

WS2812B-2020.pngws2812b-2020-rgb-led.JPG
Worldsemi WS2812B-2020 RGB LED (4500x on reel)
Sale price425,00 €
In stock
SK6812MINI-B-reel.pngSK6812MINI-B-Reel-VPE.png
OPSCO OPSCO SK6812MINI-B LED (Full Reel 1500pcs)
Sale price95,00 €
Only 3 units left

Passend zu diesem Artikel

View all
Save 1,94 €
neopixel-stick---8-x-ws2812-5050-rgb-led-with_EXP-R15-208_1_600x600.jpgneopixel-stick---8-x-ws2812-5050-rgb-led-with_EXP-R15-208_2_600x600.jpg
Adafruit Adafruit NeoPixel Stick - 8 x WS2812 5050 RGB LED with Integrated Drivers
Sale price3,96 € Regular price5,90 €
In stock, 10 units
Save 2,72 €
flora-rgb-smart-neo-pixel-version-2-pack-of-4-00_600x600.jpgflora-rgb-smart-neo-pixel-version-2-pack-of-4-01_600x600.jpg
Adafruit Adafruit Flora RGB Smart Neo Pixel version 2 - Pack of 4
Sale price5,17 € Regular price7,89 €
In stock, 5 units
Save 3,23 €
neopixel-ring---16-x-ws2812-5050-rgb-led-with_EXP-R15-225_2_600x600.jpgneopixel-ring---16-x-ws2812-5050-rgb-led-with_EXP-R15-225_1_600x600.jpg
Adafruit Adafruit NeoPixel Ring - 16 x WS2812 5050 RGB LED with Integrated Drivers
Sale price6,64 € Regular price9,87 €
In stock, 4 units
adafruit-neopixel-diffused-5mm-5-pack-01_600x600.jpgadafruit-neopixel-diffused-5mm-5-pack-04_600x600.jpg
Adafruit Adafruit NeoPixel Diffused 5mm Through-Hole LED - 5 Pack
Sale price4,90 €
Not in stock, available for backorder
Save 0,70 €
lilypad-rainbow-led-6-colors-01a_600x600.jpglilypad-rainbow-led-6-colors-03a_600x600.jpg
Sparkfun LilyPad Rainbow LED (6 Colors)
Sale price4,00 € Regular price4,70 €
In stock, 4 units
Save 2,07 €
adafruit-neopixel-jewel-7-ws2812-5050-rgb-led-01_600x600.jpgadafruit-neopixel-jewel-7-ws2812-5050-rgb-led_600x600.jpg
Adafruit Adafruit NeoPixel Jewel - 7 x WS2812 5050 RGB LED with Integrated Drivers
Sale price3,83 € Regular price5,90 €
In stock, 17 units
ws2812b-5050-rgb-led-with-integrated-driver_EXP-R15-315_1_600x600.jpg
OPSCO OPSCO SK6812 LED - 10 Pack
Sale price2,00 €
In stock, 28 units
Save 2,82 €
adafruit-neopixel-stick-8-x-5050-rgbw-leds-natural-white-4500k-07_600x600.jpgadafruit-neopixel-stick-8-x-5050-rgbw-leds-natural-white-4500k-04_600x600.jpg
Adafruit Adafruit NeoPixel Stick - 8 x 5050 RGBW LEDs - Natural White - ~4500K
Sale price5,07 € Regular price7,89 €
In stock, 6 units
Save 0,24 €
expr05668-1.jpgexpr05668-2.jpg
Sparkfun LED - RGB Addressable, PTH, 5mm Clear (5 Pack)
Sale price1,90 € Regular price2,14 €
In stock, 7 units
adafruit-neopixel-digital-rgbw-led-strip-white-pcb-144-led-m-1m-02_600x600.jpgadafruit-neopixel-digital-rgbw-led-strip-white-pcb-144-led-m-1m-05_600x600.jpg
Adafruit Adafruit NeoPixel Digital RGBW LED Strip - White PCB 144 LED/m - 1m
Sale price69,41 €
Not in stock, available for backorder
Save 3,55 €
adafruit-neopixel-ring-12-x-5050-rgbw-leds-w-integrated-drivers-natural-white-4500k_600x600.jpgadafruit-neopixel-ring-12-x-5050-rgbw-leds-w-integrated-drivers-natural-white-4500k-01_600x600.jpg
Save 1,67 €
adafruit-neopixel-diffused-8mm-led-5-pack_600x600.jpgadafruit-neopixel-diffused-8mm-led-5-pack-01_600x600.jpg
Adafruit Adafruit NeoPixel Diffused 8mm Through-Hole LED - 5 Pack
Sale price3,23 € Regular price4,90 €
In stock, 19 units