Blinking colors with the arduino

The Arduino is an open source project that provides an easy to use micro controller development board running various ATMEL Avrs.

Like all first micro controller projects, this one too is also about blinking a LED (‘The Hello World of embedded systems’). The only difference here is that I tried to step it up to blink different colors using a RGB tricolor LED. Again, nothing original, their is a good tutorial regarding the same on Wiring. The tutorial there uses the same LED as provided by Sparkfun. For me, personally, Sparkfun, along with Pololu, adafruit and acroname are the best places to look for electronics and other related items.

This tutorial just uses the Tricolor LED available at Radioshack. The basic difference between this LED and the other one is that this is just reversed. To drive the LED, it must be powered through the common terminal and the other pins must be held low to complete the circuit.

The complete schematic of the circuit is shown below:

The schematic sketch is created in Eagle (free software) and is amazing, especially after adding the Sparkfun components library.

The basic Arduino board is used and Pins 11, 12 and 13 are wired to the Red, Blue and Green terminals for the LED. Be sure to add resistors to limit the current (prevent overheating etc) depending upon the forward voltage for the LED. The LED above had Red at a lower voltage and hence a higher resistance in series.

The image below shows the connections:

Once the connections are set up, it’s just about writing some code! The arduino platform provides a simple IDE and the language is basically a derivation of Processing. It is straightforward and the arduino homepage has great documentation. There are limits with using Processing with arduino, as internally this still generates C/C++ code that is built into HEX before uploading it to the AVR. There are multiple C/C++ libraries for the arduino that allow for more functionality, but that’s a whole new blog post for another time.

The simplest thing to do is just go through the various example sketches (as they are called in the IDE) and just modify one of them and code up something that will continuously change the colors:

// Tricolor sketch : tekstop : Oct 7, 2010
// Constants
const int PINS[3] = { 11, 12, 13 }; // Pin numbers for Red, Blue and Green
// Globals
int ledStates[3] = { LOW, LOW, HIGH};
long interval[3] = { 500, 500, 500 }; // intervals for LED blinking for Red, Green and Blue
long previousMillis =0;
int I = 0;
int toggle(int state) {
if(state == LOW)
return HIGH;
return LOW;
void setup() {
for(int i=0; i < 3; i++) { // set the digital pins as output:
pinMode(PINS[i], OUTPUT);
randomSeed(analogRead(0)); // To get a random seed for the generator

void loop() { // This code runs continuously

if (millis() - previousMillis > interval[I]) {
I = (I+1) % 3;
previousMillis = millis();
interval[I] = random(1000);
ledStates[I] = toggle(ledStates[I]);
} //EOF

The code just sets up random intervals for the 3 led pins and just toggles them, producing different colors.

This is the most simple set up with this LED. I am just using digital pins that are sending either a high or a low pulse. This LED is capable of a lot more with PWM! Hold on to that!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s