1
Arduino Lesson 3. RGB LEDs
Created by : Mr. Leng Por
Email: por_leng@yahoo.com
Institute of Electrical Science
Last updated on 2016-11-18 8/15/2017
2 Guide Contents
Overview
Parts
Part
Qty
Breadboard Layout
Colors
Arduino Sketch
Using Internet Colors
Theory (PWM)
Other Things to Do
8/15/2017
3 Overview
In this lesson, you will learn how to use a RGB (Red Green Blue) LED with an
Arduino.
You will use the analogWrite function of Arduino to control the color of the
LED.
8/15/2017
4 Overview
At first glance, RGB (Red, Green, Blue) LEDs look just like
regular LEDs, however, inside the usual LED package,
there are actually three LEDs, one red, one green and
yes, one blue. By controlling the brightness of each of
the individual LEDs you can mix pretty much any color
you want.
8/15/2017
5 Overview
We mix colors just like you would mix audio with a 'mixing
board' or paint on a palette – by adjusting the brightness
of each of the three LEDs. The hard way to do this would
be to use different value resistors (or variable resistors) as
we played with in lesson 2. That's a lot of work!
Fortunately for us, the Arduino has an analogWrite
function that you can use with pins marked with a ~ to
output a variable amount of power to the appropriate
LEDs. 8/15/2017
6 Parts
To build the project described in this lesson, you will
need the following parts.
8/15/2017
7 Parts
8/15/2017
8 Parts
8/15/2017
9 Breadboard Layout
The RGB LED has four leads. There is one lead going to the positive
connection of each of the single LEDs within the package and a single
lead that is connected to all three negative sides of the LEDs.
8/15/2017
10 Breadboard Layout
The common negative connection of the LED package
is the second pin from the flat side of the LED package.
It is also the longest of the four leads. This lead will be
connected to ground.
Each LED inside the package requires its own 270Ω
resistor to prevent too much current flowing through it.
The three positive leads of the LEDs (one red, one green
and one blue) are connected to Arduino output pins
using these resistors. 8/15/2017
11 Breadboard Layout
8/15/2017
12 Colors
The reason that you can mix any color you like by varying the
quantities of red, green and blue light is that your eye has three
types of light receptor in it (red, green and blue). Your eye and
brain process the amounts of red, green and blue and convert it
into a color of the spectrum.
In a way, by using the three LEDs we are playing a trick on the eye.
This same idea is used in TVs, where the LCD has red, green and
blue color dots next to each other making up each pixel.
8/15/2017
13 Colors
8/15/2017
14 Colors
If we set the brightness of all three LEDs to be the same, then the
overall color of the light will be white. If we turn off the blue LED, so
that just the red and green LEDs are the same brightness, then the
light will appear yellow.
We can control the brightness of each of the red, green and blue
parts of the LED separately, making it possible to mix any color we
like.
Black is not so much a color as an absence of light. So the closest
we can come to black with our LED is to turn off all three colors.
8/15/2017
15
Arduino Sketch
The following test sketch will cycle
through the colors red, green, blue,
yellow, purple, and aqua. These colors
being some of the standard Internet
colors.
8/15/2017
16 Arduino Sketch
8/15/2017
17 Theory (PWM)
Pulse Width Modulation (or PWM) is a technique for controlling
power. We also use it here to control the brightness of each of the
LEDs.
The diagram below shows the signal from one of the PWM pins on
the Arduino.
8/15/2017
18 Theory (PWM)
8/15/2017
19 Theory (PWM)
Roughly every 1/500 of a second, the PWM output will
produce a pulse. The length of this pulse is controlled by
the 'analogWrite' function. So 'analogWrite(0)' will not
produce any pulse at all and 'analogWrite(255)' will
produce a pulse that lasts all the way until the next pulse
is due, so that the output is actually on all the time.
8/15/2017
20 Theory (PWM)
If we specify a value in the analogWrite that is somewhere in
between 0 and 255 then we will produce a pulse. If the output pulse
is only high for 5% of the time then whatever we are driving will only
receive 5% of full power.
If however the output is at 5V for 90% of the time then the load will
get 90% of the power delivered to it. We cannot see the LEDs
turning on and off at that speed, so to us, it just looks like the
brightness is changing.
8/15/2017
21
End
8/15/2017