webwinkelkeur logo

4.7 avg.

5101+ reviews
webwinkelkeur logoView all

5101+ reviews

5101+ reviews

Order by 16:00 for same day shipping

14 days return

GB

EN

Individual

Business

GPIO Project 6 - RGB Led

Beginner
30 Minuten
114,90

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.

Connection diagram

RGB LED connection (common cathode):

  1. Connect the common leg (cathode):
    • Connect thecommon leg of the RGB LED (longest leg) toGND (pin 6).
  2. Connect the red LED leg:
    • Connect the red leg via a 220Ω resistor to GPIO 17 (pin 11).
  3. Connect the green LED leg:
    • Connect the green leg via a 220Ω resistor with GPIO 27 (pin 13).

Pinout Reference

GPIO

Pin #

Function

Connection

GPIO 17Pin 11Red LEDConnect via resistor
GPIO 27Pin 13Green LEDConnect via resistor
GPIO 22Pin 15Blue LEDConnect via resistor
GNDPin 6Earth (Ground)Common cathode

 

Python-code in Thonny

Step 1: Write your code

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

Step 2: Save the file

Click File > Save As and name the file rgb_led.py.

Step 3: Run the script

Click the greenRun button (▶) at the top the Thonny interface.

How does it work?

  • PWMLED: This allows you to adjust the intensity of each color with values ​​between 0.0 (off) and 1.0 (fully on).
  • set_color(): A function to set the intensity of the red, green and blue LED at the same time.
  • Color Combinations: By combining different intensities of red, green and blue, you can create different colors.

Result

  • The RGB LED automatically cycles through the following colors: red, green, blue, yellow, cyan, magenta, white, and off.
  • Each color remains active for 1 second before switching to the next.

Experimenting

  1. Adjust Color: Adjust the values ​​of set_color() to create new colors. For example, try:
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().