Experiment: Controlling RGB LEDs with Arduino and Raspberry Pi
In this experiment, we will control the color of an RGB LED using both Arduino and Raspberry Pi. In the version of the experiment performed on Raspberry Pi, we will use a graphical user interface with three sliders to control the color. The images below depict the Raspberry Pi setup and the experiment's schematic.
Components
For this experiment, you will need the following components for both Arduino and Raspberry Pi:
LED1 - RGB LED
R1-3 - 470 Ohm 0.25W resistors
400-point breadboard
Male-to-Male jumper wires
Male-to-Female jumper wires (only for Raspberry Pi)
Please note that Male-to-Female jumper wires are only needed for connecting to the GPIO pins on Raspberry Pi (if you plan to conduct this experiment with Raspberry Pi as well).
To achieve optimal brightness and the best color balance, careful selection of resistors with matching resistances is essential. However, it's easier to obtain the components if you use 470 Ohm resistors for all three channels. They will work with both Raspberry Pi and Arduino.
Connecting to Raspberry Pi
In the version of the experiment focused on Raspberry Pi, we will not just enter commands to change the color, but we will use a small window with a user interface containing three sliders – one for each color channel. As you adjust the position of the sliders, the color of the RGB LED will change.
Since this program features a graphical user interface, and there is no graphical interface in SSH, you will need to connect a keyboard, mouse, and monitor to the Raspberry Pi.
The layout of the breadboard for working with Raspberry Pi is shown in the figure – it is exactly the same as in the case of Arduino, except that when working with Raspberry Pi, you will need ‘male-to-female’ jumper wires. The GPIO pins 18, 23, and 24 are used as PWM outputs here.
Program for Raspberry Pi.
The program for this experiment, written in Python, utilizes the Tkinter framework. This allows for the creation of window applications with their own controls, enabling you to move beyond the primitive command line you have been working with so far. Consequently, the code of the program turns out to be a bit longer than usual. Additionally, it sometimes employs relatively complex programming.
Let's take a look at the code:
Code: Select all
from Tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # (1)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
pwmRed = GPIO.PWM(18, 500) # (2)
pwmRed.start(100)
pwmGreen = GPIO.PWM(23, 500)
pwmGreen.start(100)
pwmBlue = GPIO.PWM(24, 500)
pwmBlue.start(100)
class App:
def __init__(self, master): #(3)
frame = Frame(master) #(4)
frame.pack()
Label(frame, text='Red').grid(row=0, column=0) # (5)
Label(frame, text='Green').grid(row=1, column=0)
Label(frame, text='Blue').grid(row=2, column=0)
scaleRed = Scale(frame, from_=0, to=100, # (6)
orient=HORIZONTAL, command=self.updateRed)
scaleRed.grid(row=0, column=1)
scaleGreen = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateGreen)
scaleGreen.grid(row=1, column=1)
scaleBlue = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.updateBlue)
scaleBlue.grid(row=2, column=1)
def updateRed(self, duty): # (7)
# change the led brightness to match the slider
pwmRed.ChangeDutyCycle(float(duty))
def updateGreen(self, duty):
pwmGreen.ChangeDutyCycle(float(duty))
def updateBlue(self, duty):
pwmBlue.ChangeDutyCycle(float(duty))
root = Tk() # (8)
root.wm_title('RGB LED Control')
app = App(root)
root.geometry("200x150+0+0")
try:
root.mainloop()
finally:
print("Cleaning up")
GPIO.cleanup()
Let's clarify some points of this program step by step, using the line layout made in the comments:
- We will configure the Raspberry Pi to work with pin names according to the Broadcom system, rather than just with positional designations.
- We will start Pulse Width Modulation (PWM) on the red, green, and blue channels to control the brightness of the LEDs.
- This function is called when the application is created.
- This frame contains various controls from the graphical user interface.
- We create labels and place each one in its own grid cell.
- We create sliders and place each one in its own grid cell. The command attribute specifies which method should be called when the slider is moved.
- This method and similar methods for other colors are triggered when the slider is moved.
- We launch the graphical user interface, setting the window title, size, and position.
We load and run the program.
We will run the program with administrator privileges using the following command:
$ sudo python mixing_colors.py
After a couple of seconds, a window will appear as shown in the figure. As the sliders are moved, the color of the RGB LED will change.