Controlling RGB LED Color with Arduino and Processing

There are times where RGB LED looks so interesting. The ability to mix the color, mmm, create pixel colors in real world, amazing. But there are also times where you feel you want to have that certain color in your RGB LED, but for some reason, you just couldn’t nail it or you have to go through a tedious process to obtain the perfect value of voltage you have to feed to your RGB LED. I always thought that the process is simply achievable using the Processing’s color picker tool, but I was wrong.

I’ve just had the latter problem, just then. For some reason, the different luminance of each color in my RGB LED makes getting the color that I aim so difficult. Even complete high value in the blue and red pin doesn’t result in magenta. Annoying.

So, I decided to use Arduino and Processing in order to control the voltage input coming in to the RGB so I can mix the perfect amount of each red, green and blue color to get the color that I want. Since this is a simple project, I decided to control it using keyboard, so I can have a precise control of the input voltage (or rather output voltage coming out to the Arduino).

The schematic is dead simple. Connect each value of the RGB LED pin to the PWM output of Arduino (PWM pins: 3, 5, 6, 9, 10 ,11). Make sure you put some resistors between the LED’s pins and the Arduino pin. Refer to the datasheet of your LED to get the proper resistor value. For my project, I use 180 ohm for the Red pin and 100 ohm for the Green and Blue pin.

After that, you can upload this Arduino code to your board:

int incomingByte = 0;
int g;
int b;
int r;

void setup() {
  Serial.begin(9600);
}

void loop() {
  analogWrite (3, g);
  analogWrite (5, b);
  analogWrite (6, r);
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'R') {
      r += 1;
    }
    if (incomingByte == 'S') {
      r -= 1;
    }
    if (incomingByte == 'G') {
      g += 1;
    }
    if (incomingByte == 'H') {
      g -= 1;
    }
    if (incomingByte == 'B') {
      b += 1;
    }
    if (incomingByte == 'C') {
      b -= 1;
    }
  }
}

Then run this Processing code to control the mix of color for your LED. I use the up, down arrow for the Red color; right, left arrow for the green color; and letter b and c for the blue color. So unintuitive but I just want a quick solution. Change those value if you feel comfortable doing it the other way around.

Here’s the Processing code:

import processing.serial.*; 

int r;
int g;
int b;

Serial port; 

PFont font; 

void setup() {
  size(200, 180);
  background (140);
  //opens the serial port connection
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[0], 9600);
  
  //load the font for the text
  font = loadFont("Arial-Black-24.vlw");
  textFont(font);
  smooth();
}

void draw() {
  fill (255, 0, 0); 
  text (r, 30, 90);
  fill (0, 255, 0); 
  text (g, 90, 90);
  fill (0, 0, 255); 
  text (b, 150, 90);
  println ("red " + r  + "green " + g  + "blue " + b);
}

void keyPressed() {
  //for the blue color
  if (key == 'b') {
    port.write('B');
    b += 1;
    background(140);
  }
  if (key == 'c') {
    port.write('C');
    b -= 1;
    background(140);
  }
  if (key == CODED) {
    //for the red color
    if (keyCode == UP) {
      port.write('R');
      r += 1;
      background(140);
    }
    if (keyCode == DOWN) {
      port.write('S');
      r -= 1;
      background(140);
    }
    //for the green color
    if (keyCode == RIGHT) {
      port.write('G');
      g += 1;
      background(140);
    }
    if (keyCode == LEFT) {
      port.write('H');
      g -= 1;
      background(140);
    }
  }
}

This code will help you get the exact value of the RGB LED that you have to mix to get the color that you aim for. In fact, it will show you the precise number of value needed to pass on to those RGB LED pins. So, next time you want to get the color, you just need to write those value to the output pin of the Arduino.

Hope this helps. Just feel free to comment if you need more help or if you find the code’s not working.

Ciao.

Leave a Reply