Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

how to Control an RGB LED by PWM

how to  Control an RGB LED by PWM

Experimental 9

In this lesson, we will program the Arduino UNO for  RGB LED control, and make RGB LED emits a various of colors of light. 

Hardware Required

 1 * Arduino UNO

1 * USB Cable

1 * RGB LED

3* 220Ω Resistor

1 * Breadboard

Several jumper wires 

Principle of operation

RGB LEDs consist of three LEDs: red, green and blue. These three colored LEDs are capable of producing any color. Tri-color LEDs with red, green, and blue emitters, in general using a four-wire connection with one common lead (anode or cathode).

What we use in this experiment is a common anode RGB LED. The longest pin is the common anode of the three LEDs.The pin is connected to the+5Vpin of the Arduino, and the rest pins are connected to pin D9, D10, and D11 of the Arduino with a current limiting resistor between.

Experiment Code

//File name:   Experiment 9

//Description: make RGB LED emits a various of colors of light.

//Website: www.okuelectronics.com

//E-mail: support@okuelectronics.com

//Author: Okudzeto osei prince

//Date: 2019/08/08 

int redPin = 6;   // R petal on RGB LED module connected to digital pin 6 
int greenPin = 4; // G petal on RGB LED module connected to digital pin 4 
int bluePin = 3;   // B petal on RGB LED module connected to digital pin 3
void setup()    
{   
   pinMode(redPin, OUTPUT);   // sets the redPin to be an output 
   pinMode(greenPin, OUTPUT); // sets the greenPin to be an output 
   pinMode(bluePin, OUTPUT);  // sets the bluePin to be an output 
}    
void loop()  // run over and over again  
{    
    // Basic colors:  
    color(255, 0, 0); // turn the RGB LED red  
    delay(1000);      // delay for 1 second  
    color(0,255, 0);  // turn the RGB LED green  
    delay(1000);      // delay for 1 second  
    color(0, 0, 255); // turn the RGB LED blue
    delay(1000);      // delay for 1 second  
  
    // Example blended colors:  
    color(255,255,0);   // turn the RGB LED yellow   
    delay(1000);        // delay for 1 second  
    color(255,255,255); // turn the RGB LED white  
    delay(1000);        // delay for 1 second  
    color(128,0,255);   // turn the RGB LED purple  
     delay(1000);       // delay for 1 second  
     color(0,0,0);      // turn the RGB LED off  
     delay(1000);       // delay for 1 second  
}     
     
void color (unsigned char red, unsigned char green, unsigned char blue)// the color generating function  
{    
     analogWrite(redPin, 255-red);     // PWM signal output   
     analogWrite(greenPin, 255-green); // PWM signal output
     analogWrite(bluePin, 255-blue);   // PWM signal output
}     

Compile the program and upload to Arduino UNO board Now,you can see the RGB LED flash red, green,blue,yellow,white and purple light, and then go out. Each state lasts for 1s each time, and the LED flashes colors repeatedly in such sequence.


Add Comment