Introduction: Simple Arduino LED Color Sensor for Beginners

Picture of Simple Arduino LED Color Sensor for Beginners

There are a couple different types of color sensors.

The one used in this instructable is a "reflective" color sensor it has a red light source a green light source a blue light source(a R,G,B LED would work) and a light sensor. The way it works is first the red light source is turned on and the value of the light sensor is recorded. then the red light source is turned off this happens for the green and blue light sources as well.

the R,G,B value are then finely compared to come out with the color of the object but why does it do this? because when red light shines on a red object it reflects more light then when green are blue light shines on it. so when a green object is held over the sensor the green value recorded is higher then the red are blue values.

The other type of color sensor is a "refractive" color sensor it works on the same concept as the one above except it is opposite. it has three light sensors with red,green,blue color filters and a white light source. the way it works is it turns on the white light source and compares the value of the light sensors if the red value is higher then the green and blue values then the color is red.

The code i used in this instructable looks until it sees a green object then turns on a LED. The way it does this is i take the red,green,blue values divide them by ten (to make them easier to manage). then i place a colored object (in this case it is a green object) and then i use the variable val1,val2,val3 (their are three variable for red,green,blue) to add and subtract from the red,green,blue values to make them all the the same value in the case of this instructable the value is fifteen.

red = (sensorValue / 10) + val1;

then i have a "if statement" that compares the values with a small bit of "space" around the value in this case two on each side. so if the values are around fifteen it turns on a LED if not it turns of the LED.

if(blue > 13 and blue < 17 and green > 13 and green < 17 and red > 13 and red < 17)<br> digitalWrite(led4, HIGH);
 delay(1000);
 
 }
 else{
 digitalWrite(led4, LOW);
 }

So now on to the build!

Step 1: Tool's and Materiel's

The materiel's we are going to need for this project:

(1) Arduino Mega

(1) Light sensitive resistor

(2) Red LED

(1) Green LED

(1) Blue LED

(4) 220 ohm resistor

(1) 12K ohm resistor

Wire

Cardboard

Packaging tape

The tool's we are going to need for this project:

Breadboard

Wire cutter/stripper

Scissors

Computer with Arduino.ino installed

Step 2: Building the Circuit

Picture of Building the Circuit

Build the circuit as in the photograph above.

Note: the clear LED on the far left is the red LED. the clear LED in the middle is the green LED.

the clear LED on the right is the blue LED

Step 3: Make a Cardboard Tube to Cover the Sensor

Cut a piece of cardboard two inches high and six inches long.

Roll the cardboard in to a tube one inch in diameter and tape.

Step 4: Codeing and Using It!

Upload the code to the Arduino Mega.

int sensorPin = A0;
int red = 0; int green = 0; int blue = 0;
  int led1 = 2;
  int led2 = 3; 
  int led3 = 4; 
 
  int led4 = 8; 
//The red,green,blue values have to be adjusted due to the fact that the LED's are of different
//  brightness. 
 
  int val1 = -12;//Use this to adjust the red value
  int val2 = -4;//Use this to adjust the green value
  int val3 = -18;//Use this to adjust the blue value
 
  int sensorValue = 0;  
void setup() {
  
  pinMode(led1, OUTPUT); //Red LED
  pinMode(led2, OUTPUT); //Green LED 
  pinMode(led3, OUTPUT); //Blue LED 
  pinMode(led4, OUTPUT); //Detection LED 
}
void loop() {
 
 
 
  
  
 
  digitalWrite(led1, HIGH);                                                                                                                                                            
  sensorValue = analogRead(sensorPin); 
  red = (sensorValue / 10) + val1;
  
 
 delay(50); 
  digitalWrite(led1, LOW);
/////////////////////////////////
 
  digitalWrite(led2, HIGH);
  sensorValue = analogRead(sensorPin);
  green = (sensorValue / 10) + val2;
 
 
 delay(50); 
  digitalWrite(led2, LOW); 
/////////////////////////////////
 
  digitalWrite(led3, HIGH);
  sensorValue = analogRead(sensorPin);  
  blue = (sensorValue / 10) + val3;
   
 delay(50); 
 digitalWrite(led3, LOW);
 if(blue > 13 and blue < 17 and green > 13 and green < 17 and red > 13 and red < 17){
 digitalWrite(led4, HIGH);
 delay(1000);
 
 }
 else{
 digitalWrite(led4, LOW);
 }
}

Using it:

Place the cardboard tube over the LED's and light sensitive resister then place a dull green object over the sensor. and the LED connected to pin eight should come on.

Enjoy!

Comments

sammy_14 (author)2017-06-12

Can we use arduino uno instead of arduino mega ??

germap (author)2016-02-15

Interesting, thanks for sharing!

DannyK13 (author)2015-11-05

Is it possible to use sensor to measure the skin color? thank you

Strombergundy (author)2014-11-26

I hope this isn't a silly question, but would just a regular Arduino Uno work? I imagine it would be up to the task but wasn't 100%.

TimSwift (author)Strombergundy2014-11-27

Yes! You can use the Arduino Uno with the same circuit and code.

Hope this helps.

Anoop M N (author)TimSwift2015-08-25

and what are the polarities of the led in the breadboard. sorry, a silly question

MarkAvenius (author)Anoop M N2015-09-28

It looks like the cathodes are on the left for each LED. Power is coming from pins 2,3,4, and 8, through the respective resistors, and to the anodes of each LED.

TimSwift (author)2015-01-21

You may need to recalibrate the sensor. here is how to do it.

Upload this code:

int sensorPin = A0;
int red = 0;
int green = 0;
int blue = 0;

int led1 = 2;
int led2 = 3;
int led3 = 4;

int led4 = 8;
//The red,green,blue values have to be adjusted due to the fact that the LED's are of different
// brightness.

int val1 = -12;//Use this to adjust the red value
int val2 = -4;//Use this to adjust the green value
int val3 = -18;//Use this to adjust the blue value

int sensorValue = 0;

void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT); //Red LED
pinMode(led2, OUTPUT); //Green LED
pinMode(led3, OUTPUT); //Blue LED
pinMode(led4, OUTPUT); //Detection LED
}

void loop() {







digitalWrite(led1, HIGH);
sensorValue = analogRead(sensorPin);
red = (sensorValue / 10) + val1;



delay(50);
digitalWrite(led1, LOW);

/////////////////////////////////

digitalWrite(led2, HIGH);
sensorValue = analogRead(sensorPin);
green = (sensorValue / 10) + val2;



delay(50);
digitalWrite(led2, LOW);

/////////////////////////////////

digitalWrite(led3, HIGH);
sensorValue = analogRead(sensorPin);
blue = (sensorValue / 10) + val3;


delay(50);
digitalWrite(led3, LOW);
Serial.print(blue );
Serial.print(green);
Serial.print(red);

if(blue > 13 and blue < 17 and green > 13 and green < 17 and red > 13 and red < 17){
digitalWrite(led4, HIGH);
delay(1000);

}
else{
digitalWrite(led4, LOW);
}
}

Make these values zero.

int val1 = -12;//Use this to adjust the red value

int val2 = -4;//Use this to adjust the green value

int val3 = -18;//Use this to adjust the blue value

Then open the serial monitor and place the object you wish to sense on the sensor.

Chang these numbers up are down until the numbers on the serial monitor all equal fifteen. now your sensor should be calibrated.

huzaifaali.7393 (author)2014-12-29

red led is not working.....

TimSwift (author)huzaifaali.73932014-12-31

Is it one of the LEDs around the sensor?

huzaifaali.7393 (author)TimSwift2014-12-31

i just need detect green color by comparing it with yellow...

huzaifaali.7393 (author)TimSwift2014-12-31

No the led connected at pin 8

OmotayoO (author)2014-12-09

hey

our circuit isn't working properly. The r,g,b led are turning on but the light sensor isnt sensing the green light/object and the led that's suppose to turn on when it senses a green object doesn't turn on. we are using arduino uno by the way. What can we do to make the led that's suppose to be turning on turn on?

TimSwift (author)OmotayoO2014-12-09

It may be the wrong shade of green try changing these values slightly.

int val1 = -12;//Use this to adjust the red value
int val2 = -4;//Use this to adjust the green value
int val3 = -18;//Use this to adjust the blue value
miniscientist (author)2014-11-29

the compiler shows expected unqualified-id before else

TimSwift (author)miniscientist2014-11-29

Thanks for the feed back.

I had missed a curly bracket in the code. I fixed it.

miniscientist (author)TimSwift2014-11-30

thanks

brmarcum (author)2014-11-21

Could you include some explanation as to what is happening? It's a good project and it's great that it works, but why does it work? What's going on that makes green objects set it off?

TimSwift (author)brmarcum2014-11-21

I updated the instructable

brmarcum

Thank for the feedback!

Nanok (author)2014-11-20

maybe you will have more accurate results by toggling on an led and waiting a bit before reading the sensor, because the light resistor value don't change instantly. Nice little project anyway.

iam_maker_leo (author)2014-11-20

I think it is a good idea.... what using the LED color sensor.

I think tt can be used as a simple cummucation system with ethernet application.

skepticaljay (author)2014-11-20

Good job :-) You may have a typo in the "note" in step 2. You have red twice. But good job again.

TimSwift (author)skepticaljay2014-11-20

Thanks

I fixed the typo.

Fission Chips (author)2014-11-19

This looks really cool, and pretty easy to build! Thanks for posting this, and great work.

TimSwift (author)Fission Chips2014-11-19

Thanks :)

About This Instructable

24,072views

225favorites

License:

Bio: I enjoy reading adventure books, building robots, cooking, and helping my Dad do just about any project he's working on.
Add instructable to: