Introduction: DIY Color Sensor

Picture of DIY Color Sensor

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

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

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

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

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

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

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

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

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

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

Comments

OngE2 (author)2017-04-04

helo sir, i want to ask why my result is negative value when i near to red object with my sensor?

kartik00013 (author)2016-10-22

its good to see an Indian doing arduino and by the way I am also an Indian .

GerardoM21 (author)2016-01-14

// 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); }

ankur sharma (author)2015-12-24

explain me the connection

diy_bloke (author)2015-12-07

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'

gearup500 (author)2015-12-05

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

About This Instructable

9,559views

130favorites

License:

Bio: Thank you instructables!!
Add instructable to: