Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

Program RGB LEDs with Arduino

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

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.

Materials List

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

Arduino Uno REV3 [A000066]
Arduino Uno is the most used and documented board in the world
$32.00 −$4.05 $27.95

Watch the Full Video Tutorial: Program RGB LEDs with Arduino

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.

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon