Fading and Mixing Colour LED’s using an Arduino Sketch

17 Nov

For my project I could use a device that glows different colours to represent the weather or notify a person when they have emails etc. An effective method of providing a person with ambient information is to use subtle lighting. In oder to do this I will be working within the Arduino environment. This is an example I got from todbot.com of how to wire three (red, green and a blue) LED’s, then fade and mix them using an Arduino Sketch. Using the serial monitor I can enter a colour command e.g. ‘#ff3344’ to adjust the three LED’s.

Firstly I must wire up the three LED’s to the Arduino Board. Ideally I should use Red, Green and Blue LED’s but unfortunately I only have one Red and two White LED’s for now. Although I can’t achieve different colours I can still prove that the method works.



The image above shows the three LED’s and three resistors.

– The White wires go from one leg of the LED to the Ground Pin on the Arduino (Gnd)

– The Red wire goes to Digital Pin 9 (PWM)

– The Yellow wire goes to Digital Pin 10 (PWM)

– The Blue wire goes to Digital Pin 11 (PWM)

Arduino Board

This is the code for the sketch to upload to the Arduino Board (todbot.com) :

* Serial RGB LED TOO
* ——————
* Serial commands control the brightness of R,G,B LEDs
* Command structure is “#RRGGBB”
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt <tod@todbot.com
* http://todbot.com/

#define slen 7        // 7 characters, e.g. ‘#ff6666’
char serInStr[slen];  // array to hold the incoming serial string bytes

int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11

void setup() {
pinMode(redPin,   OUTPUT);   // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin,  OUTPUT);
analogWrite(redPin,   127);   // set them all to mid brightness
analogWrite(greenPin, 127);   // set them all to mid brightness
analogWrite(bluePin,  127);   // set them all to mid brightness
Serial.println(“enter color command (e.g. ‘#ff3333’) :”);

void loop () {
//read the serial port and create a string out of what you read
int spos = readSerialString();

if(spos==slen && serInStr[0] == ‘#’) {
long colorVal = strtol(serInStr+1,NULL,16);
Serial.print(“setting color to r:”);
Serial.print(” g:”);
Serial.print(” b:”);
memset(serInStr,0,slen);      // indicates we’ve used this string
//spos = 0;
analogWrite(redPin, (colorVal&0xff0000)>>16 );
analogWrite(greenPin, (colorVal&0x00ff00)>>8 );
analogWrite(bluePin, (colorVal&0x0000ff)>>0 );

delay(200);  // wait a bit, for serial data

//read a string from the serial and store it in an array
int readSerialString () {
int i=0;
if(!Serial.available()) {
return -1;
while (Serial.available() && i < slen) {
int c = Serial.read();
serInStr[i++] = c;
return i;

After uploading the sketch onto your Arduino open the serial monitor window (as shown in image below) and type in the RGB value to adjust the brightness of the three LED’s.

Arduino sketch and serial monitor window


One Response to “Fading and Mixing Colour LED’s using an Arduino Sketch”


  1. Fading and Mixing Colour LED’s Using a File on a Server « Ambient Knowledge - August 2, 2010

    […] and Mixing Colour LED’s Using a File on a Server In a recent post I explained how to fade and mix colour LED’s using an Arduino Sketch. I am now going to use […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: