Controlling an RGB Led With Arduino and Processing

126,037

101

50

Introduction: Controlling an RGB Led With Arduino and Processing

About: Undergraduate Electrical Engineer and Electronics/Programming enthusiast.

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

Hardware:
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 led.linear1.org/led.wiz

Software:
Arduino
Processing

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

Step 2: 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

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

 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.

2 People Made This Project!

Recommendations

  • Audio Contest 2018

    Audio Contest 2018
  • Tiny Home Contest

    Tiny Home Contest
  • Furniture Contest 2018

    Furniture Contest 2018

50 Discussions

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

1 reply

They are text files. Just change the names.

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

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 

image(img,0,0);

Anyone had this problem, before? Thank you!

2 replies

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

thanks

0
None
zebu14

2 years ago

Hello,

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 :

https://123d.circuits.io/circuits/1213274-the-unna...

The code of the main loop :

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

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

//1 2 3 not 0 1 2 due to the dud value
outputColour(one[1],one[2],one[3]);

lcd.setCursor(0, 1);
lcd.print(one[1]);
lcd.setCursor(5, 1);
lcd.print(one[2]);
lcd.setCursor(10, 1);
lcd.print(one[3]);

}
}

delay(wait);
}

Thanks

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

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?

thanks

1 reply

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

thanks for the sketch!

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

Great ible! Got myself one fully running!
Congratulations!

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

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

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.


2 replies

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.

 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.