Arduino RGB LED Control Tutorial

In this tutorial, we’ll create a couple of Arduino RGB LED Control Projects using PWM (analog output). You’ll learn how RGB LEDs work and how to interface Arduino With RGB LED to create color mixing and transition effects. Without further ado, let’s get right into it!

Table of Contents

  1. How RGB LED Works
  2. Arduino RGB LED Interfacing
  3. Arduino RGB LED Color Code Generator
  4. Arduino Code – RGB LED Example
  5. Arduino RGB LED Rainbow Code Example
  6. Project Wrap Up

How RGB LED Works

An RGB LED is basically an electronic device that combines three LED elements in one package. It can emit pure Red, Green, or Blue light each at the same time using a separate input lead and light emitting diode.

By varying the intensity of the light for each color LED, we can achieve millions of possible colors over the entire color spectrum. Given that each LED can have 256 different levels of intensity, the combined RGB LED will therefore have (2563 ≈ 16.77Million) unique colors.

An RGB LED has typically 4 leads (pins). The longest it the ground (common) pin, next to it is the Red LED input, and on the other side there are the Green and Blue inputs. It’s better indicated in the figure below.

Arduino-RGB-LED-Pinout

We use Arduino PWM output to control the RGB inputs for the 3 LEDs (Red, Green, and Blue). By varying the duty cycle for each color input, we can get a desired specific color code as we’ll see hereafter in this tutorila.

Arduino boards have several PWM output pins usually. Those pins are designated with a (~) mark next to the pin number on the board. Before discussing how to use the PWM output pins, let’s first define what is the PWM technique and what are the properties of a PWM signal.

Pulse Width Modulation (PWM) is a technique for generating a continuous HIGH/LOW alternating digital signal and programmatically controlling its pulse width and frequency. Certain loads like (LEDs, Motors, etc) will respond to the average voltage of the signal which gets higher as the PWM signal’s pulse width is increased.

As you can see, the LED gets brighter as the pulse width (duty cycle) increases, and it gets dimmer as the pulse width decreases. And this is typically what we use the PWM output for.

Check the tutorial below to learn more about Arduino PWM. It’s a prerequisite for this project to help you understand the topic in more detail.

???? Also Read
Arduino PWM analogWrite Tutorial

This tutorial will provide you with more in-depth information about Arduino PWM output. Starting from the basics of PWM signal, its frequency, duty cycle, and resolution, and will discuss in detail how it works and how to use it in various Arduino control projects.


Arduino RGB LED Interfacing

Here is the wiring diagram for Arduino with RGB LED. You can use 220Ω or 330Ω current-limiting resistor for each LED color input pin. The complete connection diagram will be as shown below.

Note that I’m using pins (9, 10, and 11) for RGB control because those Arduino pins have PWM output capability.

Arduino-RGB-LED-Wiring-Diagram

To write a color to the RGB LED with Arduino, we’ll create the following function that we can use to directly write any color code that we want and get it shown by the RGB LED without repeating the analogWrite() function calls. We’ll call it setColor() function, and here is how it’s implemented.


Arduino RGB LED Color Code Generator

To first Arduino RGB LED control project would be to give it a specific color code and see if it’s going to generate a light with a similar color or not. But we first need to pick a color and get its (R, G, B) code in decimal or HEX format.

For this, you can use the online RGB Color Code Generator (Color Picker) tool. It’s designed to help you with Arduino or any microcontroller projects that include RGB LED control or RGB LED Strips. Just click the button and select from the color palette, click away and it’s going to update the preview color box and give you the (R, G, B) color code. And it’ll also give you a function call to directly set the RGB color to the one you’ve picked.

For this tutorial project example, you can use this tool below. But I’d recommend checking the RGB Color Code Generator (Color Picker) Tool page and keep it bookmarked because you’ll definitely need it later on.


Arduino Code – RGB LED Example

In this example project, we’ll control an RGB LED with 3 Arduino PWM output pins. To test the RGB LED functionality, we’ll generate a non-primary color code and see how it’ll look on the RGB LED. I’ll test two colors: magenta and cyan(Aqua).

Color Code Generation

Here is how i generated the color codes for (Magenta & Aqua) using the RGB Color Code Generator (Color Picker) tool.

We’ll copy the setColor() function call from the tool with the RGB values that correspond to the desired color we’ve selected. And paste it in the code as you can see in the code example below.

Code Example

Here is the full code listing for this example.

Code Explanation

First of all, we define the 3 IO pins used for the RGB LED output. Those has to be a PWM-enabled IO pins (for UNO: 3, 5, 6, 9, 10, or 11).

And we’ll also define the setColor() function which is going to write the PWM duty cycle values to each color pin respectively. So we don’t have to repeat the analogWrite() function call three times whenever we want to change the RGB color output.

setup()

in the setup() function, we’ll set the pinMode to be output for the 3 pins.

loop()

in the loop() function, we’ll call the setColor() function and give it the color code for the Aqua color (0, 255, 255). Then, we’ll add a small delay before changing the color to Magenta (255, 0, 255). And keep changing the color from Aqua to Magenta over and over again.

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator. You can run it as is, or make a copy and add your own code and start running the simulation to see how it’s going to behave. But anyway it does successfully show the Magenta & Aqua colors exactly as we want.

You can check this simulation project on TinkerCAD using this link.

❕ Note

Note that the RGB LED in the TinkerCAD simulation has a different pin ordering than the actual RGB LED. Be advised that the Blue & Green pins are flipped in the simulation LED which is not the case for a real RGB LED.


Arduino RGB LED Rainbow Code Example

In this example project, we’ll make the RGB LED cycle through the entire colors range like a rainbow LED effect. You can change the delay value to make it go faster or slower as you wish. In a future tutorial, you’ll learn how to achieve the same results without using delay at all, so stay tuned for that!

Here is the full code listing for this example.

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator. You can run it as is, or make a copy and add your own code and start running the simulation to see how it’s going to behave.

You can check this simulation project on TinkerCAD using this link.

Testing Results

Here is a demo video for testing this project on my Arduino UNO board.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Project Wrap Up

To conclude this project tutorial, we can say that you can easily control an RGB LED with Arduino PWM output using the analogWrite() function and 3 PWM output pins. To learn more about Arduino PWM, it’s highly recommended that you check out this Arduino PWM Tutorial. And check the Arduino projects page for more project ideas and code examples.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

For more projects & ideas, check this Arduino Projects resource page.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

How to use RGB LED on Arduino?

To use RGB LED with Arduino, you need 3 PWM output pins. Set the PWM pins to be output pins using the pinMode() function. And finally, write any (R, G, B) color code to the output PWM channels using the analogWrite() function. The RGB LED should light up with the exact same color that corresponds to your input color code.

What is RGB LED in Arduino?

An RGB LED in Arduino is an output device that has 3 light-emitting diodes (LEDs) internally. One for each base color (Red, Green, and Blue). You can control each color’s light intensity using the Arduino PWM output pins. Given that each LED can have 256 different levels of intensity, the combined RGB LED will therefore have (2563 ≈ 16.77 million) unique colors.

How do you code LED RGB in Arduino?

To code an RGB LED in Arduino, you need 3 PWM output pins.
1- Set The PWM pins as output pins using the pinMode() function
2- Connect the PWM output pins to the R, G, and B terminals on the LED
3- Connect the RGB ground lead to the Arduino’s ground
4- Pick a desired color and get its (R, G, B) color code
5- Set the PWM output duty cycle for the 3 pins according to the color code using the analogWrite() function

How does RGB LED work?

The RGB LED has 3 light-emitting diodes (LEDs) internally. One for each base color (Red, Green, and Blue). You can control each color’s light intensity using the Arduino PWM output pins. Given that each LED can have 256 different levels of intensity, the combined RGB LED will therefore have (2563 ≈ 16.77 million) unique colors.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment