Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Controlling an RGB LED Diode with Arduino

Contents

Introduction

There comes times where we want to use the RGB LED Diode, the RGB Diode is great for colour mixing and creating a rainbow of colours. But sometimes, using the 4 legged RGB LED lights can be a little confusing. So, let’s run through some basics and get you up and running in no time!.

How Does An RGB LED Diode Work?

The RGB LED diode consists of 3 small led light modules inside it, which are Red, Green and Blue colour.
There are 2 types of RGB LED lights.

  • Common Anode
  • Common Cathode

Common Anode is the most commonly used. So, you can assume that the one you have is more than likely a common Anode type.

The colour produced by an RGB LED light is in fact the combination of those 3 LED lights.
As the LEDs are very close to each other, we can only see the final colours result rather than the three colours individually.

Pinout

Circuit Diagram

The Code

				
					int r = 7;
int g = 6;
int b = 5;
void setup() {
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
}
void loop() {
  for(int x=0; x<= 220; x++){
    analogWrite(r, x);
    delay(10);
  }
  for(int x=0; x<= 220; x++){
    analogWrite(g, x);
    delay(10);
    }
  for(int x=0; x<= 220; x++){
    analogWrite(b, x);
    delay(10);
  }
  for(int x=220; x >= 0; x--){
    analogWrite(r, x);
    delay(10);
  }
  for(int x=220; x >= 0; x--){
    analogWrite(b, x);
    delay(10);
  }
  for(int x=220; x >= 0; x--){
    analogWrite(g, x);
    delay(10);
  }
}
				
			

Code Explanation

In this code, we have written a few loops to vary the internal Red, Green and Blue lights individually to see the colour variations.

Through using the analogWrite() function we are generating PWM. The value of the function can be any integer number between 0 to 255, where 0 means always off and 255 means always on. Anything between them adjusts the intensity of the light. So first we start by slowly adjusting the intensity of the red light from 0 to 255, then we increase the intensity of the green light and then the blue. We give a little delay so the colour changes can catch our eyes.

Then we decrease the intensities of the lights one by one through 3 loops.

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top