Introduction: Controlling an RGB Led With Arduino and Processing

Picture of Controlling an RGB Led With Arduino and Processing

This instructable (my first no less)  shows how to control an RGB led using processing and arduino.

The key feature being the colour is selected by clicking on an image, the RGB value of the pixel you click on is processed and sent to the audrino which then outputs that same value.

Obviously the match isn't perfect especially at darker colours as I'm yet to see any LED do black very well, but the project should hopefully provide some insight to those of you starting with arduino and other microcontrollers.

Feel free to ask any questions in the comments and I'll answer them as best i can.

Also as i am new to instructable publishing let me know any improvements i could make on this or future instructables.

Video below shows the final result: (if it wont play right click and go to watch in youtube)

Step 1: What You Need

Picture of What You Need

Arduino or similar
1 x RGB led (Dispersed, i used some super fine sand paper as the one i had was water clear)
4 x Connection Wires
1 x Breadboard

3 x Resistors if you plan to leave this on for extended periods.

use this tool to calculate resistors specific to your LED


(This can be done in other software packages but this instructable is to get people started with arduino)

Step 2: Setting Up the Hardware

Picture of Setting Up the Hardware

 Refer to the images below for how the equipment is set up.

Note: My RGB Led is common cathode, if yours is common anode the second pin should be connected to a voltage supply not ground.

Step 3: Software

Picture of Software

So whats going on. basically we've set the arduino up to respond to a particular message. this message consists of a header (the letter "C") a dud byte (some strange serial problems caused this to be implemented) and the bytes of data (R G B respectively)

All processing is doing is generating these messages based on a pixel in an image, when we click the mose location is recorded and the message is built and sent to the audrino over a serial connection.

See the attached files for the software for this project, hitting run in processing should bring up a screen like the one below, although it will depend on what image you choose to load.

ColourMap goes in processing

RGB_LED_arduino_code goes in (you guessed it) arduino.

Make sure you load this onto the arduino before you run the processing file or it wont do anything.

if you having trouble check the following:

Is the arduino plugged in
is the arduino power light on
have you wired the led correctly
have you chosen the correct serial port in processing

if your still having trouble post a comment below and I'll do my best to answer it.

Step 4: Enjoy

Picture of Enjoy

 Enjoy your new RGB Led. To extend this you could:

-add more leds
-add xBees 
-Add some nice fading effects between colours

the options are limitless.

hopefully this has helped some people to understand arduino and processing a little better.


bikeboy944 (author)2016-12-15

i cant download the files. it comes only FZFOSYCG5FRGO1O.tmp

JimM106 (author)bikeboy9442017-01-28

They are text files. Just change the names.

Alexander& made it! (author)2016-10-14

Thank you....i'm trying to use processing, kinect and arduino for a project and this helped me to get started :)

Jibble696 made it! (author)2016-09-26

thank you it is very useful for learning

Isaias AlejandroS (author)2016-08-06

-descargar imagen completa "FF17LKFG5FRGO1J.MEDIUM.jpg"
-convertir formato a .png y cambiar nombre a "RGBR"
-colocarlo en la carpeta del sketch y run ;)

jpindi (author)2013-03-15

I'm getting the following NullPointerException error:

"The file "RGBR.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable."

It breaks at 


Anyone had this problem, before? Thank you!

WallyWorld23 (author)jpindi2016-05-19

having the same issue, did you ever figure this out?


jpindi (author)jpindi2013-03-15

Also should have mentioned, Mac OSX. Thanks!

zebu14 (author)2015-11-10


I used your code for a project without use of "Processing"

I added an LCD to show the red green and blue value by printing "one[1"...."one[3]

When I send "C" or "CL" followed by values like 255 128 128 (for example), or anything else, everything that shows up are "0" on the screen.

I tried lots of methods : "CLFF00FF", "CFF00FF" or C128255128 etc... without success.

Any idea ?

I uploaded a sketch on autodesk simulator (only the micro arduino is used for now) if you want to have a look :

The code of the main loop :

void loop()
if (Serial.available() > 0) {
// get incoming byte:
inByte =;

if (inByte == 'C') {
int* one;
one = getColour();

//1 2 3 not 0 1 2 due to the dud value

lcd.setCursor(0, 1);
lcd.setCursor(5, 1);
lcd.setCursor(10, 1);




revilo31 (author)2015-03-22

what if we use codeblocks rather than arduino for the arduino program?is the processing code that was used here still works?

Akin Yildiz (author)2014-10-25

hello sir, can you tell me if this item is a common cathode or anode please. thank you !!

Common anode. There's a "+" sign on the third photo on the product page

yes i figured it out as well, and actually got my order too. check it out; thank you

abrar.burk (author)2014-10-16

hi. the sketch isnt working for me for some reason. :( . im a noob, but i think ive followed everything. my circuit is definitely connected right. but nothing seems to be working.

could it be a problem with this? -

//the [0] may be [another number] on your computer

myPort = new Serial(this, Serial.list()[0], 9600);

when you say 'another number' what does that mean? how do i figure out the another number on my computer?


abrar.burk (author)abrar.burk2014-10-16

ok! problem solved! i just changed the [0] to [2]. worked for me!

thanks for the sketch!

theorio (author)2013-06-28

Can I use a Bluetooth Arduino with this? (I'm new to this)

Raphango (author)theorio2013-10-10

Sure... =)
Just have to add some changes to the code (kind more complez) but yes, its possible!

Raphango (author)2013-10-10

Great ible! Got myself one fully running!

P.S.: SInce you are using pins 9~11 as PWM (Analog Out), you don't have to declare "pinMode(redPin, OUTPUT);" understand? It's not necessary. Gotta save you a couple KBs... ;D

bodet328 (author)2013-04-05

I know I'm really late to this, but why is the red part of the colour map blocked off?

smessud (author)2010-02-11

A useful ible, but I'm worried that you don't use any resistor.

If you apply the maximum power to the LEDs, with 5V output, I'm afraid they won't live long.

You'll have to determine the proper resistor for each colour, as each LED type has it's own workiing voltage.

pcooper2 (author)smessud2012-02-21

LEDs work on current, not voltage. Each type of LED has a forward current "knee" that corresponds to a narrow range of forward voltages. The supply voltage needs to be higher than the knee voltage, but once the knee voltage is exceeded, the forward current rises extremely rapidly in the absence of a current limiting resistor.

Running an LED without a current limiting resistor only works if the die has a built-in constant current source. Some blinking LEDs are built that way, although the three-color LED apparently is not.

smitec08 (author)smessud2010-02-11

 yes this is true. personally mine is one for about a minuet at a time so its pretty safe. i'll add the resistor values now, thanks for reading.

amir_t_h2 (author)2012-02-10

I have problem to run the file. I appreciate if anyone help me to resolve the problem. when i run the first code which map the picture, the error " import does not name a type" appear. it is my first experience with Arduino. I believe something wrong with my library. how can i resolve this problem.
Thank you,

dundee5 (author)2011-12-26

Version without use of Processing (constantly changes color):

supralova11 (author)2011-10-23

Thank you so much :)

cmanrique (author)2011-08-14

dude im having problem with the color map  ,  please help me with that  and dude this is a n awesome project

Zedone Two (author)2011-07-15

Could you do an instructable with both an xBee and RGB LEDs? I'm interested to see it work, especially controlling the LEDs separately and wirelessly at the same time :D

ZdProjects (author)2011-06-24

Finally got it to work! My computer says my Arduino is in COM7 but I needed to put in "[3]" for the myPort!.

32teeth (author)2011-06-07

works well if you change your serial ports
arduino's serial port will be whatever your COM port for uploading the file will be
on processings side the index will changed based on your OS and how many things you have plugged in

on OSX you will get many com ports (usually between 5 and 8 depending on whats plugged in) look for the first one that has TTY in it's property

on PC they will show up as COM1, COM4...

good starting project on communications between software and hardware.

jelmareanvlad (author)2011-04-28

it doesnt work.....I cant find the image for processing and I keep getting a "Null pointer exception" what is that ? I think your code is bad or something...please respond....

pdunandf (author)2011-04-17

great example thanks - having problems with getting the right colour from processing to arduino but it is a matter of patience, I guess

clawlor88 (author)2010-11-14

I've had no problem with calibrating or uploading the image. However, i'm getting this error when I try to run the file:

"error: 'import' does not name a type In function 'void setup()':
In function 'void draw()':
In function 'void mousePressed()':"

Anyone know this means? Thanks for any help!

scottyb4996 (author)2010-09-23

I am confused. How do I get Processing to open up the picture? Basically where do I put the image and the code at?

whiskey_14 (author)2010-09-12

this is pretty sweet, thanks for sharing!

PoisonMondo (author)2010-04-05

what do you mean by processing

Processing is a multi-platform programming IDE based on Java. It's at It's pretty simple to learn and has great support via their forums and the web in general. Terrible name though since it's such a generic word!

CCOTTER247 (author)2010-07-11

The software wont work!! Help! It downlaids a a .tmp file and processing and arduino doesnt recognize it!

Arduino Guy (author)CCOTTER2472010-07-23

left click the file and press rename, then select tmp and enter pde when the open menu asks " if the file extension is modified the file might become unusable..," press yes. do this with both files and i am pretty shure the one ending in 2K is arduino c code, the other is for prossessing.

CCOTTER247 (author)Arduino Guy2010-07-23

Thank you! I how have it working!!! How do I calibrate the color?

Arduino Guy (author)CCOTTER2472010-07-29

You need to make a another background file and import it into processing, or if your asking to change the LED's colour, just run the program and click on the colour you want to change the led into.

CCOTTER247 (author)Arduino Guy2010-07-29

I apologize for not being very clear on my last question! I was curious about calibrating the color of the led. When I click a color on the screen it changes the led to a different color than I clicked on. Any suggestions?

Arduino Guy (author)CCOTTER2472010-07-30

try turning on any single colour(red, green, or blue), then disconect all and put each pin of led into the arduino pins, once you get a matching colour leave it connected, do the same procedure for the next colour.

smitec08 (author)CCOTTER2472010-07-30

check the pure RGB values (red blue green) if they seem correct then you know the pins are correct. if they seem a bit off check if you have a common anode of cathode LED. that will change the code if its common anode attach the common anode to VCC and wherever there is an analog out replace its value with 255- that value eg analogOut(X) to analogOut(255-X) if that still doesnt fix it check you resistor values with an online calc to ensure they're in the right range as it may be letting to much power through to the LEDs. Hope that helps. Smitec :D

vIDo (author)2010-04-13

first I wanted to thank you a lot for the tutorial.. it was a lot of help..

second I played a little with your code and the code found here to add some twitter to the mix.. now I can use twitter to change the color of my lamp!

just tweet: @colormelamp <color of you choice>
supported colors: red, green, blue, cyan, yellow, magenta & white.
(actually it doesn't matter what's in the message as long as it contains "@colormelamp" and a color)

my code is pretty cruel but it works. the code on the arduino is the same, you'll just have to set the usb port.. the rest should work.. if your gonna try it you could use the same twitter account I used that way our lamps would have the same color!

processing code here
(you can leave the password and username as they are, there not needed)

tell me what you think!

ps. this was my first Processing and third Arduino project so sorry if it doesn't make sense.. :P

PoisonMondo (author)2010-04-05

where s the picture file and your code didn't work

smitec08 (author)PoisonMondo2010-04-05

 myPort is the first com port the one the arduino is attached to yours may not be the same.

go to it is a program named processing

place the image in a folder named 'data' in the same directory as your processing file.

PoisonMondo (author)2010-04-05

for the serial part
 myPort = new Serial(this, Serial.list()[0], 9600);
is that your com port

tangeeble (author)2010-03-29

 Is this tutorial possible with a single anode rbg led?

smitec08 (author)tangeeble2010-03-31

yes but:
   -anode should be connected to a voltage supply not ground
   -and, your analog outs in the arduino code should be 255-(what they are now)

xVirus (author)2010-03-02

what kind of a program, that is beside the (arduino). With the big P on it

About This Instructable




Bio: Undergraduate Electrical Engineer and Electronics/Programming enthusiast.
More by smitec08:Controlling an RGB Led with Arduino and Processing
Add instructable to: