Fig. 1: Prototype of Arduino based RGB LED Controller
RGB-LED is widely used to generate different colour Lights in Interior Decoration, Electronic Appliances, Automobiles, etc.
This article deals with Arduino to get various colours with RGB LED and show the transition from one colour to another.
The PWM output of an Arduino, is an important tool to get the variety of colours, be it Pink, Cyan, Magenta, Orange, Purple, etc.
An RGB LED is a four terminal LED with one common pin (say cathode) and three other pins (anode) for Red, Green and Blue LEDs.
Fig. 2: Pin Diagram and Internal Circuit of RGB LED
The first step, getting and RGB LED and testing it. For this we need to connect 5V supply to bare LED through Resistors (330ohms).
Now that we have got RED, GREEN and BLUE colours individually, its time to get a mix of these three basic colours.
Get an Arduino (any basic board with at least 3 PWM outputs). I used an Atmega8 one.
Connect your Arduino, do proper connections, as shown in below circuit diagram.
Proceeding further, it’s now time to get the control over the LED. We’ll do this through Arduino-IDE.
Go to Arduino-IDE, select your board type, under the ‘Tools’ menu. Select COM port, under ‘Serial Port’ menu.
After this copy the below sketch and Upload it.
Working:
Now, let us understand how this works. The ‘analogWrite()’ function of Arduino makes this possible.
Each digital output pin of Arduino can provide a voltage of 0 or 5V, but the PWM pin has an add-on feature. It can give various voltage values between 0V-5V.
Say we write,
analogWrite(red_led,50); //red_led refers to pin-9 (PWM pin)
This will provide a voltage = (5/255)*50 V = 0.98V on pin-9.
If we write:
analogWrite(blue_led,100); //blue_led refers to pin-10 (PWM pin)
This will provide a voltage = (5/255)*100 V = 1.96V on pin-10
In this experiment, what we do is create a for-loop to provide values between 0-255 at each LED pin with some delay (in milliseconds).
for(int i=0;i<=255;i+=3)
{
analogWrite(redPin,255-i); //writes (255-i) on redPin
analogWrite(greenPin, i); // writes I on greenPin
delay(100); //wait for 100uS
}
Fig. 3: Image showing RGB LED being controlled by Arduino
You may also like:
Project Source Code
### int redPin = 10; int greenPin = 11; int bluePin = 9; void colourTransition(); void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { colourTransition(); } void colourTransition() { for(int i=0;i<=255;i+=3) { analogWrite(redPin,255-i); analogWrite(greenPin, i); delay(100); } for(int i=0;i<=255;i+=3) { analogWrite(greenPin,255-i); analogWrite(bluePin, i); delay(100); } for(int i=0;i<=255;i+=3) { analogWrite(bluePin,255-i); analogWrite(redPin, i); delay(100); } } ###
Circuit Diagrams
Filed Under: Arduino Projects, Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.