top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

Connect and use an RGB LED with an Arduino



OVERVIEW


RGB LEDs are a fun and easy way to add some colour to your projects.


Since they are like 3 regular LED in one, the way to use and connect them is not much different.


They come mostly in 2 versions:  Common Anode or Common Cathode.


Common Anode uses 5V on the common pin, while Common Cathode connects to Ground.


As with any LED, we need to connect some resistors inline (3 total) so we limit the current being drawn.


In our sketch we will start with the LED in the Red colour state, then fade to Green, then fade to Blue and finally back to the Red colour.


By doing this we will cycle through most of the colour that can be achieved.


 

PARTS USED

RGB LED Common Anode


Arduino UNO


Dupont Wires


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




The RGB LED we are using is a Common Anode, so it requires 5V to the common pin.

* If we were using a Common Cathode, we would connect it to Ground(GND) instead.


We are using 3 resistor to limit the current drawn for the Red, Green, Blue pins.


Each of them are 330 Ohms.


The connections are:  Pin3-Pin1(red), 5V-Pin2(Anode), Pin5-Pin3(blue), Pin6-Pin4(green).


 

THE CODE


Our code will use FOR loops to cycle through the colors.


The First FOR loop will go from RED to GREEN


The Second FOR will go from GREEN to BLUE


and finally the last FOR will go from BLUE to RED.

As always you can watch our Tutorial video on this page to get more information about the code used.


// Define Pins
#define RED 3
#define GREEN 5
#define BLUE 6

#define delayTime 50 // fading time between colors


void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}

// define variables
int redValue;
int greenValue;
int blueValue;


// main loop
void loop()
{
  redValue = 255; // choose a value between 1 and 255 to change the color.
  greenValue = 0;
  blueValue = 0;

  analogWrite(RED, 0);
  delay(5000);

   for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
  {
    redValue -= 1;
    greenValue += 1;
    analogWrite(RED, 255 - redValue);
    analogWrite(GREEN, 255 - greenValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 255;
  blueValue = 0;

  for(int i = 0; i < 255; i += 1)  // fades out green bring blue full when i=255
  {
    greenValue -= 1;
    blueValue += 1;
    analogWrite(GREEN, 255 - greenValue);
    analogWrite(BLUE, 255 - blueValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 0;
  blueValue = 255;


  for(int i = 0; i < 255; i += 1)  // fades out blue bring red full when i=255
  {
  redValue += 1;
  blueValue -= 1;
  analogWrite(RED, 255 - redValue);
  analogWrite(BLUE, 255 - blueValue);
  delay(delayTime);
  }
}


 

TUTORIAL VIDEO




 

DOWNLOAD


Copy and Paste the above Sketch/Code in your Arduino IDE.

497 views0 comments

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page