Wednesday, June 12, 2013

Arduino Control RGB LEDs via Serial Port

-->
Arduino Control RGB LEDs via Serial Port ( USB )

Wiring Diagram for Common Cathode
If you want to use common anode. Must change wiring and edit some Arduino code.
RGB LEDs Output ( PWM output )
Arduino Pin 3 connect to Red LED
Arduino Pin 5 connect to Green LED
Arduino Pin 6 connect to Blue LED

Serial Port to Control
Arduino Pin 0 for Rx
Arduino Pin 1 for Tx
Protocal to Control RGB LEDs
Recevied Text data from serial port( pin 0,1 ) to control RGB Led.

Format Command data
R1 G1 B1 , R2 , G2 , B2 <LF>

value
0-255,0-255,0-255,0,0,0<LF>

Return Data
Data Response : RRGGBB    ( hex value )

Example
For Red colour 
Sent text data to Arduino board      "255,0,0,0,0,0<LF>"    then R LED max value
Data Response : FF0000

For Green colour 
Sent text data to Arduino board      "0,255,0,0,0,0<LF>"    then G LED max value
Data Response : 00FF00

For Blue colour 
Sent text data to Arduino board      "0,0,255,0,0,0<LF>"    then B LED max value
Data Response : 0000FF

Warning
Don't use max value ( 255 ) because voltage output = 3.3 V then LED may damage.
Use value between ( 0-180 ) is fine.

Test Arduino Board with Serial Monitor tool on Arduino Environment Software1.0.3

When you program code to arduino board already.
You can test sent command to control colour RGB LED with Serial Monitor tool.


When Arduino Board 've program already.
It sent response text "Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )"


When sent command to Arduino Board



Arduino Source Code for Common Cathode

if you want use Common Anode ( need some edit code ) 


//pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

const int redPin2 = 9;
const int greenPin2 = 10;
const int bluePin2 = 11;

#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6

#define FADESPEED 5

void setup() {
// initialize serial:
Serial.begin(9600);

// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(bluePin2, OUTPUT);

Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");
Serial.print('\n');
}

void loop() {

// if there's any serial available, read it:
while (Serial.available() > 0) {
 
 // look for the next valid integer in the incoming serial stream:
 int red = Serial.parseInt();
 // do it again:
 int green = Serial.parseInt();
 // do it again:
 int blue = Serial.parseInt();

 int red2 = Serial.parseInt();
 // do it again:
 int green2 = Serial.parseInt();
 // do it again:
 int blue2 = Serial.parseInt();

 // look for the newline. That's the end of your
 // sentence:
 if (Serial.read() == '\n') {
           
   // constrain the values to 0 - 255 and invert
   // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
  
   // This is for COMMON ANODE
   //red = 255 - constrain(red, 0, 255);
   //green = 255 - constrain(green, 0, 255);
   //blue = 255 - constrain(blue, 0, 255);
  
   red = constrain(red, 0, 255);
   green = constrain(green, 0, 255);
   blue = constrain(blue, 0, 255);
  
   red2 = constrain(red2, 0, 255);
   green2 = constrain(green2, 0, 255);
   blue2 = constrain(blue2, 0, 255);

   // fade the red, green, and blue legs of the LED:
   analogWrite(redPin, red);
   analogWrite(greenPin, green);
   analogWrite(bluePin, blue);
  
   analogWrite(redPin2, red2);
   analogWrite(greenPin2, green2);
   analogWrite(bluePin2, blue2);

   // print the three numbers in one string as hexadecimal:
    Serial.print("Data Response : ");
   Serial.print(red, HEX);
   Serial.print(green, HEX);
   Serial.println(blue, HEX);
 }
}

}


Download Arduino control RGB LEDs Code
( For USB Version and Bluetooth Version )
http://www.digital2u.net/files/ArduinoControlRGBLed/controlRGBLed.ino -->



Android App to Control Arduino RGB LEDs
Android app on Google Play
https://play.google.com/store/apps/details?id=arduino.control.rgbleds