ArduRGB

luca Tuesday November 1st, 2011 0

ArduRGB it’s a simple project to show how to drive an RGB led with Arduino.

In the GitHub repository you can find the Arduino sketch and a sample program written in C# and in the forum you can ask for informations,requests…

Warning: the program described below is designed for a common cathod led, if you need to use a common anode one, the values sent to PWM output have to be substracted from 255

The Arduino program receives via serial communication a message of 3 bytes, each of them is the intensity of the color R (red), G (green) and B (blue). Those values are used to drive the 3 PWM ouputs (pin 9/10/11) connected to the led.

First, define the pins where you connected the led and the array for the receiving message:

// pins definition
const int red_pin = 9;
const int green_pin = 10;
const int blue_pin = 11;
 
// message variables
byte message[3];
int message_index;

In the setup() block, configure serial communication (9600 baud), pins (as output) and the default values (led off):

void setup() {
 
  // init serial communication
  Serial.begin(9600);
 
  // pins configured as output
  pinMode(red_pin, OUTPUT);
  pinMode(green_pin, OUTPUT);
  pinMode(blue_pin, OUTPUT);  
 
  // Start with black value (leds off)
  analogWrite(red_pin, 0);
  analogWrite(green_pin, 0);
  analogWrite(blue_pin, 0);
 
  // reset message position pointer
  message_index = 0;
}

Then, in loop() wait for a new byte coming to serial port, store it in the array and – if it’s the third byte received – send the 3 values to PWM outputs:

void loop() {
 
  // new byte from serial communication
  if (Serial.available() > 0) {
 
    // store received byte in message array
    // and increment pointer
    message[message_index] = Serial.read();
    message_index++;
 
    // if we received 3 bytes, reset pointer
    // and output the values
    if(message_index == 3) {
      message_index = 0;
      analogWrite(red_pin, message[0]);
      analogWrite(green_pin, message[1]);
      analogWrite(blue_pin, message[2]);
    }
  }

The program on your personal computer let you choose a color and send its RGB code through a serial port to Arduino:

As a prototype, I used a common cathod piranha RGB led:

connected to the 3 PWM outputs of Arduino via resistors (1Kohm is ok) to limit current in led:

Here is the prototype and a short video to show how it works:

Leave A Response »