📖 Scenario: You have a small RGB LED connected to your Raspberry Pi. Each color (Red, Green, Blue) can be turned on or off by setting the GPIO pins high or low. You want to create different colors by mixing these three basic colors.
🎯 Goal: Write a program that controls the RGB LED by turning on or off the red, green, and blue pins to create a specific color.
📋 What You'll Learn
Create variables for the GPIO pins connected to the red, green, and blue parts of the LED.
Set up the GPIO pins as outputs.
Write code to turn on the red and green pins and turn off the blue pin to create yellow color.
Print the current color being displayed.
💡 Why This Matters
🌍 Real World
RGB LEDs are used in many devices for colorful lighting effects, status indicators, and decorations.
💼 Career
Understanding how to control hardware like LEDs with code is important for jobs in embedded systems, IoT, and hardware prototyping.
Progress0 / 4 steps
1
Set up GPIO pins for RGB LED
Create three variables called red_pin, green_pin, and blue_pin and set them to 17, 27, and 22 respectively.
Raspberry Pi
Hint
Use simple assignment to set each pin number to the correct variable.
2
Configure GPIO pins as outputs
Import the RPi.GPIO module as GPIO. Set the GPIO mode to GPIO.BCM. Then set up red_pin, green_pin, and blue_pin as output pins using GPIO.setup().
Raspberry Pi
Hint
Remember to import the GPIO library and set the mode before setting up pins.
3
Turn on red and green pins, turn off blue pin
Use GPIO.output() to turn on the red_pin and green_pin by setting them to GPIO.HIGH. Turn off the blue_pin by setting it to GPIO.LOW.
Raspberry Pi
Hint
Use GPIO.output with the correct pin and state to control the LED colors.
4
Print the current color
Write a print statement that outputs the text "The LED color is Yellow".
Raspberry Pi
Hint
Use print() to show the color name on the screen.
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
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 A
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
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 B
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
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 D
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:
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
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 A
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
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 C
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 unknownstrongwhile earning +1 Grip toward your certificate.