Simple Arduino LED Color Sensor for Beginners

24,933

225

25

Published

Introduction: Simple Arduino LED Color Sensor for Beginners

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

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

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!

Share

    Recommendations

    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest

    25 Discussions

    Can we use arduino uno instead of arduino mega ??

    0
    user
    germap

    2 years ago

    Interesting, thanks for sharing!

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

    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%.

    3 replies

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

    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.

    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.

    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?

    1 reply

    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

    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?

    1 reply
    0
    user
    Nanok

    3 years ago

    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.