Color Detection Using RGB LED
Intro: Color Detection Using RGB LED
Have you ever wanted an automated way to detect the color of an object? By shining light of a certain color on the object and looking at how much light is reflected back, you can tell what color the object is. For example, if you shine a red light on a red object, that light will be reflected back. If you shine a blue light on a red object, the object will absorb some of that light and less of it will be reflected back.
STEP 1: Parts Needed
I used a PIC 16F887 Microcontroller, but almost any with a pulse-width-modulation capability will work.
1 RGB LED
1 Microcontroller
1 Standard red LED
1 1k ohm resistor
1 Photoresistor (changes resistance depending on the amount of light shone on it)
Some wires
I only need the microcontroller and RGB LED to have a wide range of color detectors, but if you only want a circuit that detects one color, you don't need a microcontroller - you only need a bright LED of the color you want to detect. The standard red LED is the "indicator LED" - it lights up when the right color is detected.
1 RGB LED
1 Microcontroller
1 Standard red LED
1 1k ohm resistor
1 Photoresistor (changes resistance depending on the amount of light shone on it)
Some wires
I only need the microcontroller and RGB LED to have a wide range of color detectors, but if you only want a circuit that detects one color, you don't need a microcontroller - you only need a bright LED of the color you want to detect. The standard red LED is the "indicator LED" - it lights up when the right color is detected.
STEP 2: Build the Circuit
The schematic is rather simple, and in general form, is shown below. The RGB LED is externally powered by a PWM signal.
I put electrical tape around the photoresistor so ambient light doesn't get in - only the light directly above it will be detected.
I put electrical tape around the photoresistor so ambient light doesn't get in - only the light directly above it will be detected.
STEP 3: The Code
This code was written for a Microchip PIC 16F887, but hopefully you can get the general idea. I used the built-in potentiometer on my development board to vary the color spectrum of the RGB LED (and it doesn't go through the entire spectrum because I don't have 3 PWM modules, but it's good enough)
Comments included.
#include <16F887.h>
#include <delay.h>
#include "delay.c"
#include <stdlib.h>
#include <STRING.h>
#use delay(clock = 4000000)
#FUSES INTRC,NOWDT,NOPUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN,NOLVP
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B
int value = 128;
int p1 = 0;
int p2 = 0;
void my_setup_ccp1(int8 value)
{
output_low(PIN_C2);
CCP1CON = value;
PWM1CON = 0;
}
void my_setup_ccp2(int8 value)
{
output_low(PIN_C1);
CCP2CON = value;
}
//===================================
void main()
{
//A4 = power source for photodiode
output_high(PIN_A4);
output_high(PIN_B1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
setup_adc_ports(sAN0);
//Timer/Interrupt setup
enable_interrupts(INT_TIMER2);
my_setup_ccp1(CCP_PWM);
my_setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 128, 1);
//setup_compare(2,COMPARE_PWM|COMPARE_TIMER2);
while(1){ // Prevent PIC from going to sleep.
//SET PWM DUTY CYCLE
output_high(PIN_A5);
//Pin A3 is the photodiode connection
if(input(PIN_A3) == 1)
output_high(PIN_A4);
else
output_low(PIN_A4);
//Read value of potentiometer to change color of LED
value = read_adc();
switch (value) {
case 0:
p1 = value;
output_low(PIN_C0);
p2 = value;
break;
case 50:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 100:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 150:
output_high(PIN_C0);
p1 = 50;
p2 = value;
break;
case 200:
output_low(PIN_C0);
p1 = 0;
p2 = value;
break;
case 250:
p1 = 0;
p2 = value;
output_low(PIN_C0);
break;
}
p1 = value;
p2 = 128 - p1;
set_pwm1_duty(p1);
set_pwm2_duty(p2);
}
}
Comments included.
#include <16F887.h>
#include <delay.h>
#include "delay.c"
#include <stdlib.h>
#include <STRING.h>
#use delay(clock = 4000000)
#FUSES INTRC,NOWDT,NOPUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN,NOLVP
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B
int value = 128;
int p1 = 0;
int p2 = 0;
void my_setup_ccp1(int8 value)
{
output_low(PIN_C2);
CCP1CON = value;
PWM1CON = 0;
}
void my_setup_ccp2(int8 value)
{
output_low(PIN_C1);
CCP2CON = value;
}
//===================================
void main()
{
//A4 = power source for photodiode
output_high(PIN_A4);
output_high(PIN_B1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
setup_adc_ports(sAN0);
//Timer/Interrupt setup
enable_interrupts(INT_TIMER2);
my_setup_ccp1(CCP_PWM);
my_setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 128, 1);
//setup_compare(2,COMPARE_PWM|COMPARE_TIMER2);
while(1){ // Prevent PIC from going to sleep.
//SET PWM DUTY CYCLE
output_high(PIN_A5);
//Pin A3 is the photodiode connection
if(input(PIN_A3) == 1)
output_high(PIN_A4);
else
output_low(PIN_A4);
//Read value of potentiometer to change color of LED
value = read_adc();
switch (value) {
case 0:
p1 = value;
output_low(PIN_C0);
p2 = value;
break;
case 50:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 100:
p1 = value;
output_high(PIN_C0);
p2 = value;
break;
case 150:
output_high(PIN_C0);
p1 = 50;
p2 = value;
break;
case 200:
output_low(PIN_C0);
p1 = 0;
p2 = value;
break;
case 250:
p1 = 0;
p2 = value;
output_low(PIN_C0);
break;
}
p1 = value;
p2 = 128 - p1;
set_pwm1_duty(p1);
set_pwm2_duty(p2);
}
}
STEP 4: Applications!
A simple color detector like this can be used in robotics, or for cool projects like separating legos by color, sorting M&Ms, or as an aid for color blindness.
Hopefully this instructable was helpful in enhancing a project you had in mind! :) LEDs are good for so many things....
Hopefully this instructable was helpful in enhancing a project you had in mind! :) LEDs are good for so many things....
36 Comments
henry post 4 years ago
pphanaga093 5 years ago
Email: ppha.naga@gmail.com
blueorbcleaning 7 years ago
Good afternoon
I
want to know if you have a color identify sensor that support android ?
like an android app can integrate with that sensor because I
want the voice from android app to tell what color is this
Looking forward hearing from you .
Thanks
please contact back 00971 52 610 3200
Thanks
Tryfon 7 years ago
thks
Tryfon 7 years ago
it is very interesting i have a assignment i will provide a brief report of 1 – 2 pages (length for guidance only) documenting the best strategy for interfacing the colour sensor with the microprocessor pic 18f 2455 . my decision should be clearly justified and the next step is to carry out design work on the code and produce a set of design notes which may include:
a flow chart of the operation of the program,
a list of the I/O pin allocations
A list of the subroutines you will use.
Pseudocode outline for your program
can you help do you have any idea????
email trikomo2014@gmail.com
vlachouli
00306937007704
BrentW14 8 years ago
Hi i would like to make something to detect red with a possible alarm on it.. how would i go about that? Thanks
sunil vishwakarma 8 years ago
hii i'm sunil now i wana to more information aboout color sensor & if i don't follows to th RBG rule then the how to work the sensor
and accepted to other colors VGO working
pls tell me sir its my project work
AndrewP112 8 years ago
I am making a similar device to detect a compound that illuminates one color (orange) under black light. My plan is to use a blue LED to excite the compound and an orange LED to sense it. Does anyone see any problem with this logic? Thanks.
hi.instruments.9 9 years ago
Many interesting circuit ideas on
www.artic-instruments.webs.com/
fzolkifli 10 years ago
can u make it on arduino uno?
amikoamoo 10 years ago
What it costs? my e-mail is amikopertenava@gmail.com
kvendhan b 10 years ago
svaladez1 10 years ago
My e-mail is cristysonia@yahoo.com
UNGUREANU 10 years ago
mtaqmilly 11 years ago
My email mtaqmilly@gmail.com
aimamo 11 years ago
Can i have your full scheme for color detection...
My email : dbestworld90@gmail.com.
thank you..
macxfady 11 years ago
Thks for the info..recently im doing the colour detection circuit
plz send me a schematic diagrams of full project.
my email: macxfady@gmail.com
thank you
ongjq 12 years ago
naveenraikar 13 years ago
My email id is: sraikar_naveen@yahoo.com
setu 13 years ago