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.
RGB LED color mixing in 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.
set_color(100, 0, 0) # Full red, no green, no blue
set_color(0, 100, 0) # Full green, no red, no blue
set_color(0, 0, 100) # Full blue, no red, no green
set_color(50, 50, 0) # Mix red and green to get yellow
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.
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()
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.
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.
Practice
Solution
Step 1: Understand PWM role in LEDs
PWM (Pulse Width Modulation) changes how long the LED is on versus off, controlling brightness.Step 2: Apply PWM to RGB LED components
Each color (red, green, blue) brightness is controlled separately by PWM signals.Final Answer:
The brightness of each red, green, and blue LED component -> Option AQuick Check:
PWM controls brightness = D [OK]
- Confusing PWM with voltage control
- Thinking PWM controls processor speed
- Assuming PWM changes LED temperature
Solution
Step 1: Recall RPi.GPIO PWM syntax
The correct syntax is to create a PWM object with GPIO.PWM(pin, frequency).Step 2: Start PWM with duty cycle
Then call pwm.start(duty_cycle) to begin PWM signal.Final Answer:
pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle) -> Option BQuick Check:
Correct PWM start syntax = C [OK]
- Using non-existent methods like PWMStart or startPWM
- Calling start before creating PWM object
- Mixing method names or parameters
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) red = 17 green = 27 blue = 22 GPIO.setup(red, GPIO.OUT) GPIO.setup(green, GPIO.OUT) GPIO.setup(blue, GPIO.OUT) pwm_red = GPIO.PWM(red, 1000) pwm_green = GPIO.PWM(green, 1000) pwm_blue = GPIO.PWM(blue, 1000) pwm_red.start(100) pwm_green.start(0) pwm_blue.start(0)What color will the RGB LED show?
Solution
Step 1: Analyze PWM duty cycles for each color
Red PWM is started at 100% duty cycle (full brightness), green and blue at 0% (off).Step 2: Determine resulting LED color
Only red LED is on fully, so the LED shows bright red color.Final Answer:
Bright red -> Option DQuick Check:
Red=100%, Green=0%, Blue=0% = Bright red [OK]
- Assuming 0 means half brightness
- Mixing pin numbers with colors
- Thinking all LEDs light up by default
pwm_red.start(50) pwm_green.start(50) pwm_blue.start(0) GPIO.cleanup()What is the main issue causing the LED to not show the mixed color?
Solution
Step 1: Understand GPIO.cleanup() effect
GPIO.cleanup() resets all GPIO pins, stopping PWM signals immediately.Step 2: Identify timing issue in code
Calling cleanup right after starting PWM stops the LEDs before visible mixing.Final Answer:
Calling GPIO.cleanup() immediately stops PWM signals -> Option AQuick Check:
cleanup() stops PWM = A [OK]
- Thinking duty cycle must be 0 or 100 only
- Ignoring cleanup() effect on GPIO pins
- Assuming frequency defaults to zero
Solution
Step 1: Understand color fading by PWM mixing
Fading from red to blue means red brightness decreases while blue brightness increases smoothly.Step 2: Apply PWM duty cycle changes
Decrease red duty cycle from 100% to 0%, increase blue from 0% to 100%, green stays off.Final Answer:
Gradually decrease red duty cycle from 100 to 0 while increasing blue from 0 to 100 -> Option CQuick Check:
Fade red down, blue up = A [OK]
- Turning both colors fully on causes white, not fade
- Blinking one color doesn't create smooth fade
- Keeping all colors constant prevents fading
Ready to prove it?
Pass the test and this pattern moves unknown
