In this project, we will learn how to create a Raspberry Pi Rainbow Effect using the addressable RGB LED. We will use the push button to control the start and end of the Rainbow Light effect. We will use a potentiometer to control the rainbow light speed and brightness of the rainbow light effect.
Required equipment:
You have to need flowing equipment for the Raspberry Pi Rainbow Effect project
- Raspberry Pi
- Breadboard
- WS2812B addressable RGB LED strip
- Logic level converter module BSS 138
- Two 10 kΩ potentiometers
- MCP 3 008 chip
- Pushbutton
- Three header pins
- Jumper wires
- SOFTWARE REQUIRED
- WS2 81X library
Addressable RGB LED
The rainbow effect we WS2812B RGB LED’ll use. Which I get in different sizes in the market. This is shown in Figure 1 below.

shown in the figure is 5 m long. Where there are 300 addressable WS2812B RGB LEDs. From here we will cut 14 keys and use them for this project. With the help of Figure 2 shown below, we will cut 14 LEDs separately.

With WS2812B addressable RGB LED, we can easily control the color and brightness separately. There is a separate built-in circuit for each LED, which can be easily controlled via Raspberry Pi’s GPIO. To control this, only the data pin can be connected to the GPIO pin. Follow the steps below to prepare for this project.
- Cut separately according to the 14 LED marks.
- Solder the 5-volt data plan ground pin shown in Figure 2.
NOTE
We’ve found that a Raspberry Pi 5 V pin (such as pin 2 or pin 4) is capable of powering a strip of 20 LEDs or fewer, but in projects where you’re using a long strip, you’ll need to use an external 5 V power supply to provide enough current.
Now the most important thing is to determine the power supply. For LEDs, we will select a power supply of 5 volts. But how many amperes this power supply will have depends on the number of LEDs. The LED we will use can take a maximum of 60mA current. Since we will use 20 LEDs, we need a power supply of 20 × 14 = 280mA.
We need 5 volts to control the data pin. But the problem is, we get three 3.3 volts from Raspberry Pi’s GPIO pin. So how do we control the LED?
To solve this problem we will use a component called logic level converter if the input to use it is given as 3.3. But out we get 5 volts.
Introduction to LOGIC LEVEL CONVERTER
A logic level converter can convert 3.3 V signal to a 5V signal. There are many types of logic level converters. However, this project will use the two-channel logic level converter bidirectional module shown in Figure 3.
The bidirectionality of this module lets you convert both data – from 3.3 V to 5 V and from 5 V to 3.3 V. You do not need to convert 5v to 3.3v in this project. This project needs to be converted from 3.3v to 5v. This logic level converter has two channels (Channel 1 and Channel 2). In this project, you will use only one channel to control the data pin of the LED strip.

The module has a low-voltage low voltage side (left side of Figure 3). Here you can connect everything from 3.3 V to 3.3 V and a high-voltage High Voltage side (on the right), where you can connect everything to 5 V. For this project, you need to use one of the pins highlighted in red to send 3.3v data and convert it to 5v.
To use the logic level converter, connect GND on both sides, 3.3 V on the low-voltage side and 5 V on the high-voltage side. Then, attach the data from Raspberry Pi to any TX1 pin. You can use Channel 1 or Channel 2 and get 5V data from the corresponding TX0 pin.
Raspberry Pi Rainbow Effect Circuit Wiring
After cutting 14 LEDs from the RGB LED strip, the header pin has to be inserted and soldering has to be done here. At the same time its pins have to be connected to the logic level converter. Now you are ready to make the circuit. To do this you need to connect an addressable RGB LED strip with the MCP 3008 chip using a pushbutton, two potentiometers and a logic level converter module. Which is shown in Figure 4.

WARNING
Remember that you can’t connect 5 V to the Pi GPIOs, as that can permanently damage your board.
NOTE
To identify the MCP3008 pins, orient the chip so it’s facing you with the half-circle cutout at the top. The first pin is the top left and the last pin is the top right. See “Analog-to-Digital Converters” on page 55 for a full MCP3008 pinout description.
- The ground of the raspberry pie and the pin of 3.8 volts should be attached to the rail of the breadboard. The
- MCP3008 IC should be placed in the middle of the breadboard so that its two ends are divided or divided at the center.
- After installing the potentiometer, one pin of the potentiometer has to be connected to the ground and the other to 3.3V.
- The mcp3008 IC should be placed on the breadboard as per the table below as shown in the figure. It doesn’t matter which pin you are connecting the potentiometer to. They will work the same way.
MCP3008 | CONNECT TO |
1 | One potentiometer’s middle lead |
2 | Other potentiometer’s middle lead |
9 | GND |
10 | GPIO 8 |
11 | GPIO 10 |
12 | GPIO 9 |
13 | GPIO 11 |
14 | GND |
15 | 3.3 V |
16 | 3.3 V |
- Place the push button in the center of the breadboard so that the two sides are evenly spaced. Now connect one pin with the ground and the other with another GPIO2.
- Attach the RGB LED strip pins to the breadboard.
- Place the logic level converter on the breadboard. Because we will convert low voltage to high voltage. Add it to the raspberry pie according to the table below.
LOGIC LEVEL CONVERTER | RASPBERRY PI |
TX1 (channel 2) | GPIO 18 |
LV | 3.3 V |
GND | GND |
- Connect the high voltage side of the logic level converter according to the table.
LOGIC LEVEL CONVERTER | CONNECT TO |
TX0 (channel 2) | RGB LED strip’s Data pin (middle pin) |
HV | 5 V |
GND | GND |
- Connect the RGB LED strip as instructed as soon as the logic level is converted.
RGB LED STRIP | CONNECT TO |
5 V | 5 V |
Din | Logic level converter TX0 pin |
GND | GND |
NOTE
If you choose to do this project with a strip of more than 20 LEDs, you’ll need to connect your 5 V power source to the strip’s 5 V pin and the GND power source to the GND rail.
Raspberry Pi Rainbow Effect Script Writing:
I will use the WS281X library to control the LEDs of this script. So you need to install this library as well as it will work on Serial Peripheral Interface (SPI) communication. So you need to enable this communication.
WS281X Library Installation
: The WS281X library can be installed by following some of the states below.
- Open the terminal and install with the following command.
pi @ raspberrypi: ud sudo apt install scons python3-dev swig |
- A folder needs to be created on the desktop. To do this, create a folder on the desktop called Libraries and follow the commands below.
pi @ raspberrypi: ~ d cd ~ / Desktop pi @ raspberrypi: ~ / Desktop $ mkdir Libraries pi @ raspberrypi: ~ / Desktop $ cd Libraries pi @ raspberrypi: ~ / Desktop / Libraries |
- Clone and download the library from your address.
pi @ raspberrypi: ~ / Desktop / Libraries $ git clone https: // github.com/jgarff/rpi_ws281x.git |
- Go to the rpi_ws281x library folder and run the scons command.
pi @ raspberrypi: ~ / Desktop / Libraries $ cd rpi_ws281x pi @ raspberrypi: ~ / Desktop / Libraries / rpi_ws281x sudo scons |
- Now go to Python folder and install WS281X library on your Raspberry Pi.
pi @ raspberrypi: ~ / Desktop / Libraries / rpi_ws281x $ cd python pi @ raspberrypi: ~ / Desktop / Libraries / rpi_ws281x / python $ sudo python3 setup.py install Now you have to prepare WS281X library in your code. |
Enabling SPI
communication: To communicate with the MPC 3008 chip, you need to enable SPI communication. Figure 5 below shows how to enable SPI communication.
Go to the taskbar main menu and select Preferences ▸ Raspberry Pi Configuration. In the Interfaces tab, click Enabled in the SPI row and then click OK.

The Raspberry Pi Rainbow Effect Script writing :
Before we start writing the script, let’s try to understand how it will work.
- The LED strip will display a running rainbow.
- A potentiometer will control the rainbow speed.
- Another potentiometer will control the brightness.
- The push-button will turn Rainbow Animation on or off.
TROUBLESHOOTING CRAZY PIXELS At the time of this writing, there is an issue with the strip pixels on newer versions of Raspbian. The pin used to control the strip is shared with analog audio output, so the pixels can go crazy and not work properly. If this happens when you load the code, you need to add two lines to the config.txt file. Go to the terminal and enter the following: pi @ raspberrypi: ~ $ sudo nano /boot/config.txt In the file that opens, add the following two lines (anywhere should be fine): hdmi_force_hotplug = 1 hdmi_force_edid_audio = 1 Press CTRL- X to save the file and then, when prompted, type Y and press ENTER. Reboot your Pi for the changes to take effect, and then proceed to the library installation. |
Now take out the python IDE of Raspberry Pi and write the following program there and save it as rainbow_effect.py.
#import necessary libraries ➊ from neopixel import * from time import sleep from gpiozero import Button, MCP3008 #LED strip configuration ➋ LED_COUNT = 14 #number of LED pixels LED_PIN = 18 #GPIO pin connected to the pixels (must support PWM!) LED_FREQ_HZ = 800000 #LED signal frequency in Hz (usually pot_brightness = MCP3008 (0) pot_speed = MCP3008 (1) #connect pushbutton to GPIO 2, pull-up button_start = Button (2) #animation running control variable running_animation = False #generate rainbow colors across 0-255 positions ➍ def wheel (pos ): if pos <85: return Color (pos * 3, 255 - pos * 3, 0) elif pos <170: pos - = 85 return Color (255 - pos * 3, 0, pos * 3) else: pos - = 170 return Color (0, pos * 3, 255 - pos * 3) #draw rainbow that uniformly distributes itself across all pixels ➎ def rainbowCycle (strip): for j i n range (256): for i in range (strip.numPixels ()): strip.setPixelColor (i, wheel ((int (i * 256 / strip.numPixels ()) + j) & 255)) strip.show ( ) ➏ sleep ((pot_speed.value * 40) /1000.0) #function to start and stop the animation ➐ def start_animation (): global running_animation if running_animation == True: running_animation = False else: running_animation = True #assign a function that runs when the button is pressed ➑ button_start.when_pressed = ) ➓ while True: if running_animation == True: #set LED strip brightness strip.setBrightness (int (pot_brightness.value * 255)) rainbowCycle (strip)
Raspberry Pi Rainbow Effect Script Analysis
➊ from neopixel import *
from time import sleep
from gpiozero import Button, MCP3008
The library required for the project has been imported here. Imported the neopixel library to control the LED strip and the time library for time control. Button and mcp3008 have been imported from gpiozero library to read the value of push button and potentiometer.
#LED strip configuration
➋ LED_COUNT = 14 #number of LED pixels
LED_PIN = 18 #GPIO pin connected to the pixels (must support PWM!)
LED_FREQ_HZ = 800000 #LED signal frequency in Hz (usually 800 kHz)
LED_DMA = 5 #DMA channel to use for generating signal (try 5)
LED_INVERT = False #set True to invert the signal
Here is the variable configuration for the RGB LED strip using Raspberry Pi’s GPIO PIN.
#create pot objects to refer to MCP3008 channel 0 and 1
➌ pot_brightness = MCP3008 (0)
pot_speed = MCP3008 (1) #connect
pushbutton to GPIO 2, pull-up
button_start = Button (2)
#animation running control variable
running_animation = False
Two objects of potentiometer have been created for brightness control and speed control. Zero (0) number pin of mcp3008 channel is used for brightness control and one (1) pin of mcp3008 channel is used for speed control. Simultaneously an object was created for the push button. Here another variable called running_animation is declared, which turns on and off the animation of the RGB LED strip. It stores Boolean data, which is by default.
➍ def wheel(pos):
if pos <85:
return Color (pos * 3, 255 – pos * 3, 0)
elif pos <170:
pos – = 85
return Color (255 – pos * 3, 0, pos * 3 )
else:
pos – = 170
return Color (0, pos * 3, 255 – pos * 3)
#draw rainbow that uniformly distributes itself across all pixels
def ➎rainbowCycle(strip):
for j in range (256):
for i in range (strip.numPixels ()):
strip.setPixelColor (i, wheel ((int * i *) 256 /
strip.numPixels ()) + j) & 255))
strip.show ()
➍ and ➍ have created two functions. Which creates a running rainbow effect. These functions are used in the example strandtest.py, which is similarly contained in the neopixel library. Simply put, the wheel () function creates colors by separating each color parameter from 0 to 255, and each color is made up of red, green, and blue. Which separates each parameter from 0 to 255 to create different color colors. The result is a rainbow effect. Here the rainbowCycle () function creates a rainbow effect based on the number of LEDs in your strip.
➏ ((pot_speed.value * 40) /1000.0)
Delay time is set using function on this line. A calculation has been made for the delay time. We can see here that the value of the potentiometer is multiplied by 40 and divided by 1000. Here the value of the potentiometer is multiplied by 40. Because if it was not done, the delay time would have been much shorter. As a result, the rainbow effect would be much faster. Here the second divided by 1000 is converted to milliseconds.
Using the gpiozero library in the following ways, you can press a push button to perform a specific activity.
button.when_pressed = function_name Here the function_name function refers to a generic function that will be called when the button is pressed. Note that the function must be defined before it is called. |
#function to start and stop the animation
➐ def start_animation():
global running_animation
if running_animation == True:
running_animation = False
else:
running_animation = True
#assign a function that runs when the button is pressed
➑ button_start.when_pressed = start_animation
In this case, the function is start_animation (). Notice that function_name has no first parentheses. This is because we are assigning a function to another function instead of just running the function. When the button_start.when_pressed function is triggered, the start_animation function is run.
Now pressing the push button will change the running_animation value. When the running_animation variable is False and the Button is pressed, it changes to True. This allows you to start and stop the rainbow effect.
#create NeoPixel object with appropriate configuration
➒ strip = Adafruit_NeoPixel (LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA,
LED_INVERT, int (pot_brightness.value * 255))
#initialize the strip
strip.begin ()
:
running = if true
#set LED strip brightness
strip.setBrightness (int (pot_brightness.value * 255))
rainbowCycle (strip)
The while loop runs the program. You can set the brightness before the animation starts. If the running_animation variable is true, then the function is run and the animation starts. If the push button is pressed, the running_animation variable changes to false and the animation stops.
Raspberry Pi Rainbow Effect Run script
to run this script, you’ll need to use the terminal. If you use the Python IDE editor, you will get an error.
Save the project named rainbow_effect.py in the folder and open the terminal in the same directory and follow the instructions below to run the script.
pi @ raspberrypi: ~ d cd ~ / Desktop / Projects / LEDs
pi @ raspberrypi: ~ / desktop / Projects / LEDs ud sudo python3
rainbow_effect.py
Now you can control the brightness and speed by turning the potentiometer as per your requirement and press the push button if you want to turn it off.
Homework:
Below are some ideas for your skill development.
- Light up a specific LED in the middle of the strip.
- Light all the LEDs in just one color.
- Add a pushbutton to change between preset effects.
- Blink the LEDs like Christmas lights.
- Invent your own effects.
RASPBERRY PI TUTORIALS FOR BEGINNERS
23 total views, 1 views today