LED Color Picker

uses a color gradient to control the LED using colorpicker a color gradiant library. Colorpicker is easy to integrate with and allows you to receive color events in real-time, as you drag the mouse around the gradient.

This solution is written in Java and uses the same serial library (RXTX) as Arduino. it will run on any platform that Arduino supports (Windows/Mac/Linux etc.). RXTX libraries are included in the download so it will run on any of these platforms, out-of-the-box.

The application consists of Java application that serves as the colorpicker interface and an Arduino Sketch that receives RGB colors and applies them to the PWM pins

The basic process flow of the Java application is as follows:

– Open serial port to Arduino
– Initialize colorpicker component and register an event handler
– (In forever loop)
– Wait for colorpicker event.
– Send color to Arduino
– Wait for reply from Arduino (ACK)

The colorpicker runs in a separate thread and sends color events to the main thread.

The Java application sends the RGB colors in a sequence of four bytes: red, green, blue and a EOT byte. The EOT byte signals the end of a color sequence and instructs the Arduino to process the color. Since a byte has 256 values and we need all of them for the color values, I created a escape byte (0x2) to distinguish the EOT byte (0x1) from the color value (0x1). Any time a color value of 0x1 or 0x2 is sent, it is preceded by a escape byte and XOR’d with 0x20. When the Arduino receives the data via Serial.read(), it performs a corresponding XOR operation to un-escape the bytes.

Once the Arduino processes the color, it sends an ACK byte back to the Java application, so that it can send the next color. Without the ACK byte, we run the chance of overflowing the buffer by sending color events faster than the Arduino can process them.

Here’s the Arduino sketch:

// Adjust as necessary to match your RGB LED.  Remember to only use the PWM pins
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

// Indicates what color we are reading next; 0 = red, 1 = green, 2 = blue
int pos = 0;

// red PWM value
int red = 0;
// green PWM value
int green = 0;
// blue PWM value
int blue = 0;

// indicates if next byte should be unescaped
boolean escape = false;

void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);


// turn on LED on low brightness
analogWrite(redPin,   16);
analogWrite(greenPin, 16);
analogWrite(bluePin,  16);

void loop () {

while (Serial.available()) {
int rgb = Serial.read();

if (rgb == 1) {
// end of RGB sequence byte
// reset pos
pos = 0;

// process this color
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

// Send ACK byte so Java app can send the next color

// get next byte
} else if (rgb == 2) {
// escape byte
escape = true;
// discard byte and read next byte

if (escape) {
// unescape byte
rgb = 0x20 ^ rgb;
// reset escape
escape = false;

switch (pos++) {
case 0:
red = rgb;
case 1:
green = rgb;
case 2:
blue = rgb;

To run the app, first download the project from my Google Code project. I’ve included the full Java source code and everything necessary to run the app, sans Java.

Next, connect your RGB LED to the Arduino PWM pins, as specified in the Arduino sketch. Be sure to use appropriate resistors so that you do not draw more current than the Arduino can supply. If you need a RGB LED, here’s a basic one offered by SparkFun . You could also use separate Red, Green and Blue LEDs and diffuse the colors with something like a paper box.

Now upload the sketch to your Arduino.

If you don’t have Java 1.5 or later, now would be a good time to get it. You can find out by opening a command prompt/shell and typing “java -version” If you see something like this then you are all set:

java version “1.5.0_16”
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)

Before running the Java application you need to open the run script for your platform and specify the COM port of your Arduino:

Windows: ledcolorpicker.cmd
Mac/Linux: ledcolorpicker.sh

The COM port should be the last argument on the line that starts with “java”

Now run the script for your platform:

Windows: double-click on ledcolorpicker.cmd (note: if the window closes immediately, open a command prompt and run the script again to get the error)

Mac/Linux: First you probably need to make it executable: “chmod u+x ledcolorpicker.sh”. Now run the script “./ledcolorpicker.sh”

The LedColorPicker application should now start up and be ready to go.

The Arduino processes colors in about 16ms — that is time it takes to send a color to the Arduino and receive an ACK @ 9600 baud. The Arduino can process color events almost as fast as the colorpicker can generate events. In this projects testing, only less than 2% of color events were discarded because the Arduino was busy processing a previous color. You could use a higher baud rate but with this level of performance it’s not necessary. This translates to the Arduino processing about 30 colors changes per second — not bad.

It is possible to make this solution wireless with minimal effort, using with XBee radios. Using the Arduino XBee Shield configured in transparent mode, you could put your LED across the room or anywhere within range and control it from your PC. In this configuration you would need two XBee radios, 1 Arduino XBee Shield and 1 USB-Serial device with XBee socket, such as the SparkFun’s XBee Explorer.


This project is really interesting in that we could control light color so effectively in our project. It could be used to set the atmosphere in accordance to the imagery on screen.

food for thought!!!


February 12, 2010. Uncategorized.

Leave a Comment

Be the first to comment!

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

Trackback URI

%d bloggers like this: