Not Your Typical RGB LED

Not Your Typical RGB LED © GPL3+

An easy way to add color cues to your project, giving you the versatility to light multiple RGB LEDs simulatenously with little code.

  • 2,194 views
  • 1 comment
  • 9 respects

Components and supplies

Apps and online services

About this project

NOTE: This IS NOT for those using a shield or addressable LEDs. This is only for those who need to light RGB LEDs connected directly to the Arduino. If that's you, then keep reading.

Typically, you'd need to set up each LED individually and then when it comes to changing colors you'd need to analogWrite each pin for each LED. With all those numbers floating around it can get confusing even with comments to help.

So I found a slightly better way. In the attached file, all you'll need to do is Select the LED you want to turn on and Choose the Color. Cutting it off is the same way. Choose what LED you wish to turn off and tell it you want it off.

That looks something like this...

ColorExecute(1, Orange);

delay(1000);

ColorExecute(1, Off);

To change Multiple LEDs "simultaneously" after 2 seconds, it looks like this....

ColorExecute(1, Orange);

ColorExecute(2, Purple);

ColorExecute(3, Cyan);

delay(2000);

ColorExecute(1, Blue);

ColorExecute(2, Blue);

ColorExecute(3, Blue);

Short and simple, right!? This program works with up to 3 LEDs and it's set to work with an Arduino Uno. I've added a few extra lines so you can incorporate more LEDs easily. Simply change the Pin Numbers for any of the LEDs, and Add more LEDs by uncommenting/copying a few more lines of code.

The Current Color Types:

  • Off
  • Red
  • Green
  • Blue
  • Yellow
  • White
  • Purple
  • Cyan
  • Orange
  • Test (Allows you to manually set a LED color)

Anyway, I'm sure this can be turned into a library, somehow. Let me know if you have any questions.

Results tested on a SMD RGB LED on Arduino UNO.

Applications: This is great for projects where you have multiple LEDs that need to correspond to sensor data or program modes. Or projects that use multiple LEDs that need different colors in different parts on prop suit or weapon.

Code

Easy RGB LedArduino
Details on how to integrate are noted in the program comments.
//Define the Pins for Each Color of the LED
#define LED1RED    11
#define LED1GREEN  10
#define LED1BLUE   9

#define LED2RED    8
#define LED2GREEN  7
#define LED2BLUE   6

#define LED3RED    5
#define LED3GREEN  4
#define LED3BLUE   3

//#define LED4RED   0
//#define LED4GREEN 0
//#define LED4BLUE  0



//You may fine tune these colors as you see fit.
//However DO NOT Rename them.
#define Off 0
byte OFF[3] = {0, 0, 0};
#define Red 1
byte RED[3] = {255, 0, 0};
#define Green 2
byte GREEN[3] = {0, 255, 0};
#define Blue 3
byte BLUE[3] = {0, 0, 255};
#define Yellow 4
byte YELLOW[3] = {255, 50, 0};
#define White 5
byte WHITE[3] = {255, 75, 50};
#define Purple 6
byte PURPLE[3] = {255, 0, 50};
#define Cyan 7
byte CYAN[3] = {0, 255, 150};
#define Orange 8
byte ORANGE[3] = {255, 10, 0};



//Add Other Colors Here
//Be sure to Give it a Unique Color Number (ex. 9)
//#define ColorName 9
//byte COLORNAME[3] = {ColorValue1, ColorValue2, ColorValue3};

//CHANGE TEST TO TRY OUT A NEW COLOR BEFORE SETTLING
#define Test 20
byte TEST[3] = {255, 75, 50};



void setup() {
  pinMode(LED1RED, OUTPUT);
  pinMode(LED1GREEN, OUTPUT);
  pinMode(LED1BLUE, OUTPUT);
  pinMode(LED2RED, OUTPUT);
  pinMode(LED2GREEN, OUTPUT);
  pinMode(LED2BLUE, OUTPUT);
  pinMode(LED3RED, OUTPUT);
  pinMode(LED3GREEN, OUTPUT);
  pinMode(LED3BLUE, OUTPUT);
  //pinMode(LED4RED, OUTPUT);
  //pinMode(LED4GREEN, OUTPUT);
  //pinMode(LED4BLUE, OUTPUT);

  /***************COLOR PROPERTIES***********/
  /*
     Color Types:
     Off
     Red
     Green
     Blue
     Yellow
     White
     Purple
     Cyan
     Orange
     Test

  */

  //Example Code
  //ColorExecute(LED#, COLOR);
  ColorExecute(1, Green);
  ColorExecute(2, Purple);
  ColorExecute(3, Cyan);
  delay(500);
  ColorExecute(1, Blue);
  ColorExecute(2, Blue);
  ColorExecute(3, Blue);
  delay(500);
  ColorExecute(1, Off);
  ColorExecute(2, Off);
  ColorExecute(3, Off);
  delay(2000);

  ColorExecute(1, Test);
  delay(2000);
  ColorExecute(1, Off);
}



//You can put the ColorExecute function anywhere in your code.
//Ideally somewhere near the bottom as it gets pretty long...

int ColorExecute (byte LEDSelect, byte ColorSelect) {
  //**************************LED 1********************//
  if (LEDSelect == 1) {
    if (ColorSelect == Off) {
      analogWrite(LED1RED, OFF[0]); analogWrite(LED1GREEN, OFF[1]); analogWrite(LED1BLUE, OFF[2]);
    }
    if (ColorSelect == Red) {
      analogWrite(LED1RED, RED[0]); analogWrite(LED1GREEN, RED[1]); analogWrite(LED1BLUE, RED[2]);
    }
    if (ColorSelect == Green) {
      analogWrite(LED1RED, GREEN[0]); analogWrite(LED1GREEN, GREEN[1]); analogWrite(LED1BLUE, GREEN[2]);
    }
    if (ColorSelect == Blue) {
      analogWrite(LED1RED, BLUE[0]); analogWrite(LED1GREEN, BLUE[1]); analogWrite(LED1BLUE, BLUE[2]);
    }
    if (ColorSelect == Yellow) {
      analogWrite(LED1RED, YELLOW[0]); analogWrite(LED1GREEN, YELLOW[1]); analogWrite(LED1BLUE, YELLOW[2]);
    }
    if (ColorSelect == White) {
      analogWrite(LED1RED, WHITE[0]); analogWrite(LED1GREEN, WHITE[1]); analogWrite(LED1BLUE, WHITE[2]);
    }
    if (ColorSelect == Purple) {
      analogWrite(LED1RED, PURPLE[0]); analogWrite(LED1GREEN, PURPLE[1]); analogWrite(LED1BLUE, PURPLE[2]);
    }
    if (ColorSelect == Cyan) {
      analogWrite(LED1RED, CYAN[0]); analogWrite(LED1GREEN, CYAN[1]); analogWrite(LED1BLUE, CYAN[2]);
    }
    if (ColorSelect == Orange) {
      analogWrite(LED1RED, ORANGE[0]); analogWrite(LED1GREEN, ORANGE[1]); analogWrite(LED1BLUE, ORANGE[2]);
    }
    if (ColorSelect == Test) {
      analogWrite(LED1RED, TEST[0]); analogWrite(LED1GREEN, TEST[1]); analogWrite(LED1BLUE, TEST[2]);
    }
  }

  //**************************LED 2********************//
  if (LEDSelect == 2) {
    if (ColorSelect == Off) {
      analogWrite(LED2RED, OFF[0]); analogWrite(LED2GREEN, OFF[1]); analogWrite(LED2BLUE, OFF[2]);
    }
    if (ColorSelect == Red) {
      analogWrite(LED2RED, RED[0]); analogWrite(LED2GREEN, RED[1]); analogWrite(LED2BLUE, RED[2]);
    }
    if (ColorSelect == Green) {
      analogWrite(LED2RED, GREEN[0]); analogWrite(LED2GREEN, GREEN[1]); analogWrite(LED2BLUE, GREEN[2]);
    }
    if (ColorSelect == Blue) {
      analogWrite(LED2RED, BLUE[0]); analogWrite(LED2GREEN, BLUE[1]); analogWrite(LED2BLUE, BLUE[2]);
    }

    if (ColorSelect == Yellow) {
      analogWrite(LED2RED, YELLOW[0]); analogWrite(LED2GREEN, YELLOW[1]); analogWrite(LED2BLUE, YELLOW[2]);
    }

    if (ColorSelect == White) {
      analogWrite(LED2RED, WHITE[0]); analogWrite(LED2GREEN, WHITE[1]); analogWrite(LED2BLUE, WHITE[2]);
    }

    if (ColorSelect == Purple) {
      analogWrite(LED2RED, PURPLE[0]); analogWrite(LED2GREEN, PURPLE[1]); analogWrite(LED2BLUE, PURPLE[2]);
    }
    if (ColorSelect == Cyan) {
      analogWrite(LED2RED, CYAN[0]); analogWrite(LED2GREEN, CYAN[1]); analogWrite(LED2BLUE, CYAN[2]);
    }
    if (ColorSelect == Orange) {
      analogWrite(LED2RED, ORANGE[0]); analogWrite(LED2GREEN, ORANGE[1]); analogWrite(LED2BLUE, ORANGE[2]);
    }

    if (ColorSelect == Test) {
      analogWrite(LED2RED, TEST[0]); analogWrite(LED2GREEN, TEST[1]); analogWrite(LED2BLUE, TEST[2]);
    }
  }

  //**************************LED 3********************//
  if (LEDSelect == 3) {
    if (ColorSelect == Off) {
      analogWrite(LED3RED, OFF[0]); analogWrite(LED3GREEN, OFF[1]); analogWrite(LED3BLUE, OFF[2]);
    }
    if (ColorSelect == Red) {
      analogWrite(LED3RED, RED[0]); analogWrite(LED3GREEN, RED[1]); analogWrite(LED3BLUE, RED[2]);
    }
    if (ColorSelect == Green) {
      analogWrite(LED3RED, GREEN[0]); analogWrite(LED3GREEN, GREEN[1]); analogWrite(LED3BLUE, GREEN[2]);
    }
    if (ColorSelect == Blue) {
      analogWrite(LED3RED, BLUE[0]); analogWrite(LED3GREEN, BLUE[1]); analogWrite(LED3BLUE, BLUE[2]);
    }
    if (ColorSelect == Yellow) {
      analogWrite(LED3RED, YELLOW[0]); analogWrite(LED3GREEN, YELLOW[1]); analogWrite(LED3BLUE, YELLOW[2]);
    }
    if (ColorSelect == White) {
      analogWrite(LED3RED, WHITE[0]); analogWrite(LED3GREEN, WHITE[1]); analogWrite(LED3BLUE, WHITE[2]);
    }
    if (ColorSelect == Purple) {
      analogWrite(LED3RED, PURPLE[0]); analogWrite(LED3GREEN, PURPLE[1]); analogWrite(LED3BLUE, PURPLE[2]);
    }
    if (ColorSelect == Cyan) {
      analogWrite(LED3RED, CYAN[0]); analogWrite(LED3GREEN, CYAN[1]); analogWrite(LED3BLUE, CYAN[2]);
    }
    if (ColorSelect == Orange) {
      analogWrite(LED3RED, ORANGE[0]); analogWrite(LED3GREEN, ORANGE[1]); analogWrite(LED3BLUE, ORANGE[2]);
    }
    if (ColorSelect == Test) {
      analogWrite(LED3RED, TEST[0]); analogWrite(LED3GREEN, TEST[1]); analogWrite(LED3BLUE, TEST[2]);
    }
  }

  /*
     //************LED 4 **********
     if (LEDSelect == 4) {
         if (ColorSelect == ColorName) {
             analogWrite(LED4RED, COLORNAME[0]); analogWrite(LED4GREEN, COLORNAME[1]); analogWrite(LED4BLUE, COLORNAME[2]);
         }
     }
  */
}

void loop() {}

Schematics

Easy RGB Led Pin Layout
Easy rgb led pin layout euer9hklnp

Comments

Similar projects you might like

LED Emergency Lights using WS2812 RGB LED Module

Project tutorial by bigboystoys13

  • 10,022 views
  • 5 comments
  • 20 respects

Smooth RGB LED Transitions with Johnny-Five

by Iain

  • 8,106 views
  • 3 comments
  • 27 respects

Music Adaptive RGB LED with Arduino and Processing

Project in progress by ounane mohamed elfarouk

  • 2,954 views
  • 8 comments
  • 24 respects

Kids RGB LED Star Nightlight

Project tutorial by Unexpected Maker

  • 2,467 views
  • 1 comment
  • 14 respects

DMX RGB LED Outdoor

Project tutorial by Sebastian Wiessner

  • 9,638 views
  • 4 comments
  • 15 respects
Add projectSign up / Login