Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

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

By Ganesh Selvaraj January 29, 2014

 

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
 

Next Article

← Previous Article
Next Article →

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.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

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!


RSS EDABOARD.com Discussions.

  • Lighting bus needed: 2kW at 40Vdc
  • Current sense transformer with Phase Shift Full Bridge
  • Interfacing Flash chip to FPGA
  • Regarding the element values for the cascode current mirror used in the DAC.
  • What to study as a Mechanical Engineer getting into RF?

RSS Electro-Tech-Online.com Discussions

  • strange laptop problem
  • JLCPBC are using a different shipping company = less $$$$$$$$
  • Manually actuate fuel tank selector solenoid
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • need two ICs

Featured – Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
  • A filter design example
  • Types of filter responses
  • What are the two types of hardware filters?
  • What are hardware filters and their types?

Recent Articles

  • GigaDevices introduces 32-bit MCUs with integrated DSP and FPU support
  • Grinn introduces 8-core SBC supporting AI-ready embedded development
  • EPC’s 100 kHz BLDC inverter supports high-efficiency motion control
  • Melexis announces 5 W smart driver to supports sensorless FOC operation
  • STMicroelectronics’ motion sensor simplifies industrial IoT system design

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 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

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe