RGB LED color mixing lets you create many colors by combining red, green, and blue light. It helps make lights show different colors using just one LED.
0
0
RGB LED color mixing in Raspberry Pi
Introduction
You want to make a light that changes colors for decoration.
You want to show different statuses with colors on a device.
You want to learn how colors mix using electronics.
You want to build a simple mood light with a Raspberry Pi.
You want to control colors in a project with buttons or sensors.
Syntax
Raspberry Pi
import RPi.GPIO as GPIO import time # Setup pins for red, green, blue RED_PIN = 17 GREEN_PIN = 27 BLUE_PIN = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(RED_PIN, GPIO.OUT) GPIO.setup(GREEN_PIN, GPIO.OUT) GPIO.setup(BLUE_PIN, GPIO.OUT) # Create PWM objects for each color red = GPIO.PWM(RED_PIN, 1000) green = GPIO.PWM(GREEN_PIN, 1000) blue = GPIO.PWM(BLUE_PIN, 1000) # Start PWM with 0 duty cycle (off) red.start(0) green.start(0) blue.start(0) # Function to set color by duty cycle (0-100) def set_color(r, g, b): red.ChangeDutyCycle(r) green.ChangeDutyCycle(g) blue.ChangeDutyCycle(b)
Use PWM (Pulse Width Modulation) to control brightness of each color.
Duty cycle 0 means LED off, 100 means fully on.
Examples
This makes the LED show bright red color.
Raspberry Pi
set_color(100, 0, 0) # Full red, no green, no blue
This makes the LED show bright green color.
Raspberry Pi
set_color(0, 100, 0) # Full green, no red, no blue
This makes the LED show bright blue color.
Raspberry Pi
set_color(0, 0, 100) # Full blue, no red, no green
This mixes red and green light to create yellow color.
Raspberry Pi
set_color(50, 50, 0) # Mix red and green to get yellow
Sample Program
This program cycles through main colors by changing the brightness of red, green, and blue parts of the LED. It prints the color name each time so you know what color is showing.
Raspberry Pi
import RPi.GPIO as GPIO import time RED_PIN = 17 GREEN_PIN = 27 BLUE_PIN = 22 GPIO.setmode(GPIO.BCM) GPIO.setup(RED_PIN, GPIO.OUT) GPIO.setup(GREEN_PIN, GPIO.OUT) GPIO.setup(BLUE_PIN, GPIO.OUT) red = GPIO.PWM(RED_PIN, 1000) green = GPIO.PWM(GREEN_PIN, 1000) blue = GPIO.PWM(BLUE_PIN, 1000) red.start(0) green.start(0) blue.start(0) def set_color(r, g, b): red.ChangeDutyCycle(r) green.ChangeDutyCycle(g) blue.ChangeDutyCycle(b) try: while True: set_color(100, 0, 0) # Red print("Red") time.sleep(1) set_color(0, 100, 0) # Green print("Green") time.sleep(1) set_color(0, 0, 100) # Blue print("Blue") time.sleep(1) set_color(50, 50, 0) # Yellow print("Yellow") time.sleep(1) set_color(0, 50, 50) # Cyan print("Cyan") time.sleep(1) set_color(50, 0, 50) # Magenta print("Magenta") time.sleep(1) set_color(100, 100, 100) # White print("White") time.sleep(1) except KeyboardInterrupt: pass finally: red.stop() green.stop() blue.stop() GPIO.cleanup()
OutputSuccess
Important Notes
Make sure to connect the LED pins correctly with resistors to avoid damage.
Use BCM pin numbering mode for clarity and consistency.
Clean up GPIO pins after use to avoid warnings on next run.
Summary
RGB LED color mixing uses red, green, and blue light combined at different brightness levels.
Use PWM to control each color's brightness from 0 (off) to 100 (full).
Mixing colors lets you create many colors with just one RGB LED.