RGB LED, Processing, Firmata and Arduino

My wife plays in a band and some of us started to get the idea of rigging up an led light system for the stage. I figured this would be the perfect project for the arduino. I bought a RGB led here (for $6 AUD from Jaycar, an absolute ripoff) and decided to have a go controlling it. I’m starting small with just one for now and eventually I want to create a large matrix.

If you haven’t worked with an RGB led they are a little different that a normal LED. They contain 4 legs, one for each colour and a common anode or cathode. In my case I had a common cathode. I plugged it into my breadboard and wired up a 1K resistor to limit the current.

I’m using PWM on the arduino to control the brightness of each LED. Here’s a look at the code:

int ledpinr = 9;
int ledping = 10;
int ledpinb = 11;
int red = 0;
int blue = 0;
int green = 0;

void setup() {
  //Nothing to setup
} 

void changeColor(int red, int green, int blue){
  //Set PWM on pins 9 10 and 11
  analogWrite(ledpinr, red);
  analogWrite(ledping, green);
  analogWrite(ledpinb, blue);
  delay(500);
}

void loop() {
  //Red
  changeColor(255, 0, 0);

  //Green
  changeColor(0, 255, 0);

  //Blue
  changeColor(0, 0, 255);

  //White
  changeColor(255, 255, 255);
}

I know, pretty basic. We set up our led pins on 9, 10, and 11 and change the PWM value on each one and call a half second delay.

Processing + Arduino + Firmata = Cool

Now lets do something a little more complicated. Lets see how we can control the rgb led using our computer. To do this we’ll need to set up a small program in processing and get it to send serial data to the arduino.

We’ll be using Firmata, a library for the arduino to communicate with processing with only a few lines of code. You’ll need to download the Firmata library and place place it in your arduino library folder. Instructions to do this can be found on the arduino website. After downloading unzip the package and view the README.txt. Once you’ve finished that we’re ready to code.

We are going to use one of the included examples for Firmata. Here is the Arduino code:

/* Supports as many analog inputs and analog PWM outputs as possible.
 *
 * Written by Hans-Christoph Steiner
 */
#include 

byte analogPin;

void analogWriteCallback(byte pin, int value)
{
    pinMode(pin,OUTPUT);
    analogWrite(pin, value);
}

void setup()
{
    Firmata.setFirmwareVersion(0, 1);
    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
    Firmata.begin();
}

void loop()
{
    while(Firmata.available()) {
        Firmata.processInput();
    }
    for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
        Firmata.sendAnalog(analogPin, analogRead(analogPin));
    }
}

This will receive the serial data sent to us from the processing sketch that we’ll be making next. Most of the hard work is done us by the Firmata library. If you’d like to see more about how their functions look checkout the Firmata.cpp file in the library.

Now for the processing code. This will draw a 512×200 box. When we move our mouse in this box it will use the coordinates of the mouse to change the color of the background and the rgb led hooked up to the arduino.

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

int red;
int green;
int blue;

void setup() {
  size(512, 200);
  arduino = new Arduino(this, Arduino.list()[2], 115200);
}

void draw() {
  red = constrain(mouseX/2, 0, 255);
  green = constrain(255 - mouseY, 0, 255);
  blue = constrain(255 - mouseX/2, 0, 255);
  background(red, green, blue);
  arduino.analogWrite(9, red);
  arduino.analogWrite(10, green);
  arduino.analogWrite(11, blue);
}

There you have it. Controlling an rgb led with processing and firmata using an arduino. Next we’ll be looking at using a TLC5940 to expand our PWM outputs and control more leds.

Recommended Reading:

  • phil

    This is a well written and interesting article, I am new to both arduino and processing so I appreciated it. Thank you.

  • Sed

    Hi, I am curious to your following to this as you mentioned ‘Next we’ll be looking at using aTLC5940 to expand our PWM outputs and control more leds.’, but can’t find it anywhere? Many thanks!

  • niko

    hi!..can i have the circuit of the first example reported in this page???thanks

  • Mahdi

    just wanted to thanks and say that the latest Arduino software include the Firmata libraries .

  • Mahdi

    if you have just one Arduino connected to your PC,then change that line to :

    arduino = new Arduino(this, Arduino.list()[0], 115200);

  • fran

    hey, im new to arduino. are these codes are ran(or inputted) within the arduino sketch? :s thank you.

  • http://makesomecode.com Nick

    The last chunk of code goes into a processing sketch. Go to processing.org to download.

  • http://www.hotcaricatures.com Kostas

    Nice code… but…
    My RGB led is not lighting!!!
    At processing i get the message
    WARNING: RXTX Version mismatch
    Jar version = RXTX-2.2pre1
    native lib Version = RXTX-2.2pre2

    Is that the problem??

  • http://www.hotcaricatures.com Kostas

    I fixed the RXTX Version mismatch error
    But i still dont get the rgb led working
    I am very new at arduino so pls try to get me understand
    I have to run the above code at Processing and also at arduino??

  • http://makesomecode.com Nick

    The second block of code is for arduino, the third block is for processing.

  • Pingback: Example led controlled with arduino « rosiemcdonnell