An RGB LED is a LED which has three LED's integrated in one packaging. These LED's have the colors Red, Green, and Blue. The RGB LED used here is a hyperflux LED with common cathode, which can draw 20mA current. Such a LED costs about 2€. With these three colours you can mix to any colour.

In this design each colors can be controlled seperately. The technique to control the intensity of each LED is pulse width modulation (PWM). When using 8-bit PWM a number of 256 * 256 * 256 combinations can be made, thus the LED can show 16.777 million colours, and 256 different brightnesses. When all the three LED's are at full brightness the color will be white. When they are all off the color will be 'black' in the dark ;). The LED used here has a common cathode and is an Ultra Brightness type. The intensity of each LED can be varied from minimum to maximum in 256 continuous levels by using the potentiometers. Different intensities of red, green, and blue LEDs produce different colors.

RGB LED color mixer pinsRGB LED color mixer


 Schematic and Build

Below shows the schematic diagram.of the RGB LED color mixer.The RGB LED used here has four leads, one for each color an one for the common cathode(ground). The Red LED operates on a voltage of 2 V, Green needs 3.5 V, and the Blue LED needs also 3.5 V. Each LED draws 20mA current, so the maximum current consumption is 60mA. The supply for the circuit is 5V DC. The µ-controller reads the value of the potmeters by using the three channels of the ATMega328's analog-to-digital convertor. The corresponding value is reflected to the thrtee LED's by using a eight bit PWM. Each ADV channnel converts the analog voltage output of the potentiometer into a digital value within the range of 0 to 255 which can be converted directly to the intensity level of the coresponding LED.

rgb led color mixer sch

The circuit is simple.The RGB LED is hooked up to the PWM outputs on PORTB1, PORTB2 and PORTB3 of the ATMega328. The potentiometers are connected to the ACD channel inputs PC0- PC2.There is also a resistor between the LED's and the µ-controller to limit the current to 20mA. The circuit with the RGB LED with the current limiting resistors and the potentiometers is build on a breadboard and connected to the ATMega328 board with jumper wires.

You can buy the ATMega328 board at www.bizztronix.eu

 rgb led color mixer pic1

 


Software

The code for the RGB LED color mixer is written in C with the WINAVR C-Compiler The hardware PWM timers of the ATMega328 are used to let the LED's light. To use the PWM timers you first need to initialize the timers at the begin of the program code. The value of the potmeters is read by the adc converter and assigned to the corresponding PWM that will light up the LED's. You can copy and paste the textfile into the WINAVR C-compiler. Below you find the C code.

 

// program name: RGB LED Color Mixer
// date: 12-07-2016
// target device: ATMeg328
// purpose: mix RGB LED to 8 colors with 3 potmeters, uses ADC channels input.
// author: www.avrprojects.net
//

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

// this function read the value of the selected ADC channel
unsigned char read_adc(unsigned char channel)
{
ADMUX = 0b00100000; // set ADC0
ADMUX |= channel; // set the adc channel
ADCSRA = 0b10000111; //set ADEN, precale by 128
ADCSRA |= 1<<ADSC; //start conversion;
while (ADCSRA&(1<<ADSC)); //wait conversion end
return ADCH;
}

// main program
int main (void)
{
//PWM1&2 initialisation
TCCR1A = 0b10100001;// set PWM mode for PB1 and PB2
TCCR1B = 0b00000010; //set clock:8 prescaler

//PWM3 initialisation
TCCR2A = 0b10000001; // set PWM mode for PB3
TCCR2B = 0b00000010; //set clock:8 prescaler

DDRB = 0xFF; //set portB as output

while(1) //infinite loop
{
OCR1A = read_adc(0); // reads the adc channel and assigns it to the corresponding PWM
OCR1B = read_adc(1);
OCR2A = read_adc(2);
_delay_ms(50);
}

}


Video