3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

RGB LED Tutorial (using an Arduino) (RGBL)

Step 4Digital Arduino Code

Digital Arduino Code
It is possible to control an RGB LED digitally. This requires 3 digital pins for each LED and allows for 7 colours to be displayed (Red, Green, Blue, Yellow, Cyan, Magenta, White).

But don't take our word for it download the code below and get playing. To do this simply...

  • Copy the code below and paste it into a an empty Arduino sketch.
  • Compile and upload the sketch to your Arduino board.
  • Enjoy as your three LEDs light up Red, Green, and Blue.
  • If you go to the loop() section of the code under example 1 you can see where you change the colors. Try a few other variations
  • To see how we control each LED look at the setColor() function.
  • Next comment out (add //) to the three lines of example 1.
  • Un-comment (delete //) from in front of every line in the Example 2 section of the loop() code
  • Upload this to your board and watch as your three LEDs change colour randomly.
  • Play around with the code to see how it works and make your own fun color functions.

(A quick video of Digital Example one and Digital Example 2)


Appendix 1: _RGBL_Digital.pde
//--- bof RGBL - RGB Digital Preamble//RGB LED pinsint ledDigitalOne[] = {14, 15, 16}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalTwo[] = {9, 10, 11}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinint ledDigitalThree[] = {3, 5, 6}; //the three digital pins of the first digital LED 14 = redPin, 15 = greenPin, 16 = bluePinconst boolean ON = LOW;                  //Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)const boolean OFF = HIGH;                //Define off as HIGH//Predefined Colorsconst boolean RED[] = {ON, OFF, OFF};    const boolean GREEN[] = {OFF, ON, OFF}; const boolean BLUE[] = {OFF, OFF, ON}; const boolean YELLOW[] = {ON, ON, OFF}; const boolean CYAN[] = {OFF, ON, ON}; const boolean MAGENTA[] = {ON, OFF, ON}; const boolean WHITE[] = {ON, ON, ON}; const boolean BLACK[] = {OFF, OFF, OFF}; //An Array that stores the predefined colors (allows us to later randomly display a color)const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};//--- eof RGBL - RGB Digital Preamblevoid setup(){  for(int i = 0; i < 3; i++){   pinMode(ledDigitalOne[i], OUTPUT);   //Set the three LED pins as outputs   pinMode(ledDigitalTwo[i], OUTPUT);   //Set the three LED pins as outputs   pinMode(ledDigitalThree[i], OUTPUT);   //Set the three LED pins as outputs     }}void loop(){/* Example - 1 Set a color   Set the three LEDs to any predefined color*/   setColor(ledDigitalOne, RED);    //Set the color of LED one   setColor(ledDigitalTwo, GREEN);  //Set the color of LED two   setColor(ledDigitalThree, BLUE); //Set the color of LED three/* Exampe - 2 Go through Random Colors  Set the LEDs to a random color*/  //int rand = random(0, sizeof(COLORS) / 2);   //get a random number within the range of colors  //  setColor(ledDigitalOne, COLORS[rand]);    //Set the color of led one to a random color      //rand = random(0, sizeof(COLORS) / 2);       //Set the color of LED 2 to a random color  //  setColor(ledDigitalTwo, COLORS[rand]);          //rand = random(0, sizeof(COLORS) / 2);       //Set the color of LED 3 to a random color  //  setColor(ledDigitalThree, COLORS[rand]);      //delay(1000);    }/* Sets an led to any color   led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)   color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)*/void setColor(int* led, boolean* color){ for(int i = 0; i < 3; i++){   digitalWrite(led[i], color[i]); }}/* A version of setColor that allows for using const boolean colors*/void setColor(int* led, const boolean* color){  boolean tempColor[] = {color[0], color[1], color[2]};  setColor(led, tempColor);}

« Previous StepDownload PDFView All StepsNext Step »
4 comments
Nov 11, 2009. 5:25 PMscooterboi says:
hey there , im planning on doing this on a larger scale , maybe 20-30 rgb led's for a project , id really like the user to be able to turn a dial  and as the dial is turned the colours change on all of the led's at once , is this possible? well , i know it is , but how do i i go about it , what sort of dial ? i really dont know, in the project i really dont have any space for a computer , hence the dial ,  thanks
Jun 9, 2011. 12:30 PMphenoptix says:
A little bit late in reply but just in case anyone else wanted do the same bigclive has a product that's perfect for your application! http://bigclive.com/knob.htm A neat kit and cheaper than using and Arduino
Sep 12, 2010. 1:57 PMJermsG says:
Hi scooterboi,
I'm sure this is possible, and something like an Arduino would be the way to go, at least to test the idea. However a basic Arduino (like the one I'm still learning to play around with) won't have enough sockets for that many individual LEDs in series - if I'm reading your comment right then maybe you could just have a single socket connected to multiple LEDs in parallel (well, four sockets, since the LEDs have four pins). You won't be able to control them individually but perhaps that won't matter to you.
The dial itself could be pretty much any random dial that physically fits - as far as I'm aware they all work as variable resistors, so the Arduino would just figure out how far the dial was turned. No problem.

The only other problem I can think of is power consumption - the Arduino may not put out enough power for that many LEDs, so you might need to plug in a separate power source, and make a power circuit to deal with that too.

Perhaps someone more qualified could be more help. :)

Good luck! Let us know when you've posted your own 'structable for this project!
Apr 24, 2010. 2:53 PMMatt LaVoie says:
Could you attach the .pde file of the saved code or a text file of it?  That'd be much easier to handle.

Thanks!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
408
Followers
14
Author:oomlout