We can form almost any color by using the three primary colors, i.e. red, green, and blue. Using these colors we can form all most any color, each one having a different wavelength and intensity. Here we will detect different colors using a color sensor that uses white light made up of these three primary colors.
How does a color sensor work?
A color sensor consists of a white light emitter to illuminate the surface and three filters having wavelength sensitivities at 580nm, 540nm, and 450nm to measure the wavelengths of red, blue, and green colors respectively. The sensor converts light to a voltage which we read with the help of our microprocessor-based kits. The diode consists of 8x8 photodiodes which is a total of 64 photodiodes, of these 64 photodiodes, 16 are used to detect red color, 16 are used to detect green color, 16 are used to blue color and the remaining 16 are do not detect any color and are known as clear photodiodes.
How to use the module?
(i) Connections:
• Connect the 5V pin of the module to the 5V pin of the Arduino.
• Connect the GND of the module to the GND of the Arduino.
• Connect the OUT pin of the module to pin 8 of the Arduino.
• Connect S3 of the module to pin 6 of the Arduino.
• Connect S2 of the module to pin 7 of the Arduino.
• Connect S1 of the module to pin 5 of the Arduino.
• Connect S0 of the module to pin 4 of the Arduino.
(ii) Code:
- Before we can upload the code to the Arduino we need to calibrate the sensor.
- To calibrate the sensor we will use the pulse in function to find values of different colors and program our code accordingly to display the color the sensor detects on the serial monitor, We can also use an RGB LED to recreate the same color detected by the color sensor module which will make writing the code an easier and shorter task.
- After calibration of the module upload the following code to the Arduino to detect the colors.
- In the code below we are setting the frequency scaling to 20%.
- We are individually accessing the photodiodes to detect the colors and printing the said colors if the frequency of the color in question is in a particular range which satisfies the condition.
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
int redfrequency =0;
int greenfrequency =0;
int bluefrequency =0;
void setup(){
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);
pinMode(sensorOut,INPUT);
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
Serial.begin(9600):
}
void loop() {
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
redfrequency = pulseIn(sensorOut,LOW);
delay (100);
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
bluefrequency = pulseIn(sensorOut,LOW);
if redfrequency>25 & & redefrequency<77){
Serial.println ("RED COLOUR");
}
else if bluefrequency>25 & & bluefrequency<77){
Serial.println ("BLUE COLOUR");
}
else if greenfrequency>25 & & greenfrequency<77)
{
Serial.println ("GREEN COLOUR");
}
else
{
Serial.println ("NO COLOUR DETECTION");
}
}
Applications:
• Health fitness systems• Light color temperature measurement
• Process control for printing
• Color detection and sorting operations