Bird
Raised Fist0
Raspberry Piprogramming~10 mins

RGB LED color mixing in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - RGB LED color mixing
Start
↓
Set Red Intensity
↓
Set Green Intensity
↓
Set Blue Intensity
↓
Combine Colors
↓
Output Mixed Color to LED
↓
End
The program sets the intensity of red, green, and blue LEDs, combines them, and outputs the mixed color to the RGB LED.
Execution Sample
Raspberry Pi
red = 255
green = 100
blue = 50
color = (red, green, blue)
print(color)
This code sets red, green, and blue values, combines them into a color tuple, and prints the result.
Execution Table
StepVariableValueActionOutput
1red255Set red intensity to max
2green100Set green intensity to medium
3blue50Set blue intensity to low
4color(255, 100, 50)Combine RGB values into tuple
5print(color)Output the combined color(255, 100, 50)
6--End of program
💡 All RGB values set and combined; program ends after printing the color tuple.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
redundefined255255255255255
greenundefinedundefined100100100100
blueundefinedundefinedundefined505050
colorundefinedundefinedundefinedundefined(255, 100, 50)(255, 100, 50)
Key Moments - 3 Insights
Why do we combine red, green, and blue values into a tuple?
Combining into a tuple groups the three color intensities so they can be sent together to the LED, as shown in step 4 of the execution_table.
What does the printed output (255, 100, 50) represent?
It represents the combined color intensities for red, green, and blue channels that the LED will display, as seen in step 5 of the execution_table.
Why do the variables red, green, and blue have values before color is set?
Because color depends on red, green, and blue values, they must be set first (steps 1-3) before combining them in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'green' after step 2?
A100
B255
C50
Dundefined
💡 Hint
Check the 'Value' column for 'green' at step 2 in the execution_table.
At which step is the RGB color tuple created?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the action 'Combine RGB values into tuple' in the execution_table.
If we change blue from 50 to 200, what will be the new color tuple at step 4?
A(255, 100, 50)
B(255, 200, 50)
C(255, 100, 200)
D(200, 100, 50)
💡 Hint
The tuple is (red, green, blue). Changing blue affects the third value, see variable_tracker.
Concept Snapshot
RGB LED color mixing:
- Set red, green, blue intensities (0-255)
- Combine as (red, green, blue) tuple
- Send tuple to LED to mix colors
- Higher value = brighter color
- Mix creates many colors by blending intensities
Full Transcript
This example shows how to mix colors on an RGB LED by setting red, green, and blue values. Each color is set from 0 to 255, where 0 means off and 255 means full brightness. We combine these three values into a tuple representing the mixed color. The program prints this tuple to show the final color sent to the LED. The execution table traces each step: setting red to 255, green to 100, blue to 50, combining them, and printing the result. The variable tracker shows how each variable changes step by step. Key moments clarify why we combine colors and what the output means. The quiz tests understanding of variable values and tuple creation. This helps beginners see how RGB mixing works in code on a Raspberry Pi.

Practice

(1/5)
1. What does PWM control when mixing colors on an RGB LED connected to a Raspberry Pi?
easy
A. The brightness of each red, green, and blue LED component
B. The voltage level of the Raspberry Pi power supply
C. The speed of the Raspberry Pi processor
D. The temperature of the LED

Solution

  1. Step 1: Understand PWM role in LEDs

    PWM (Pulse Width Modulation) changes how long the LED is on versus off, controlling brightness.
  2. Step 2: Apply PWM to RGB LED components

    Each color (red, green, blue) brightness is controlled separately by PWM signals.
  3. Final Answer:

    The brightness of each red, green, and blue LED component -> Option A
  4. Quick Check:

    PWM controls brightness = D [OK]
Hint: PWM changes LED brightness, not voltage or speed [OK]
Common Mistakes:
  • Confusing PWM with voltage control
  • Thinking PWM controls processor speed
  • Assuming PWM changes LED temperature
2. Which of the following is the correct way to start PWM on a Raspberry Pi GPIO pin using Python's RPi.GPIO library?
easy
A. pwm = GPIO.PWMStart(pin, frequency, duty_cycle)
B. pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle)
C. pwm = GPIO.startPWM(pin, duty_cycle); pwm.frequency(frequency)
D. pwm = GPIO.PWM(pin); pwm.begin(frequency, duty_cycle)

Solution

  1. Step 1: Recall RPi.GPIO PWM syntax

    The correct syntax is to create a PWM object with GPIO.PWM(pin, frequency).
  2. Step 2: Start PWM with duty cycle

    Then call pwm.start(duty_cycle) to begin PWM signal.
  3. Final Answer:

    pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle) -> Option B
  4. Quick Check:

    Correct PWM start syntax = C [OK]
Hint: Use GPIO.PWM(pin, freq) then start(duty) [OK]
Common Mistakes:
  • Using non-existent methods like PWMStart or startPWM
  • Calling start before creating PWM object
  • Mixing method names or parameters
3. Given this Python code controlling an RGB LED on Raspberry Pi:
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?
medium
A. Bright green
B. White
C. Bright blue
D. Bright red

Solution

  1. 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).
  2. Step 2: Determine resulting LED color

    Only red LED is on fully, so the LED shows bright red color.
  3. Final Answer:

    Bright red -> Option D
  4. Quick Check:

    Red=100%, Green=0%, Blue=0% = Bright red [OK]
Hint: 100% duty cycle means full brightness for that color [OK]
Common Mistakes:
  • Assuming 0 means half brightness
  • Mixing pin numbers with colors
  • Thinking all LEDs light up by default
4. This code is intended to mix red and green colors on an RGB LED using Raspberry Pi PWM, but it does not work as expected:
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?
medium
A. Calling GPIO.cleanup() immediately stops PWM signals
B. Duty cycle values must be 0 or 100 only
C. PWM frequency is not set before starting
D. Blue LED must be started with 50% duty cycle

Solution

  1. Step 1: Understand GPIO.cleanup() effect

    GPIO.cleanup() resets all GPIO pins, stopping PWM signals immediately.
  2. Step 2: Identify timing issue in code

    Calling cleanup right after starting PWM stops the LEDs before visible mixing.
  3. Final Answer:

    Calling GPIO.cleanup() immediately stops PWM signals -> Option A
  4. Quick Check:

    cleanup() stops PWM = A [OK]
Hint: Don't call GPIO.cleanup() before PWM runs [OK]
Common Mistakes:
  • Thinking duty cycle must be 0 or 100 only
  • Ignoring cleanup() effect on GPIO pins
  • Assuming frequency defaults to zero
5. You want to create a smooth color fade from red to blue on an RGB LED using Raspberry Pi PWM. Which approach correctly mixes colors to achieve this effect?
hard
A. Turn off green and blue, then blink red rapidly
B. Keep red at 100 and blue at 100 simultaneously for full brightness
C. Gradually decrease red duty cycle from 100 to 0 while increasing blue from 0 to 100
D. Set all colors to 50% duty cycle and keep constant

Solution

  1. Step 1: Understand color fading by PWM mixing

    Fading from red to blue means red brightness decreases while blue brightness increases smoothly.
  2. Step 2: Apply PWM duty cycle changes

    Decrease red duty cycle from 100% to 0%, increase blue from 0% to 100%, green stays off.
  3. Final Answer:

    Gradually decrease red duty cycle from 100 to 0 while increasing blue from 0 to 100 -> Option C
  4. Quick Check:

    Fade red down, blue up = A [OK]
Hint: Fade one color down, other up for smooth transition [OK]
Common Mistakes:
  • Turning both colors fully on causes white, not fade
  • Blinking one color doesn't create smooth fade
  • Keeping all colors constant prevents fading
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard