Shopping Cart

⚡️ We’re Helping 1 Million People Build Their First Robots

🇺🇸 Proudly Women-Owned Small Business
code RGB LEDs with Arduino for beginners

Program RGB LEDs with Arduino

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

RGB LEDs can be used to display a variety of colors without the need to wire multiple different colored LEDs at once. In this article, I’ll show you how to wire and program an RGB LED using Arduino for your next technicolor project.

The RGB LED project was featured in Episode 1 of our Special Topics Robotics Course.

Materials List

Here’s a list of what you’ll need for this project:

ARDUINO UNO R3 [A000066]
Arduino Uno is the most used and documented board in the world
$29.95 −$7.09 $22.86

RGB LED Wiring Diagram for Arduino

We can use the wiring diagram to connect the RGB LED to our breadboard. First, place the LED on the breadboard so that all four pins are in different numbered columns.

wire an RGB LED using a breadboard and Arduino Uno fritzing diagram for RGB LED

Most LEDs have a polarity. This one has a common cathode configuration, which means we’re going to be plugging the longer leg into the ground.

Then we’ll connect the remaining legs from left to right (red, ground, blue, green). Double-check your LEDs datasheet to ensure you have the proper configuration.

connect resistors to the RGB LED seat the resistor close to the board

Next, add the resistors to the breadboard. Each color pin needs its resistor. This is to limit the amount of current that the LED is getting so that we don’t burn it out.

Tip: When you place a resistor on the breadboard, make sure it sits flush. Use a pair of snips to trim the resistor before fixing the resistor in the circuit.

Then, take the jumper wires and connect the red pin’s resistor side to the Arduino digital signal pin. Start with the red jumper wire and plug it into pin 11. Connect the Cathode pin to Ground on the Arduino. Then attach the blue pin’s resistor side to pin 10. Finally, run a jumper wire from the green pin’s resistor side to pin 9.

Here’s what it should look like when you’re finished:

RGB wiring to Arduino close-up

If you want, you can also snip the pins on the LED so that they sit closer to the board.

For more permanent projects, I highly recommend trimming the legs to keep them closer to the board. (Keep the ground pin a little longer so that you can keep track of the orientation.)

How to use RGB LEDs

There are many projects you can do with RGB LEDs; however, the most popular is creating light shows. Let’s explore RGB color patterns by using an RGB color picker.

We can create a map between the red, green, and blue values to create custom colors. Then, we can use delays to develop transitions and patterns.

Now that we have everything wired up, it’s time to head on over to the software. If you’ve never used Arduino before this is a good time for you to check out the four steps to write an Arduino Program.

A Brief Overview of Coding Arduino

Arduino populates two methods automatically: setup() and loop(). These methods are required, which means you have to have them in your code for it to compile.

The setup() method is like an initialization routine. The loop() method is similar to the main method. It gets executed from top to bottom sequentially, and then when it gets to the bottom it goes back to the top and it runs through the code again.

Program an RGB LED with Arduino

First, let’s map the pins that are plugged into the signal lines. Create three variables for each LED color. The first pin is for the red color, so I’ll call it redPin and set it to 11.

You can use any of the PWM pins for this, just make sure that all of the signal pins are plugged into a PWM pin so that we can control the colors later. For the blue and green pins, I’ll create variables equal to 10 and 9, respectively.

Next, go into the setup() method and set all of the signal pins equal to OUTPUTS using the pinMode() method. The LEDs are going to be taking information from the controller and sending it to the environment, which is also known as an output.

If you’re confused about inputs and outputs, you can learn more about them here, or you can check out my Arduino for Beginners course that’ll go more in-depth on the different sensor types.

Control Color Patterns with an RGB LED

If you’ve ever worked photo editing or graphics programs like Photoshop or PowerPoint, or if you’ve ever used any tool that has a fill button, you’ll notice that colors are represented using an RGB or a Hex value.

You can use RGB color values to set the color of your RGB LED. All you need to do is set your red, green, and blue values (from 0 to 255), and the LED will light up according to that color code.

First, open this color picker, and then look in the bottom left corner for the RGB values.

use a color picker and hex values to change the colors of an RGB LED connected to Arduino Uno

Play around with the slider wheel and select a color. The closer you are to the red, green, or blue colors, directly, the better the color is going to look on the LED.

That’s based on resolution and also how our eyes interpret colors, but for the most part, the purpose of this activity is to show you color mapping, and then using the Hex codes to create RGB LED color patterns.

Once you have a color you like, jot down (or remember) the RGB values. We’ll use these to program the RGB LEDs in just a minute.

Makeronics Solderless 1660 Breadboard Super Kit -...
✔️Perfect for Arduino Shield Prototyping and Testing. Powered by Makeronics Technology

Program RGB LEDs with Arduino

The Arduino has an 8-bit PWM output command that we can use to write the Hex values to the signal pins on the LED. It’s called analogWrite(...).

Here’s what it looks like:

analogWrite(redPin, value); //set the red pin to a value 0-255, where 0 is off and 255 is on

We’ll repeat this code for each of the colors to create a combination. So, if you wanted to command the RGB LED to red, then you’d use the following lines:

analogWrite(redPin, 255);
analogWrite(greenPin, 0);

Store these in your method, and upload the code to your board. The LED should be red.

RGB LED with Arduino wiring and programming setting the color red with code

If it’s not, be sure that you’ve assigned the proper pin values to the redPin, greenPin, and bluePin variables. Also, verify that each of the pins is connected to PWM pins on the Arduino.

While it’s pretty easy to command colors, writing those same three lines of code repeatedly can become troublesome. I recommend converting these commands into a method that you can call with input parameters.

Rather than writing a single line to command each color, we can write one line that includes all three values for red, green, and blue. Let’s store this in a method called setColor(...).

void setColor(int red, int green, int blue){
    analogWrite(redPin, red);
    analogWrite(greenPin, green);

Then, if we want to set the RGB LED to green, we can use the command as follows:

setColor(0,255,0); //set the RGB color to red value, green value, blue value

And that’s it! Call the method in loop() and upload the code to your board.

change the color of an RGB LED using Arduino and hex color codes

You can use this method and a for-loop to fade the LED different colors and add delays to control how fast the sequence is.

Combine this project with the random() method to create custom light shows.

Did you try this project? Let me know what you made in the comments below.
You can also watch the full lesson from Special Topics Robotics Course when you enroll in the Free Robotics course.

Support Content Like This

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying a coffee.
If you'd like to work with Liz, you can schedule a call here.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Learn Robotics Botly Favicon

Become a Learn Robotics Insider


Be the first to know when we launch new robotics content. Plus gain FREE robotics tips to your inbox.

Awesome! Check your inbox for a surprise!

Remote Robotics Courses for High School, Middle School, or Pre-College Training

Join the Powered by Learn Robotics Program

Teach robotics anywhere with our turnkey robotics program!

Download our free program brochure!

Success! Check your email for the download!