Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Controlling RGB LED colour using Atmega16- (Part 19/46)

By Ganesh Selvaraj

 

This tutorial will give you a brief introduction to the concept of colors and how different colors can be produced using RGB LED. The color would be controlled using an ATMega16 microcontroller.

RGB LEDs are basically the combination of the 3 LEDs (Red, Green and Blue) fused into a single package. It consists of four pins totally out of which three of them for the three different colors. The 4th pin is common for all three colors and it’s either Cathode or Anode.

Circuit Diagram of RGB LED

Fig. 1: Circuit Diagram of RGB LED

Concept of Color

Color is the visual perceptual property corresponding in humans to the categories called red, blue, yellow, green and others. (Wikipedia definition)

We know that white light (say coming from the Sun) consist of mixture of colors. Remember the prism experiment you head of when you were in school?

Image showing triangular dispersion of light by Prism

Fig. 2: Image showing triangular dispersion of light by Prism

So the primary colors of light are Red, Blue and Green. All other colors we see are formed by mixing of these three colors in different proportions.

Image of Primary Colors

Fig. 3: Image of Primary Colors

When we see an object, we are not seeing the object but actually we are seeing the light reflected by the object. Confusing isn’t it?

Image showing visualization of color

Fig. 4: Image showing visualization of color

Let’s say that you are looking at a red car. Now the car appears red to you because it absorbs the green and blue part of the light incident on it and only allows the red part of the light to reflect.

So if you see a red colored car through a green filter (which allows only green light to pass through it), then it will appear in black color (since no light would reach your eyes)

And the conclusion is that

Table showing color composition of primary colors

Fig. 5: Table showing color composition of primary colors

 Concept of PWM

Concept of PWM

A square wave consist of two important parameters namely ON Time and OFF time. OFF Time is the duration for which a wave is at minimum amplitude (i.e. 0 or GND) and ON Time is the duration for which the wave is at maximum amplitude in a particular cycle. The ON Time is also called as “Pulse duration” or “Pulse width” and varying this parameter is called as Pulse Width Modulation.

Remember: Time period= ON Time + OFF Time

So while keeping the Time period constant, the OFF time would also vary when ON Time is altered

Diagram of Pulse Width Modulation

Fig. 6: Diagram of Pulse Width Modulation

Duty Cycle: The percentage of time for which the signal is high out of the total Time period. For example if pulse width of a given signal is 2msec and time period is 5msec then the duty cycle is given as

D=100 X 2/5 = 40%

Why use PWM?

Using PWM we can control the intensity of a particular color and hence mixing of the right proportions of the primary colors would become easier for us.

Example: Let’s say we want a color pink which is formed by mixing RGB colors in ratio 2:1:2. Then we set the Duty cycles of the corresponding pins as follows

R-100%

G-50%

B-100%

How to Achieve PWM in ATMega16

How to achieve PWM in ATMega16

ATMega16 consists of two 8-bit and one 16-bit timer. We would be using two of them to generate PWM signals. All we have to do is to manipulate the Timer Counter/Control Register (TCCRx) values.

Set the register values as follows:

TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);

TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);       

TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);

ICR1=255;

 

Well let me not go into details of each and every terminology mentioned in above code snippet. If you want to know more about them then refer ATMega16’s datasheet or refer to the AVR tutorials section.

So the R, G and B pins of the output LED will be connected to PB3, PD4 and PD5 pins of the controller.

 

Code Algorithm

·         Initialize the timers for Pulse width modulation and the Analog to Digital converter of the microcontroller.

·         Enter an infinite loop.

·         Read the analog voltages at ADC pins 1, 2 & 3 and convert them into a value of range 0-255 (Remember we are using ADC in 8 bit mode).

·         Set the duty cycles of the three PWM channels based on the ADC values obtained.

 

Project Source Code

###

/* PWMext.c

 *

 * Created: 9/28/2013 11:25:16 PM

 *  Author: GANESH SELVARAJ

 */ 

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

void InitPWM()
{
  TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
  TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);       
  TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10); 
  ICR1=255;  
   DDRB|=(1<<PB3);
   DDRD|=(1<<PD5)|(1<<PD4);
   OCR0=0;
   OCR1A=0;
   OCR1B=0;
}

void Waiting(int j) // simple delay function
{
uint8_t i;
for(i=0;i<j;i++)
_delay_ms(200);
}

void SetADC()
{
ADMUX|=(1<<REFS0)|(1<<ADLAR); //8 bit conversion
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}

uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX&=0b11100000;
ADMUX|=ch;

//Start Single conversion

ADCSRA|=(1<<ADSC);

//Wait for conversion to complete

while(!(ADCSRA & (1<<ADIF)));

//Clear ADIF by writing one to it

ADCSRA|=(1<<ADIF);

return(ADCH); //only higher byte is returned
}

void main()
{
      SetADC();
      InitPWM();
   while(1)
   {
OCR0=ReadADC(1);
OCR1A=ReadADC(2);
OCR1B=ReadADC(3);
   }
} 

###

 


Circuit Diagrams

Circuit-Diagram-AVR-ATMega16-Based-RGB-LED-Controller


Filed Under: Electronic Projects
Tagged With: atmega16, avr, led, rgb
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • MicroPython – I2C protocol in ESP8266 and ESP3
  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Photovoltaic MOSFET Drivers - Voltage Rating
  • Impedance requirement for SDRAM signals
  • A circuit that can adjust a resistance and probing a voltage node
  • A analogue circuit that spit out the resistance desired
  • DC to DC buck converter

RSS Electro-Tech-Online.com Discussions

  • Finally switched to Linux.
  • How to quickly estimate lead acid battery capacity ?
  • Multistage BJT amplifier
  • VOLTAGE GAIN AND VOUT RESISTENCE OF TRANSISTOR IN AC CIRCUIT
  • Enclosure sought
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering