What you’ll need:
- A Raspberry Pi with an operating system that is attached to a regular computer setup, or connected via SSH
- A breadboard
- 2-3 male-to-female jumper wires (2 for a single LED and 3 for an RGB LED)
- An LED and/or an RGB LED-strip (our tutorial uses a common anode RGB LED)
- 1-3 resistors (1 for a single LED up to 1000ohms) (3 for an RGB LED – calculate required ohm levels here – see resistors section for help)
- Hookup cables
This tutorial will teach you how to control an RGB LED with the Raspberry Pi. We’ll go over the Raspberry Pi breadboard, how to run a single LED, and how to run an RGB LED with customizable options. This should be sufficient for both beginners and users with more experience. Depending on how much experience you have with Raspberry Pi circuitry, you can navigate through the sections below to get to the instruction best suited for your level. If you’ve never created a circuit setup with your Raspberry Pi before, you should read the entire tutorial.
Section 1: Background Knowledge of Raspberry Pi Circuitry
Section 2: Making a Single LED Circuit
Section 3: Making an RGB LED Circuit
Section 1: Background Knowledge
While it will be helpful to have familiarity with the setup of a breadboard and the basic anatomy of your Raspberry Pi, we’ll explain the necessary knowledge related to our tutorial as we go through each step. Feel free to skip down to Section 2 if you are already familiar with this setup. Connecting your Raspberry Pi to a display will be required for the coding part of this tutorial. If you don’t know how to do that, you can read this article to learn how to connect to a display or remotely via SSH.
First, let’s go over the basics of a breadboard. Breadboards control electrical currents. We will be using a solderless breadboard, meaning that you can move your components around as many times as needed without having to solder and desolder them. The breadboard is covered in a number of organized holes, laid in rows and columns. Rows are labeled by numbers, and each row on the left corresponds to each row on the right. Columns are labeled A-J, vertically separated by ravine; A-E lies on the left side of the ravine, and F-J lies on the right. However, the holes are just access points to what lies underneath. A metal strip acting as a conductor lies underneath each row on both sides of the ravine. The ravine is the line that vertically separates the two sides of the breadboard. Finally, the two sets of “buses” on each side of your breadboard connect to the positive and negative ends of the supply. To complete a circuit, they are connected using jumpers.
Resistors control the current, which is necessary to run a circuit without the risk of destroying the device. Since we are connecting to a 3V, without a resistor, the LED would try to set a corresponding current that would destroy the LED. Simply reducing voltage supply is not adequate because it is not precise enough, as even the smallest differences in supply voltage can negatively affect the LED. When using a single LED, only one resistor is required, and that can be any value up to 1000 ohms. However, when using an RGB LED, each color is treated as an individual LED and requires a different level of resistance. To determine which resistors you need, check the LED values for each color and plug them into this calculator. 220 ohms for each resistor can be used for a simple setup, but it will slightly limit the output. For basic color purposes, it should be fine.
Section 2: Creating a Circuit with a Single LED
Step 1: Connecting the LED
First, we have to connect the LED to the breadboard. On row 1, insert the positive leg (anode) on the left side of the ravine and the negative leg (cathode) on the right. The positive leg is longer than the negative leg. On some LEDs, the legs are the same length and can be differentiated by looking at the round and flat edges. The negative leg is on the flat edge.
Step 2: Attaching the resistor
Next, you will attach your resistor by connecting either leg anywhere on the same row that the LED is in (row 1). This assures that it will connect to the LED. The other leg will go into any other open row on the right side of the ravine.
Step 3: Attaching the jumper to the 3V3 GPIO pin
Insert the male end of the male-to-female jumper wire into the same row as the LED, row 1, on the left side of the ravine. Connect the female end to the 3V3 GPIO pin. The following visual from the Raspberry Pi Projects site provides a good visual of what your circuit should look like:
Step 4: Connecting a second jumper to the GND GPIO pin
Power on your Raspberry Pi. Then, attach the male end of the second jumper on the right side of the ravine, into the same row as the resistor’s second leg (the leg that is not on row 1). Connect the female end into the GND pin, completing the circuit.
Your LED should light up!
Section 3: Creating an RGB LED Circuit
Step 1: Attaching the RGB LED
First, let’s take a look at our RGB LED. It has four legs, with one leg being longer than the other three. The longest leg is called the positive leg, which we will connect to the power supply. The other three legs correspond to each RGB color. This is a common anode type RBG.
Place the RGB onto the breadboard. It can go anywhere. For the sake of this tutorial, we will be arranging the RGB so the positive leg is on the right. Next to the positive leg, on the far right, is the red leg.
Step 2: Attaching the power supply jumper cable
Plug one of the jumper cables into one of the holes on the same row as the positive leg on the RGB. This cable will be used for the power supply, so if you have them labeled, make sure that you use the one specified for the power supply.
Step 3: Attaching the resistors
Plug a resistor into the same row as the red leg of the RGB. Again, this leg is on the far right next to the positive leg. One end of the resistor will be on the same side of the ravine as the RGB LED. The other end of the resistor will cross over to the other side of the ravine. We will keep it in the same row as the RGB LED on both sides.
Plug the green resistor into the row with the green leg. This is to the left of the positive leg. Cross the other end of the resistor over the ravine and plug it into the same row.
Finally, plug the blue resistor into the row with the blue leg. This is the farthest leg to the left, next to the green leg. Cross the other end of the resistor over the ravine and plug it into the same row.
Step 5: Finishing the connection to the Raspberry Pi via GPIO pins
Now, we’ll set up our connection to the Raspberry Pi. We will be using three different GPIO pins on the Raspberry Pi. The power supply will be connected to the 3V pin on the top right corner of the Raspberry Pi’s GPIO pins, via a male to female jumper wire.
The 6th pin from the right (where the power supply is plugged in) is GPIO 17. This is where we will plug in the jumper cable for the red part of the RGB. The other end of the jumper cable will be placed in the same row as the red leg of the RGB LED, but on the opposite side of the ravine (next to the second end of the red resistor.)
The 7th pin from the right, next to GPIO 17, is GPIO 27. This is where we’ll plug in the cable for green. The other end of the jumper cable will be placed in the same row as the green leg of the RGB LED, but on the opposite side of the ravine (next to the second end of the green resistor.)
Finally, GPIO 22 is the 8th pin from the right, which is next to GPIO 27. This is where we’ll plug in the cable for blue. The other end of the jumper cable will be placed in the same row as the blue leg of the RGB LED, but on the opposite side of the ravine (next to the other end of the blue resistor.)
Step 6: Controlling the LED with code
The RGB LED will be controlled by the user via code. This code can be written in several ways, so feel free to experiment with various methods and libraries that might help you customize the code to your liking. Our code is written in Python using GPIOZero, which is a newer library and can be simpler for users to read and write. However, RPi GPIO is another common library. They are both used to code for GPIO pins. Most of the code used for LED control and Raspberry Pi operations is in Python. If you’re unfamiliar with this programming language, Python and general programming basics can be explored in our Python fundamentals article.
Section 2.17 under the GPIOZero documentation shows how to run a full color LED with an RGB LED setup. There are also instructions for adding features like blinking lights or a button controlled camera. The basic recipes move in order from simplest to more complicated, so if you start from section 2.1 and move through to the section you are interested in, you should have a good understanding of what you’re coding and what each line does.
Alternatively, there are plenty of pre-written codes to run the RGB LED, including the basic one shown in section 2.17, so feel free to copy and paste the code if you’d prefer. Once your Raspberry Pi is connected and the code is executed, the LEDs should run the way you’ve specified in your code.
In the future, we’ll be posting more Raspberry Pi tutorials that build off of the knowledge learned from previous articles. Keep your eyes open for our next tutorial, which will teach users how to connect the RGB LED to their live Twitch streams!
As always, you can reach out in our comments section below if you have any questions or suggestions!
Previous Raspberry Pi-related tutorials:
How to Connect Your Raspberry Pi to a Display and Use SSH to Remotely Connect to Your Raspberry Pi