loading

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
  3. Male headers
  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
  3. Breadboard
  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); }

void checkColour(){ for(int i = 0;i<=2;i++){ digitalWrite(ledArray[i],HIGH); //turn or the LED, red, green or blue depending which iteration delay(100); //delay to allow CdS to stabalize, they are slow getReading(5); //take a reading however many times colourArray[i] = avgRead; //set the current colour in the array to the average reading float greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between colourArray[i] = (colourArray[i] - blackArray[i])/(greyDiff)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned digitalWrite(ledArray[i],LOW); //turn off the current LED delay(100); } } void getReading(int times){ int reading; int tally=0; //take the reading however many times was requested and add them up for(int i = 0;i < times;i++){ reading = analogRead(0); tally = reading + tally; delay(10); } //calculate the average and set it avgRead = (tally)/times; }

//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!!

Please vote if you like!!

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 .
<p>// Define colour sensor LED pins</p><p>int ledArray[] = {2,3,4};</p><p>// boolean to know if the balance has been set boolean balanceSet = false;</p><p>//place holders for colour detected </p><p>int red = 0; </p><p>int green = 0;</p><p>int blue = 0;</p><p>//floats to hold colour arrays float </p><p>colourArray[] = {0,0,0}; </p><p>float whiteArray[] = {0,0,0};</p><p> float blackArray[] = {0,0,0};</p><p>//place holder for average int avgRead;</p><p>void setup(){ //setup the outputs for the colour sensor </p><p>pinMode(2,OUTPUT); </p><p>pinMode(3,OUTPUT); </p><p>pinMode(4,OUTPUT); //begin serial communication </p><p>Serial.begin(9600); }</p><p>void loop(){</p><p>checkBalance(); </p><p>checkColour();</p><p> printColour(); }</p><p>void checkBalance(){ //check if the balance has been set, if not, set it </p><p>if(balanceSet == false){ setBalance(); } }</p><p>void setBalance(){ //set white balance </p><p>delay(5000); //delay for five seconds, this gives us time to get a white sample in front of our sensor </p><p>//scan the white sample.</p><p> //go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array</p><p> for(int i = 0;i&lt;=2;i++){ </p><p> digitalWrite(ledArray[i],HIGH); </p><p> delay(100);</p><p> getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well. </p><p> whiteArray[i] = avgRead; </p><p> digitalWrite(ledArray[i],LOW);</p><p> delay(100); } //done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample. </p><p> //set black balance delay(5000); </p><p> //wait for five seconds so we can position our black sample</p><p> //go ahead and scan, sets the colour values for red, green, and blue when exposed to black </p><p> for(int i = 0;i&lt;=2;i++){ </p><p> digitalWrite(ledArray[i],HIGH); </p><p> delay(100); getReading(5);</p><p> blackArray[i] = avgRead; </p><p> //blackArray[i] = analogRead(2); </p><p> digitalWrite(ledArray[i],LOW);</p><p> delay(100); } //set boolean value so we know that balance is set </p><p> balanceSet = true; //delay another 5 seconds to allow the human to catch up to what is going on </p><p> delay(5000); }</p><p>void checkColour(){ </p><p>for(int i = 0;i&lt;=2;i++){ </p><p>digitalWrite(ledArray[i],HIGH); //turn or the LED, red, green or blue depending which iteration </p><p>delay(100); //delay to allow CdS to stabalize, they are slow </p><p>getReading(5); //take a reading however many times </p><p>colourArray[i] = avgRead; //set the current colour in the array to the average reading float</p><p> greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between</p><p> colourArray[i] = (colourArray[i] - blackArray[i])/(greyDiff)*255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned </p><p> digitalWrite(ledArray[i],LOW); //turn off the current </p><p> LED delay(100); } } </p><p> void getReading(int times){ </p><p> int reading; int tally=0; //take the reading however many times was requested and add them up </p><p> for(int i = 0;i &lt; times;i++){ </p><p> reading = analogRead(0); </p><p> tally = reading + tally;</p><p> delay(10); } //calculate the average and set it </p><p> avgRead = (tally)/times; }</p><p>//prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works.</p><p> void printColour(){ </p><p> Serial.print(&quot;R = &quot;); </p><p> Serial.println(int(colourArray[0])); </p><p> Serial.print(&quot;G = &quot;);</p><p> Serial.println(int(colourArray[1]));</p><p> Serial.print(&quot;B = &quot;);</p><p> Serial.println(int(colourArray[2]));</p><p> delay(2000); }</p>
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>

About This Instructable

8,605views

127favorites

License:

Bio: Thank you instructables!!
More by Gursimran Singh 425:Plant Box DIY CNC For 60$ (Large Work Area) Advanced Mosquito Repeller and Swatter 
Add instructable to: