Tutorials

Playing with RGB LEDs on Arduino

Introduction

Regular LEDs are great, but I have such a hard time choosing the right color for a project! Sometimes one color just isn’t enough. What if we could decide the color of the LED on the go? Better yet, what if we could change the color of the LED multiple times at code level, turning it into a more meaningful output component? Yes, we can! RGB LEDs are there for us. And they are A-M-A-Z-I-N-G.

This tutorial will show you the basics of working with RGB LEDs, and how to create a fun “breathe” effect using an Arduino Micro.

If you haven’t played with regular LEDs before, I strongly recommend you to check my tutorial: Getting Started with Arduino: led blinking on Arduino Micro Tutorial. This will help you understand the basics about LEDs and how to control them at code level.

What you are going to need for this tutorial

  • Breadboard (I’m using a mini breadboard here)
  • Arduino Micro
  • RGB LED (can be either common anode or common cathode)
  • 3x 220 ohm resistor
  • breadboard jumper wires

Understanding the Components

BreadBoard

A Breadboard is used to prototype new projects without the need for soldering. You just need to plug / connect the components to create your circuit. Pretty much like a “development environment” you might be used to, it’s ideal to start and develop a project. It’s also a great way  for just playing around with no strings attached.

Learn More

Arduino Micro

The Arduino Micro is a very compact microcontroller board based on the ATmega32U4 microcontroller.

Learn More

LED

LED comes from  “light-emitting diode”. LEDs have two legs, one positve (anode) which is usually the longer one, and a negative (cathode) which is usually the shorter leg. You will normally connect the positive leg of an LED to a resistor (around 220 ohm or higher up to 1k) and the negative leg to GND.

Learn More

Resistor

Resistors are a fundamental electronic component. They are used to limit current flowing through a circuit. Resistors can have many different pre-defined values, measured in ohms. The bigger the value, the bigger the resistance, which means less electrons passing through.

Learn More

Jumper Wires

Jumper wires are simple reusable wires that can be easily connected to the breadboard and components for prototyping. 

Learn More

Getting the environment / IDE ready

First things first: “plug” the Arduino board to the breadboard, making sure to position each side of pins in a different side of the breadboard, so they don’t short-circuit. It should look similar to this:

You can now connect the USB cable to the board in order to turn it on.

Go to the Arduino website and download the Arduino IDE.  Once you get it installed, select the right board in the menu “Tools ->  Board”. For the Arduino Micro, you should select “Arduino / Genuino Micro”.  You might need to also select the port for communication, under the menu “Tools -> Board -> Port”. The port selection works only when the board is connected.

If you turn the board on by plugging the USB cable, you should be able to get info about it by going to the menu “Tools -> Get Board Info”.

The Board Pinout

It’s always interesting to have at hand the board pinout. It will show you details about the board, the available pins and other information.  You can find pinouts for all the most popular boards easily with a Google search – you can even go to “images” directly to get what you need quickly.

This is how a simple pinout looks like for the Arduino micro:

The RGB LED

Even tough you can’t easily notice this from the outside, RGB LEDs are, in fact, composed by 3 tiny regular LEDs: a red one, a green one, and a blue one. They have 4 leads, one for each channel and another one (usually, the longer lead) that can be either ground or power, depending on the type of the RGB LED you have. A common anode LED must be connected to power (5V or such) while a common cathode LED must be connected to GND.

Common Anode x Common Cathode

It’s easier to visualize the difference in practice. With the common anode LED, the “off” state of the pin (0V) actually translates to 255, so if you have the RGB value of [0, 255, 0], you’ll have at pin level the state [ HIGH, LOW, HIGH ]. It’s sort of inverted. With the common cathode LED, the same green color will be represented at pin level as [ LOW, HIGH, LOW ].

The Circuit

Now let’s plug the LED into the breadboard and create a little circuit to connect it to the Arduino Micro. We’re going to need 3 pins to individually control the R, G and B channels. To have a nuanced output, we will be using PWM ports – they basically emulate an analog output. This way we can variate the levels of each channel, instead of having just either 0 or 255 as values.

I chose pins 9, 10 and 11 as they are all PWM pins and closer to each other. I’m using a common cathode LED, so the other lead should be connected to GND. Here is how my circuit looks like IRL:

Making Things Happen

First example: All Green

Now that we are all set up, let’s write some code and play around with this LED. We’ll start with a simple code that should turn the LED green (0, 255, 0) when the Arduino board is booted:

/*
 RGB LED example - codingmama.io
*/

int pinR = 9;
int pinG = 10;
int pinB = 11;

void setup() {
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinB, OUTPUT);
}

void loop() {
  analogWrite(pinR, 0);
  analogWrite(pinG, 255);
  analogWrite(pinB, 0);
}

If the circuit is right and you have a common cathode RGB LED, this should light up the LED in a green color. If your LED is a common anode, thou, your LED should be magenta / purple-ish (255, 0, 255) with this same code, however you gotta make sure you connect the extra lead to 5v or such, instead of GND as I did in my circuit.

Second Example: Just Breathe

Now, let’s test all channels and make it a little more interesting by making use of the PWM capabilities and modulating the output. It sounds all very complicated, but in fact it just means we’re going to use values other than just the binary 0/1 or 0/255.

The code below implements a little “breathe” function that can be run in a channel per time:

/*
 RGB LED example - codingmama.io
*/

int pinR = 9;
int pinG = 10;
int pinB = 11;

void setup() {
  // put your setup code here, to run once:
  pinMode(pinR, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinB, OUTPUT);

  setColor(0, 255, 0);
}

void loop() {
  setColor(0, 0, 0);
  
  breathe(pinR);
  breathe(pinG);
  breathe(pinB);
}

void setColor(int r, int g, int b) {
  analogWrite(pinR, r);
  analogWrite(pinG, g);
  analogWrite(pinB, b);
}

void breathe(int channel) {
  for (int i=0; i<=255; i++) {
    analogWrite(channel, i);
    delay(10);
  }

  for (int i=255; i>0; i--) {
    analogWrite(channel, i);
    delay(10);
  }
}

And this is the little demo video:

How fun is that? Can you think of a million things you could build using RGB LEDs? Because I certainly can!

Keep an eye on my Twitter for the upcoming interactive LED lamp tutorial.

2 thoughts on “Playing with RGB LEDs on Arduino”

  1. Thanks for sharing this, Erika. 🙂 As a software developer with an academic background 100% in CS I have basically zero knowledge about electronics. Therefore I am glad I found this blog, it’s very well written and easy to understand.

Leave a Reply

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