Color Sensor using Atmega16 microcontroller

Summary of Color Sensor using Atmega16 microcontroller


This article describes a color sensor project using an ATmega16 microcontroller to detect Red, Green, and Blue colors. The system works by sequentially illuminating an RGB LED and measuring the reflected light intensity via an LDR sensor connected to the MCU's ADC. Calibration involves adjusting variable resistors to equalize voltage readings for a white object. The provided software initializes the ADC, reads analog values, compares them, and activates corresponding display LEDs based on which color component is strongest.

Parts used in the Color Sensor Project:

  • Atmega16 MCU
  • LDR sensor
  • RGB LED
  • Variable resistors
  • Voltmeter
  • Display LEDs
  • AVR Studio 4 Software

This is a simple color sensor using  Atmega16 MCU and can sense Red ,Green and Blue color.
How it works:
the sensor consist from LDR sensor and RGB LED ,so when the object putted on the sensor the light that emitting  from RGB LED will reflected from the object  to LDR sensor and read it by ADC of ATMEGA16 as below sequence:
1-Turn on Blue in RGB LED and wait.
2-Read ADC and store it in the registry as blue.
3-Turn on Green in RGB LED and wait.
4-Read ADC and store it in the registry as green .
5-Turn on Red in RGB LED and wait.
6-Read ADC and store it in the registry as red.
7- if red      >green>blue turn on Red LED display.
if green >   red  >  blue turn on Green LED display.
if blue    >   red  >green turn on Blue Led display.
Calibration :
The RGB LED emitting red ,blue and green in different intensity so to equalize them , variable resistors should be used with below steps :
1-put a white object on the sensor.
2-turn on Blue    in RGB LED and read the voltage across the LDR sensor using voltmeter.
3-turn on Green in RGB LED and read the voltage across the LDR sensor using voltmeter.
4-turn on Red     in RGB LED and read the voltage across the LDR sensor using voltmeter.
5-adjust the variable resistances to make all the voltage equalized when white object putted on the sensor.
Software (AVR studio 4):
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
void InitADC()  //Initiate ADC
{
ADMUX=(1<<REFS0);                                     // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);    //Rrescalar div factor =128
}
uint16_t ReadADC(uint8_t ch)
{
ch=ch&0b00000111;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}
void Wait()
{
uint8_t i;
for(i=0;i<1;i++)
_delay_loop_2(0);
}
void main()
{
DDRB =0xFF; // Define output for RGB LED
DDRD=0xFF;  // Define output for display LEDs
uint16_t red;
uint16_t green ;
uint16_t blue ;
//Initialize ADC
InitADC();
while(1)
{
PORTD = 0b11111111;   //turn off all the display LEDs
PORTB = 0b11111110;   // Turn on Blue RGB
_delay_ms(2000);      // wait 2s
blue=ReadADC(0);      // Read Analog value and store it in blue
PORTB = 0b11111101;   //Turn on Green RGB ;
_delay_ms(2000);      //wait 2s
green = ReadADC(0);   //Read Analog value and store it in green
PORTB = 0b011111011;   //Red RGB;
_delay_ms(2000);       //wait 2s
red = ReadADC(0);      //Read Analog value and store it in red
if ((red > green) & (red > blue))   {PORTD =0b11111110; _delay_ms(4000);PORTD =0b11111111;}  //Red   Display
if ((green > red) & (green > blue)) {PORTD =0b11111011; _delay_ms(4000);PORTD =0b11111111;}  //Green Display
if ((blue > green) & (blue > red))  {PORTD =0b11111101; _delay_ms(4000);PORTD =0b11111111;}  //Blue  Display
}
}

Quick Solutions to Questions related to Color Sensor Project:

  • How does the sensor determine the color?
    The sensor turns on each color of the RGB LED sequentially, reads the reflected light via the LDR using the ADC, and compares the stored values to identify the dominant color.
  • Can I use this circuit without calibration?
    No, variable resistors must be adjusted to equalize the voltage output from the LDR when a white object is placed on the sensor.
  • What happens if the red value is greater than green and blue?
    The system turns on the Red LED display for 4 seconds before turning it off.
  • Which software is required for the code?
    The project requires AVR Studio 4 to compile and run the provided C code.
  • How long does the sensor wait between reading each color?
    The code waits for 2000 milliseconds (2 seconds) after turning on each specific RGB LED before reading the ADC value.
  • Does the sensor read all three colors at once?
    No, it reads the colors sequentially by turning on one LED at a time: Blue, then Green, then Red.
  • What is the role of the variable resistors in this project?
    They are used to adjust the intensity of the RGB LED outputs so that the voltage readings across the LDR are equalized for a white object.
  • How does the program initialize the ADC?
    The InitADC function sets the reference voltage to AVcc and configures the prescaler division factor to 128.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top