# The world is colorful – part 1 of 3

In this and two other following posts, we will introduce some fun projects related to generating and detecting colors.

It is actually pretty easy to get some color from Arduino.  In this post we will show you how to use an Arduino Nano board to control an RGB LED and generate millions of colors.

A. Materials

1. Arduino Nano board x 1
2. Breadboard x 1
3. RGB LED x 1
4. 150 Ohm resistor x 3
5. Jumper wires
6. USB cable (A plug to mini-B plug) x 1

B. Key notes

The Arduino Nano board has 14 digital I/O pins, of which 6 pins can provide PWM output.  Each I/O pin can provide 40mA DC current. We will use pins 3, 5 and 6 to control LED intensity with PWM.

The RGB LED we use consumes about 20mA forward current for each anode pin.  If the current is too high, both the LED and the Nano board may be damaged.  Therefore, we should use resistors to limit the current.  Since the output voltage of a Nano pin is 5V, the voltage drop on the LED is about 2~3V, then the voltage on the resistor is about 2~3V.  To get ~20mA current, the resistor value should be about 150 Ohm.

C. Diagram

The connection of the components is shown below: In this diagram, U1 is the Nano board, L1 ~ L3 are the 3 diodes in the RGB LED, R1 ~ R3 are 3 resistors.  The digital output pins D3, D5 and D6 of the Nano board are used to drive the RGB LED, and the cathode of the LED is connected to the GND pin of the Nano board.

The structure of a typical 5mm diameter RGB LED is shown below, where pins 1, 2, 3, 4 are red, cathode, blue, and green pins, respectively. D. Sketch

```/*
RGB LED Control

This sketch demonstrates how easily one can use
PWM digital output pins to drive an RGB LED
and generate millions of colors.

Open Source Photonics
osphotonics@gmail.com

8/14/2014
*/

// define digital output pins
int r = 6;
int g = 3;
int b = 5;
int delayTime0 = 1000;  // initial R,G,B ON time in miniseconds
int delayTime1 = 5;  // ON time for each color in miniseconds
int vr0 = 0;   // initial R G B values
int vg0 = 0;
int vb0 = 0;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);

// control the RGB LED
analogWrite(r, 0);   // turn the red color off
analogWrite(g, 0);   // turn the green color off
analogWrite(b, 0);   // turn the blue color off

delay(delayTime0);   // wait for a while

analogWrite(r, 255);   // turn the red color on
delay(delayTime0);     // wait for a while
analogWrite(r, 0);     // turn the red color off

analogWrite(g, 255);   // turn the green color on
delay(delayTime0);     // wait for a while
analogWrite(g, 0);     // turn the green color off

analogWrite(b, 255);   // turn the blue color on
delay(delayTime0);     // wait for a while
analogWrite(b, 0);     // turn the blue color off

delay(delayTime0);     // wait for a while

while(vr0+vg0+vb0 < 255) {  // initial random color should be
vr0 = random(256);        //  bright enough
vg0 = random(256);
vb0 = random(256);
}

// set initial random color
analogWrite(r, vr0);   // turn the red color on
analogWrite(g, vg0);   // turn the green color on
analogWrite(b, vb0);   // turn the blue color on

delay(delayTime1);     // wait for a while
}

// the loop routine runs over and over again forever.
void loop() {
int colorStep;  // step of color change
int vr,vg,vb;  // new R G B values

colorStep = random(10);  // step can vary from 0 to 9

// modify color with small steps
vr = vr0+(random(3)-1)*colorStep;
vg = vg0+(random(3)-1)*colorStep;
vb = vb0+(random(3)-1)*colorStep;

while(vr+vg+vb < 255) {  // new color should also be
vr = vr0+(random(3)-1)*colorStep;  // bright enough
vg = vg0+(random(3)-1)*colorStep;
vb = vb0+(random(3)-1)*colorStep;
}

// clean up vr, vg, vb so they are within [0,255]
if (vr > 255) vr = 255;
if (vg > 255) vg = 255;
if (vb > 255) vb = 255;
if (vr < 0)  vr = 0;
if (vg < 0)  vg = 0;
if (vb < 0)  vb = 0;

// generate new color
analogWrite(r, vr);
analogWrite(g, vg);
analogWrite(b, vb);

// update old color values
vr0 = vr;
vg0 = vg;
vb0 = vb;

delay(delayTime1);
}

```

E. Result and discussion

The wiring of this project is very simple. The following picture shows how the final product looks like. After uploading the sketch from your computer to the Arduino Nano board, you can enjoy the colorfulness of your project. The LED will first display three solid colors: Red, Green and Blue, each for a second, and then it will continue to display various random colors, with millions of options.

If you need help on how to upload the Arduino sketch, please refer to our post “Instruction on how to connect your Arduino board to a computer and make it work”.

Have fun!

Thanks for reading.  Open Source Photonics Blogs are supported by 612 Photonics.

Advertisements