Hello everyone!!,

I had spent my whole week tinkering with arduino and I also bought some sensors and modules for it but something was missing!!.And that is a Color Sensor!! After googling I found many ways of making it,I tried many and this one worked perfectly.This is a cheap but effective sensor that senses the R,G and B values of a image in front.Here is a video of it in action :-

## Step 1: Working

It is working on a very simple principle as we are giving all basic colors to the object in front,red,green and blue colors make white light present around us and when the white light strikes any object then it reflects back at different wavelength and hence we see the color of an object,this happens because the vibration reduces when the white light strikes object and hence the white light scatters into 7 colors and every single one changes at different value.Same s done here,firstly all primary colors are emitted by L.E.D s and then the LDR (Light Dependent Resistor) receives the changed wavelength hence giving us the R,G and B values of color of an object!!

## Step 2: Thank You Instructables and Seeed Studio,Mediatek Labs

I have got some money in return of publishing instructables using Mediatek Linkit One.And I bought Nexus 7 tab from Amazon.com.It arrived on 1st of December.I thank Instructables and Seeed Studio as well as Mediatek Labs for organizing this Giveaway!!!!

Thank you !!!!!!!

## Step 3: Things Required

We need :-

1. L.E.D s {Red,Green and Blue colors}
2. LDR
4. Solder
5. 220 ohms resistor
6. 10 kilo ohms resistor
7. Hot glue
8. Soldering Flux

Tools :-

1. Soldering iron
2. Hot glue gun
4. Nipper

## Step 4: Programming Arduino

The code is very simple.In this code you would easily understand how to use this sensor.

// Define colour sensor LED pins
int ledArray[] = {2,3,4};

// boolean to know if the balance has been set boolean balanceSet = false;

//place holders for colour detected int red = 0; int green = 0; int blue = 0;

//floats to hold colour arrays float colourArray[] = {0,0,0}; float whiteArray[] = {0,0,0}; float blackArray[] = {0,0,0};

//place holder for average int avgRead;

void setup(){ //setup the outputs for the colour sensor pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); //begin serial communication Serial.begin(9600); }

void loop(){

checkBalance(); checkColour(); printColour(); }

void checkBalance(){ //check if the balance has been set, if not, set it if(balanceSet == false){ setBalance(); } }

void setBalance(){ //set white balance delay(5000); //delay for five seconds, this gives us time to get a white sample in front of our sensor //scan the white sample. //go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array for(int i = 0;i<=2;i++){ digitalWrite(ledArray[i],HIGH); delay(100); getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well. whiteArray[i] = avgRead; digitalWrite(ledArray[i],LOW); delay(100); } //done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample. //set black balance delay(5000); //wait for five seconds so we can position our black sample //go ahead and scan, sets the colour values for red, green, and blue when exposed to black for(int i = 0;i<=2;i++){ digitalWrite(ledArray[i],HIGH); delay(100); getReading(5); blackArray[i] = avgRead; //blackArray[i] = analogRead(2); digitalWrite(ledArray[i],LOW); delay(100); } //set boolean value so we know that balance is set balanceSet = true; //delay another 5 seconds to allow the human to catch up to what is going on delay(5000); }

//prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works. void printColour(){ Serial.print("R = "); Serial.println(int(colourArray[0])); Serial.print("G = "); Serial.println(int(colourArray[1])); Serial.print("B = "); Serial.println(int(colourArray[2])); //delay(2000); }

## Step 5: Prototyping

Use a breadboard for prototyping and also check if the arrangement works with arduino.In the breadboar arrangement the :-

• Red L.E.D cathode to Digital Pin 2
• Green L.E.D cathode to Digital Pin 3
• Blue L.E.D cathode to Digital Pin 4
• 220 ohms resistor and LDR's connection to +5 V from arduino
• 10 kilo ohms resistor and LDR's connection to Analog Pin 0
• Other terminal of 10 kilo ohms resistor to Gnd from arduino.

It is very simple!!

## Step 6: Using the Perfboard

Cut out 6 male headers for perfboard using nipper or similar cutting tool and arrange all the components as you wish , leave plenty of space alone for LDR.Soory because in this image I had tried 100 kilo ohms resistor but this was just a try,I have used 10 kilo ohms resistor!!

## Step 7: Soldering

Solder all connections,remember the breadboard version.Connect all the components as we done it there.Use connecting wire for making connections if perfboard soldering is not possible!!Solder all wires to male headers in order as :-

Vcc,Red,Green,Blue,Analog,Gnd

## Step 8: Give It a Strong Bond

Use hot glue gun and use it over the backside of perfboard to give strengthen the connection and preventing wrong connections.

## Step 9: Done!!

This is perfect solution for projects related to color detection.This is the cheapest solution and is effective too!!Enjoy making your own DIY sensor!!

Thank you!!

<p>helo sir, i want to ask why my result is negative value when i near to red object with my sensor?</p>
its good to see an Indian doing arduino and by the way I am also an Indian .
explain me the connection<br>
<p>Great, but your explanation is not entirely correct as LDR's dont distinguish in wavelength. What is basically happening with these sensors is that when e.g. pointed at a red object there will be hardly any reflection of the green and blue and the LDR sees that as 'less light'</p>
<p>I think this is cool, although I can't see the video from my tablet. I think it's my tablet and not your video, though. XD</p>

8,824views

128favorites

Bio: Thank you instructables!!
More by Gursimran Singh 425: