Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • 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
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

DIY – How to generate different colours from RED-GREEN-BLUE LEDs using AVR microcontroller

By Ashutosh Bhatt May 6, 2015

The circuit presented here generates different colours from 3 LEDs – RED, GREEN and BLUE only using AVR micro controller ATMega32.

As we all know there are three primary colours – red, green and blue. All other colours can be made by mixing these three colours in different amount. The picture tube of any television set also uses this principle. Today’s modern LCD and LED TVs are also based on the same principle. They generate millions of different colours by setting different levels of red, green and blue colour and generates realistic like picture.

From these three primary colours, secondary colours yellow, cyan and magenta are generated by adding two these three colours as given below

RED + GREEN = YELLOW

GREEN + BLUE = CYAN

RED + BLUE = MAGENTA

The following figure shows the colour chart for primary and secondary colours

Image showing Primary Colors

Fig. 1: Image showing Primary Colors

The given circuit demonstrates how to generates these three primary and three secondary colours from three RED, GREEN, BLUE colour LEDs with the help of AVR micro controller

Circuit description:

(Check the circuit diagram tab for complete circuit for how to generate different colours from RED-GREEN-BLUE LEDs using AVR micro controller)

As shown in figure, the circuit is made up of 3 LEDs of red, green and blue colour, AVR micro controller ATmega32 and 8 MHz crystal

Three LEDs BLUE, RED and GREEN are connected with PORTB pins PB2, PB1 and PB0 respectively with 330Ω current limiting resistors as shown. All three LEDs are connected in sinking current configuration. One 8 MHz crystal along with two 22 pf capacitors is connected to crystal input pins XTAL1 and XTAL2. It gives basic clock internal operation and program execution.

Circuit operation:

The anodes of LEDs are connected to Vcc and cathodes are connected to PORTB pins. So to turn LED ON, low logic (0) has to be given to respective pin. 

To generate three primary colours RED, GREEN and BLUE, the respective LED is turned ON by making particular port pin low. To generate secondary colours, at a time any two LEDs are turned ON by making two pins low simultaneously. Please refer the table given below

Table showing bit values at output port for color generation on RGB LED

Fig. 2: Table showing bit values at output port for color generation on RGB LED

So to generate different colours, LEDs are turned ON by sending data to PORTB as per given in table. The data bytes are given to PORTB one by one with 2 second delay in between. The program is loaded into internal FLASH of ATmega32 to perform above functionality. Click on the tab to see the C program written and compiled in AVR studio software.

Project Source Code

###

#include <avr/io.h>

#include <util/delay.h>

 

int main()

 {

 

      DDRB = 0x07;            // declare PORTB pins as output

      PORTB= 0x07;            // set all pins to high to turn off all LEDs

      while(1)                // continue loop

        {

            PORTB = 0xFE;     // GREEN LED ON

            _delay_ms(2000);  // 2 second delay

            PORTB = 0xFD;     // RED LED ON

            _delay_ms(2000);

            PORTB = 0xFB;     // BLUE LED ON

            _delay_ms(2000);

PORTB = 0xFC;     // RED n GREEN ON => YELLOW

            _delay_ms(2000);

            PORTB = 0xF9;     // RED n BLUE ON  => MAGENTA

            _delay_ms(2000);

            PORTB = 0xFA;     // BLUE n GREEN ON => CYAN

            _delay_ms(2000);

 

     }

  } 

###

 


Circuit Diagrams

Circuit-Diagram-8051-Microcontroller-RGB-LED-Controller


Filed Under: Electronic Projects

 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

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

  • System for measuring the amount of Joules delivered from a capacitor discharge.
  • Two sections broadband impedance matching
  • The GaN revolution must now happen?
  • How to find the resonance frequency and impedance of a planar spiral coil in HFSS?
  • PhD for Electronics Designers

RSS Electro-Tech-Online.com Discussions

  • The Analog Gods Hate Me
  • Impact of Tariffs on PCB Fab
  • More fun with ws2812 this time XC8 and CLC
  • I Wanna build a robot
  • Wierd makita battery

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz
  • Amphenol RF introduces SMPM to SMPM assemblies on RG-178 cable

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • 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
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • 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
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe