RGB LED

Overview

  • Operating Voltage:
    RGB LED (common cathode)
    RGB LED (common cathode)
    • RED: 2.5V
    • GREEN: 3.5V
    • BLUE: 3.5V
  • 4 pins: GND, R, G and B

Features

  • 3 in 1 colour LED
  • It is a common cathode RGB LED

Description

In electronics any colour has three basic components, some amount of red, green and blue in it. Variations in their amount help us to create various colours, and truly speaking they are infinite in number. A RGB led is a single LED package which provides us all three kinds of colors. They come in two forms: common cathode (one which we’ll be talking about in this project) and common anode. Common cathode has a common GND pin for all three colours while VCC pin is common in common anode one. Here’s a look at it:

rgb-2

This is the pin-out of a common cathode RGB led.

RGB LED with Atmega 16 microcontroller:

In this project I have used this type of LED to generate various forms of colours. Now to do the same we need to send different proportions of these colours and this can be done with the help of PWM. I have used timers (0, 1 and 2) for this stuff. Timer 0 and 2 are of 8 bit resolution (0-255) while timer 1 is a 16 bit (0-65535) one and since I am operating these timers on 20 ms cycle/ 50Hz frequency, the values for timers get reduced to 0-157 for timer0 and timer2 while its 0-20,000 for timer1. You’ll understand the conversion part when you take a look at my coding part.

Also to make this project much more convenient, I have used potentiometers to adjust the amount of R, G and B values. So ADC section comes to play here. ADC0, 1 and 2 pins are being used in this stuff. ADC provides the values from 0-1023 and then they get converted to their respective ranges.

Connections:

  • R pin is connected to OC0 pin (timer0 O/P)
  • G pin is connected to OC2 pin (timer2 O/P)
  • B pin is connected to OC1A pin (timer 1A O/P)
  • 3 POTs are connected to ADC0, 1 and 2
  • 5V is given on Aref pin

Note:

  • Since timers are being used here, interrupts are really necessary part of this project, just take care to globally set the interrupts on SREG register. (i.e. SREG|=(1<<)7;)

Also these LEDs need to be operated carefully so use of appropriate resistor values is appreciated. Here I am using 820 Ohm.

Circuit Diagram

CD

Code


#include&lt;avr/io.h&gt;

#include&lt;util/delay.h&gt;

#include&lt;avr/interrupt.h&gt;

#include&lt;adc_lib.h&gt;

volatile uint16_t i_0=0,i_2=0,time_0,time_2,time_1A;

void main()

{              DDRB|=(1&lt;&lt;3);

DDRD|=(1&lt;&lt;7)|(1&lt;&lt;5)|(1&lt;&lt;4);

SREG|=(1&lt;&lt;7);

&nbsp;

TCCR0|=(1&lt;&lt;WGM01)|(1&lt;&lt;COM01)|(1&lt;&lt;CS02)|(1&lt;&lt;CS00);       //CTC mode, clear OC0 on compare match and 1024 pre-scaler on 8MHz

TCCR2|=(1&lt;&lt;WGM21)|(1&lt;&lt;COM21)|(1&lt;&lt;CS22)|(1&lt;&lt;CS20)|(1&lt;&lt;CS21);                //CTC mode, clear OC2 on compare match and 1024 pre-scaler on 8MHz

TCCR1A|=(1&lt;&lt;WGM11)|(1&lt;&lt;COM1A1)|(1&lt;&lt;COM1B1);

TCCR1B|=(1&lt;&lt;WGM13)|(1&lt;&lt;WGM12)|(1&lt;&lt;CS11);

&nbsp;

TCNT0=0;

TCNT2=0;

TCNT1=0;

&nbsp;

ICR1=20000;

&nbsp;

ADCinit();

&nbsp;

TIMSK|=(1&lt;&lt;OCIE0);                                      //interrupt on compare match

TIMSK|=(1&lt;&lt;OCIE2);                                      //interrupt on compare match

&nbsp;

while(1)

{

time_0=((read_adc(0)*15)/100);

time_2=((read_adc(1)*15)/100);

time_1A=((read_adc(2)*20);

&nbsp;

OCR0=time_0;                   //your calculated <strong>on</strong> time for R

OCR2=time_2;                   //your calculated <strong>on</strong> time for G

OCR1A=time_1A;             //your calculated <strong>on</strong> time for B

}

&nbsp;

}

ISR(TIMER0_COMP_vect)

{

if(i_0==0)

{              TCCR0|=(1&lt;&lt;COM00);                   //set OC0 on compare match

OCR0=157-time_0;                          //20ms-(your calculated on time)

i_0=1;

}

else

{              TCCR0&amp;=~(1&lt;&lt;COM00);                               //clear OC0 on compare match

OCR0=time_0;                                   //your calculated on time

i_0=0;

}

}

ISR(TIMER2_COMP_vect)

{

if(i_2==0)

{              TCCR2|=(1&lt;&lt;COM20);                   //set OC2 on compare match

OCR2=157-time_2;                          //20ms-(your calculated on time)

i_2=1;

}

else

{              TCCR2&amp;=~(1&lt;&lt;COM20);                               //clear OC2 on compare match

OCR2=time_2;                                   //your calculated on time

i_2=0;

}

}

Links

Video link:    https://youtu.be/qPVYqs6kmvo

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar