Complete Guide Raspberry pi Multicolor RGB LED

Wiring a common cathode RGB LED to the Raspberry Pi

In this project, we will get acquainted with Raspberry pi Multicolor RGB LED (Multicolor LED Control) and learn how to create graphical user interfaces and control electronics circuits using Tkinter Library.

Required Component:

You have to need a flowing component for the Raspberry pi Multicolor RGB LED project 

  1. Raspberry Pi
  2. Breadboard
  3. Common cathode RGB LED
  4. Three 330 Ω resistors
  5. Jumper wires

 

Introduction to RGB LED :

RGB LED consists of three colors (red, green, and blue). Almost all colors can be created by combining these three colors. With an RGB LED you can create red, green, and blue lights and you can create other colors by configuring the light intensity of each color. For example, if you want to create the perfect blue light, you need to set the blue LED to the highest intensity and the green and red LED to the lowest intensity.

 

Figure 1 shows you a simple RGB color-mixing chart to give you an idea of ​​how to combine red, green, and blue to create different colors.

Simple RGB color mixing
FIGURE 1: Simple RGB color mixing

 

RGB LEDs are usually of two types. Namely-

  1. Common anode (common cathode) edge LED
  2. Common Cathode (common anode) edge LED 
Common anode and common cathode RGB LEDs
Figure 2: Common anode and common cathode RGB LEDs 

RGB LEDs usually have four legs. The RGB color has three legs and an anode or cathode edge. Which you can determine by looking at the leg length of the LED. It is shown in Figure 3 below.

Common anode and common cathode RGB LED leads
Figure 3: Common anode and common cathode RGB LED leads

Raspberry pi Multicolor LED circuit wiring : 

Raspberry Pi is very easy to communicate or wiring with RGB LED. For this, you need three current limiting registers and one register to be attached to each LED color.

Wiring a common cathode RGB LED to the Raspberry Pi
FIGURE 4: Wiring a common cathode RGB LED to the Raspberry Pi
  1. Connect the LED to the breadboard and attach the 300-ohm register to each color LED 

. Connect the raspberry pie from the ground to the ground of the board and connect the RGB LED to the raspberry pie as per the table below and be sure to see Figure 3 to see if your LED is fitted properly.

 

NOTE

If you’re using a common anode RGB LED, connect the longest lead (the anode) to 3.3 V.

 

RGB LEDRASPBERRY PI
First lead: redGPIO 23
Second lead: cathodeGND
Third lead: greenGPIO 24
Fourth lead: blueGPIO 25

 

Raspberry pi Multicolor LED Script Writing

 A user interface needs to be created using Tkinter Library to control RGB LEDs. Tkinter Library is installed automatically on the Russian operating system. All you have to do is import for scriptwriting. 

The user interface has three sliders to control the RGB LED intensity and a close button to shut down the program. 

Now take out the python IDE of Raspberry Pi and write the following program in it and save it as rgb_led_controller.py. 

#import necessary libraries
➊ from gpiozero import PWMLED
  from tkinter import *


  #change the RGB LED color
➋ def change_color (self):
      red.value = red_slider.get ()
      green.value = green_slider.get ()
      blue.value = blue_slider. get ()
      print (self)


  #close the window
➌ def close_window ():
      window.destroy ()




  #create a PWMLED object for each color
➍ red = PWMLED (23)
  green = PWMLED (24)
  blue = PWMLED (25)


  # create window
window = Tk ()
  ➎window.title ('RGB LED Controller')
  window.geometry ('300x200')


  #create three sliders to control each RGB LED lead
➏ red_slider = Scale (window, from_ = 0, to = 1 , resolution = 0.01,
  orient = HORIZONTAL, label = 'Red', troughcolor = 'red', length = 200,
  command = change_color)
  red_slider.pack ()


  green_slider = Scale (window, from_ = 0, to = 1, resolution = 0.01,
  orient = HORIZONTAL, label = 'Green', troughcolor = 'green', length = 200,
  command = change_color)
  green_slider.pack ()


  blue_slider = Scale (window, from_ = 0, to = 1, resolution = 0.01,
  orient) = HORIZONTAL, label = 'Blue', troughcolor = 'blue', length = 200,
  co mmand = change_color)
  blue_slider.pack ()


  #create close button
➐ close_button = Button (window, text = 'Close', command = close_window)
  close_button.pack ()



➑ mainloop ()

 Script Analysis

➊ from gpiozero import PWM LED

  from Tkinter import *

 

Imported PWM LED Library from gpiozero library to control LED PWM. At the same time, Tkinter library has been imported to create a graphical user interface. 

 

➋ def change_color(self):

      red.value = red_slider.get ()

      green.value = green_slider.get ()

      blue.value = blue_slider.get ()

      print (self)

 

A function called Change_color () has been created here. The function of this function is to change the value of the slider in the user interface as the intensity of the LED changes.

 

def ➌close_window():

      window.destroy ()

Close the window withclose_window () function. This function is called when you press the close button on the Tkinter interface.

 

➍ red = PWMLED (23)

  green = PWMLED (24)

  blue = PWMLED (25)

 

Red, green, and blue separate objects have been created for each color LED.

 

= Window = Tk ()

  window.title (‘RGB LED Controller’)

  window.geometry (‘300×200’)

 

Here the main window interface is defined. The name of the window title has been determined through Window.title. Window.geometry determines the dimensions of the window.

 

➏ red_slider = Scale (window, from_ = 0, to = 1, resolution = 0.01,

  orient = HORIZONTAL, label = ‘Red’, troughcolor = ‘red’, length = 200,

  command = change_color)

  red_slider.pack ()

 

After creating the window, you can add different elements to the window. For example slider or button and we know it as widgets. Here we will add a Button widget to close Windows. I will add scale widgets to add a slider to the window. We will also add three sliders to control the RGB LED.

 

 Follow the syntax below to create slides with the Tkinter library.

slider_name = Scale (master, option, option, …)

 

The window widget has been added as a master argument. 

from_ = 0 to = 1 Here the minimum and maximum range of the scale is declared.

 

resolution = 0.01 Resolution Sets the resolution of the slider – the minimum difference between two consecutive values. Here, the resolution is 0.01.

 

orient = HORIZONTAL Here the resolution of the slider is set i.e. orient is used to set the sliders horizontally or vertically. Horizontal is used as orient here. 

 

label = ‘Red’ This sets the label. You can label each slider according to the LED color in this script.

 

trough color = ‘red’ This sets the background color of the slider.

 

length = 200 This determines the length of the slider. All sliders are set to 200.

 

command = change_color The command specifies the process that will be called each time the slider is moved. In that case, moving the slider calls the change_color () function. Which changes the Duty cycle of the LED and as a result changes its color.

 

After creating the red_slider.pack () slider, you need to use the pack () method to keep Windows in the main window. Without passing any arguments in the pack () mode, widgets can be placed in the window by default settings. In this, the widgets only meet their minimum dimensions and are packed at the top of the window or opposite the widget at the top.

 

➐ close_button = Button (window, text = ‘Close’, command = close_window)

  close_button.pack ()

 

Now follow the syntax below to create the Close button.

Button_name = Button (master, option, option, …) The

following options have been used to create close buttons.

 

text = ‘Close’ Here text is used to display the name of the button.

Here is an analysis of many of the options given above.

 

This is the final part of the script, called the mainloop () function. This is an infinite loop. It helps draw windows and make events work and keeps the program running.

 

Raspberry pi Multicolor LED script run

To run the run, first you have to save or save the above program, then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run. When the program runs, you will see a popup window like the one in Figure 5. To close the running program, press CTRL + F2 or the Close button of the popup window. 

The RGB LED controller user interface
FIGURE 5: The RGB LED controller user interface

Congratulations! You have created a user interface. Now try moving the sliders to change the color of the LED. When you change the position of each slider, the RGB LED color changes accordingly. 

Homework 

Now how do you create a graphical user interface. You can customize this project to create more different types of projects. Below are some project ideas: 

  • Build a graphical user interface that controls turning an LED on and off.
  • Make a graphical user interface that controls the brightness of the LED.
  • Alter the user interface to control several LEDs.

 

 13 total views,  1 views today