5101+ reviews
Order by 16:00 for same day shipping
14 days return
EN
Individual
Business
In this project you will learn how to use an RGB LED to create different colors by controlling the three primary colors (red, green, blue) separately via GPIO pins. You can combine colors by varying the intensity of each color.
RGB LED connection (common cathode):
GPIO | Pin # | Function | Connection |
GPIO 17 | Pin 11 | Red LED | Connect via resistor |
GPIO 27 | Pin 13 | Green LED | Connect via resistor |
GPIO 22 | Pin 15 | Blue LED | Connect via resistor |
GND | Pin 6 | Earth (Ground) | Common cathode |
Open the Thonny Python IDE and enter the following code:
from gpiozero import PWMLED
from time import sleep
# RGB LED-pinnen koppelen aan GPIO
red = PWMLED(17) # Rode LED op GPIO 17
green = PWMLED(27) # Groene LED op GPIO 27
blue = PWMLED(22) # Blauwe LED op GPIO 22
def set_color(r, g, b):
"""Stel de kleur van de RGB LED in."""
red.value = r # Rood intensiteit (0.0 - 1.0)
green.value = g # Groen intensiteit (0.0 - 1.0)
blue.value = b # Blauw intensiteit (0.0 - 1.0)
try:
while True:
print("Rood")
set_color(1, 0, 0) # Rood
sleep(1)
print("Groen")
set_color(0, 1, 0) # Groen
sleep(1)
print("Blauw")
set_color(0, 0, 1) # Blauw
sleep(1)
print("Geel")
set_color(1, 1, 0) # Geel (rood + groen)
sleep(1)
print("Cyaan")
set_color(0, 1, 1) # Cyaan (groen + blauw)
sleep(1)
print("Magenta")
set_color(1, 0, 1) # Magenta (rood + blauw)
sleep(1)
print("Wit")
set_color(1, 1, 1) # Wit (rood + groen + blauw)
sleep(1)
print("Uit")
set_color(0, 0, 0) # LED uit
sleep(1)
except KeyboardInterrupt:
print("\nProgramma gestopt.")
set_color(0, 0, 0) # Zet LED uit bij stoppen
Click File > Save As and name the file rgb_led.py.
Click the greenRun button (▶) at the top the Thonny interface.
set_color(0.5, 0.3, 0.7) # Experimentele kleur
2. Interactive Control: Use a push button or potentiometer to manually control the intensity of the colors.
3.Add Blinking: Make the RGB LED blink in specific colors by adjusting sleep().