How to Control an RGB LED Strip with a Raspberry Pi

I've previously read several tutorials explaining how to control an RGB LED strip with a Raspberry Pi. Yet they were often vague or even inaccurate. As a result, I've written a brief yet straightforward tutorial explaining how to control an LED strip. Even laypeople should be able to accomplish this since it doesn't take much work. There are pictures of each step after it. The tutorial ought to work with any Raspberry Pi model.

I'll show you how this works using an RGB LED strip made from SMD5050 LED chips. Red, green, and blue LEDs are included in each of these chips. The result is a mixed color, depending on which colors are turned on. If a specific color is set, the whole LED strip will typically take that color.

SMD3528 LED chips are also available as LED strips. Every third chip typically has the same color since each of these chips has exactly one LED and just one color. Because of this, SMD3528 LED-Strips cannot be used to produce a mixed color, but they may still be controlled using the techniques described in this tutorial.

lilygo Official Store

The next possibility is LED strips with a WS281X controller (for example, WS2812B), also known as Neopixel. They also have SMD5050 individually controllable chips. Nevertheless, this necessitates a different structure, which I'll describe below.

What is needed?

I will offer links to Amazon for the required components since I am often requested for purchase links. I'm sorry, but I'm unable to offer a link for every nation. Local suppliers are, of course, equally appropriate.

  1. The following packages have to be installed:
sudo apt-get install build-essential unzip wget
  1. PiGPIO can be downloaded and installed via the command chain.
wget http://abyz.me.uk/rpi/pigpio/pigpio.zip && unzip pigpio.zip && cd PIGPIO && sudo make install

Read More: How to Setup a Raspberry Pi: A Beginner’s Guide

1. Connecting the MOSFETs

The MOSFETs take over the real control of the LED strip. As an expert who can handle this better, I won't get into the technical details of a MOSFET's controls; instead, I'll simply discuss how it works with the LED strip.

The voltage at the so-called gate input may be used to operate a MOSFET, which behaves like a switch. The MOSFET behaves like a very big resistor when there is no voltage at the gate input, which means that no current is flowing and the matching color on the LED strip will not illuminate. On the other hand, when 3.3V is supplied, the MOSFET behaves as a very tiny resistor, and the current may flow.

keyestudio Official Store

Now the MOSFET can be connected:

  • Looking at the IRLZ34N MOSFET from the front, then:
    • The first pin from the left is the gate pin.
    • The second pin is the drain pin.
    • The third pin is the source pin.
  • Now all three MOSFETs are plugged into the breadboard.
  • Then the source pins are connected to the ground bus of the breadboard.

2. Connecting the LED-Strip

The LED strip is then connected to the MOSFETs:

  • For this, one of the three colors of the LED strip is connected to the drain pin of a MOSFET.
  • Also, a wire from the 12V pin of the LED strip is plugged into the breadboard's supply voltage bus.

3. Connecting the Raspberry Pi

With the Raspberry Pi, each color of the RGB LED strip requires just one GPIO pin and a common ground pin.

Below is an overview of the Raspberry Pi's GPIO pins:

The GPIO Pins of the Raspberry Pi
  • The ground bus of the breadboard is first connected to one of Raspberry Pi's ground pins.
  • Also, one of the Raspberry Pi's green pins (see figure above) is connected to the Gate pin of a MOSFET for each color.
    • I will use the following pins:
      • GPIO17 for red
      • GPIO22 for green
      • GPIO24 for blue

Read More: How to Setup a Raspberry Pi Pico: A Beginner’s Guide

TSCINBUNY Official Store

4. Connecting the power jack

A secure connection to the power supply is made possible by the power jack, which is required. Never touch the wires while the LED Strip is in use since it may draw a few amps of current! The Raspberry Pi and the RGB LED strip need independent power supplies and must never be supplied with the same!

  • One wire is screwed to the ground and one to the voltage contact of the power jack.
  • The ground wire is connected to the ground bus of the breadboard.
  • The voltage cable is connected analogously to the voltage bus of the breadboard.
  • Then the power supply can be plugged into the power jack.
  • Only then should the power supply be plugged into the socket!

5. Finished

That's it! Here are two pictures of the finished setup:

7. Testing

Using the PiGPIO software, the LED strip can now be tested. For this, the brightness of a color is set in a range from 0 (off) to 255 (full brightness).

You may light up the LED strip by entering the following commands into the terminal of a Raspberry Pi:

  • sudo pigpiod – Starts PiGPIO
  • pigs p 17 255 – The brightness of red (pin 17) is set to 100%, and the LEDs should be red.
  • pigs p 22 128 – The brightness of green (pin 22) is set to 50%, and the LEDs should be yellow.
  • pigs p 24 128 – The brightness of blue (pin 24) is set to 50%, and the LEDs should be purple.

You have connected your LED strip to the Raspberry Pi successfully if all the colors are set. If not, please write a comment, and I'll try to assist you.

What's next?

Your RGB LED strip may now be used for nearly anything. The PiGPIO software may also be used to light up the LED strip, for instance.

The brightness of a color may be set using the PIN command, where BRIGHTNESS should be set in the range of 0 to 255, as previously described, and PIN is set to the GPIO pin number of one of the three colors. Here is an example of code:

import pigpio
pi = pigpio.pi()
pi.set_PWM_dutycycle(PIN, BRIGHTNESS)
…
pi.stop()

To allow the lights to fade, I also created a small script called Fading.py.

To start the script using Python fading.py, just provide the GPIO pin numbers for the three colors.

I hope you enjoyed this lesson. Feel free to leave a comment or send me an email if you have any questions or comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

20 Easy Raspberry Pi Projects Toys, Tools, Gadgets

20 projects using the Raspberry Pi, a tiny and affordable computer, for beginners looking to make cool things, Projects are explained with full-color visuals and simple step-by-step instructions.