Introduction: Color Detection Using RGB LED

Picture of 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

Picture of Parts Needed

I used a PIC 16F887 Microcontroller, but almost any with a pulse-width-modulation capability will work.

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

Picture of 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.

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)
#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B

int value = 128;
int p1 = 0;
int p2 = 0;

void my_setup_ccp1(int8 value)
CCP1CON = value;
PWM1CON = 0;

void my_setup_ccp2(int8 value)
CCP2CON = value;

void main()
//A4 = power source for photodiode

//Timer/Interrupt setup


setup_timer_2(T2_DIV_BY_1, 128, 1);

while(1){ // Prevent PIC from going to sleep.
//Pin A3 is the photodiode connection
if(input(PIN_A3) == 1)
//Read value of potentiometer to change color of LED
value = read_adc();
switch (value) {

case 0:
p1 = value;
p2 = value;
case 50:
p1 = value;
p2 = value;
case 100:
p1 = value;
p2 = value;
case 150:
p1 = 50;
p2 = value;
case 200:
p1 = 0;
p2 = value;
case 250:
p1 = 0;
p2 = value;

p1 = value;
p2 = 128 - p1;


Step 4: Applications!

Picture of 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....


blueorbcleaning (author)2016-07-25

Good afternoon

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 .


please contact back 00971 52 610 3200


Tryfon (author)blueorbcleaning2016-11-19


Tryfon (author)2016-11-19

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????




BrentW14 (author)2016-03-03

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 (author)2016-02-29

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 (author)2016-01-05

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 (author)2014-08-28

Many interesting circuit ideas on

fzolkifli (author)2014-04-02

can u make it on arduino uno?

amikoamoo (author)2014-02-20

What it costs? my e-mail is

kvendhan b (author)2013-08-31

really nice

svaladez1 (author)2013-06-30

Hi, may I have full scheme for you color sensor...
My e-mail is

UNGUREANU (author)2013-06-26

hi..could you send me the schematic for this email is you

mtaqmilly (author)2013-03-17

Can i have your full scheme for color detection...
My email

aimamo (author)2012-08-09

Can i have your full scheme for color detection...
My email :
thank you..

macxfady (author)2012-06-25

Thks for the info..recently im doing the colour detection circuit
plz send me a schematic diagrams of full project.
my email:
thank you

ongjq (author)2012-02-10

Thks for the info..recently im doing the colour detection circuit..can i have a copy of your schematic diagrams..this is my mail: a lot..

naveenraikar (author)2011-02-26

Plz send me full scheme for color detection...
My email id is:

setu (author)2011-01-10

its great...but u have to change every time the led light to sense the color ...actually i want to design a color sensor for my project which can sense RGB colors continuously without any need of changing led light every time...if there is a way plz tell id :

vojkann (author)2010-11-15

hi.... can you send me full scheme for color detection.. :D
It would be good if you send it to me... :D
here is my email:

qz9090 (author)2010-08-12

This is a pretty COOL Instructable and I would like to do the something similar with a project of mine but I am a little confused. 1) Can you provide some more technical details, for example, part numbers and sources to get the parts? 2) Also, you said the RBG LED is powered by an external PWM signal, but in your schematic, it looks like it is connected to a 5V power supply. Thanks.

jsnng00 (author)2010-02-05

Hi there? can u send me 1

Color Detection Using RGB LED diagram for me?

i need for my study.. grateful

mattthegamer463 (author)2009-05-21

Do you think you could do this with a white LED and 3 photoresistors, one with a red filter, one green filter and one blue filter? Then you could tell if something was say, yellow, because the indicator LEDs on the green and red photoresistors would trigger, or something purple, because the blue and red indicators would trigger, etc. Thoughts?

Arx (author)mattthegamer4632009-06-20

that would work, but the output of white leds isn't that linear. It might be easier to just cycle through the lighting colours quickly, and figure it out from there.

I think i would work better if you used different colored LEDs and just 1 photoresistor instead, it seems more reliable then using filters.

Possibly, but you need to reflect quite a lot of light off the object to make this work, but this project already requires that so yeah, a really bright white led and 3 filtered photoresistors would be gud hope i helped

draxos (author)2009-06-05

Very Nice Work Kyri.... Keep it up ..

Skater_j10 (author)2009-05-31

Great Idea! I to was wondering how to simplify color detection, and now all the hard work has been done already, a big thank you to Kyri. I will try to use this concept on my next bot!

dark sponge (author)2009-05-25

But if you hold up a shiny object, like a white lego or a mirror, wouldn't it say it found the color it was trying to detect?

praetorious (author)2009-05-20

Hey Nice work! I've done this before and have a few tips. You need to use a filter of some sort to remove IR radiation. I salvaged one from a camera. This will improve the detection. Also, I added white LEDs to illuminate the object so that the reflected light is then detected.
A guy over at a website called robotroom has done this. On my web page it's.
Still under construction and missing a few things...

Kyri (author)praetorious2009-05-20

Oooh, cool, thanks for the tips! Nice site, I like how you put a lot of the theory behind why this works (I only understand it so-so, mostly I just built the circuit, tried it, and to my surprise it worked!)

praetorious (author)Kyri2009-05-22

Thank-you. I honestly like what you have come up with. It's simplicity is a very desirable aspect of the design.

praetorious (author)2009-05-20

Oh yeah, you can also use the RGB LED itself to detect the light instead of the photoresitor. LEDs have a spectral response which is the same as the frequency of light they emit

nf119 (author)2009-05-19

wow, when i was watching the video i was wondering if you were from cmu for some reason, then i looked at your page, go ece! lolz

jeff-o (author)2009-05-19

Awesome! I was wondering how this might be accomplished - now I know. Thanks!

About This Instructable




Bio: ECE student at Carnegie Mellon University.
More by Kyri:Color Detection Using RGB LEDYour own Operation Board GameRoly Poly LED
Add instructable to: