Project

Feel the Rainbow: Sensing Color with an Arduino

June 26, 2015 by Tim Youngblood

Since different colors reflect different levels of light, you can use a photoresistor or a phototransistor to sense different colors.

Since different colors reflect different levels of light, you can use a photoresistor or a phototransistor to sense different colors.

List of materials needed:

  • phototransistor
  • photoresistor
  • Arduino Mega
  • jumper wires
  • LEDs
  • LED current-limiting resistors (250 Ohms)
  • sensor calibration resistors (variable resistor, 10 ohm resistor)
  • breadboard
  • heat-shrink tubing

A phototransistor is a three-terminal device, just like an ordinary transistor, with a base, collector, and emitter, but the phototransistor base is also sensitive to light. A typical phototransistor is shown below. Light shining on the phototransistor will cause a base current to flow, which will then cause the transistor to conduct. The flow of current through the transistor is proportional to the intensity of light falling on the transistor base.


 

Light-dependent resistors, called photoresistors, change resistance when light shines on them. When no light is shining on them, the resistance is at the maximum value, at perhaps a megohm or more. When a light is shone on the photoresistor, the value decreases depending on how bright the light is. The minimum resistance may be only a couple hundred ohms. Below is a photo of a typical photo resistor:

Using a phototransistor or photoresistor as a color sensor

To use either the phototransistor or photoresistor as a color sensor, you make them part of a voltage divider circuit. Because both components can control the flow of current based on the intensity of light falling on them, the output of the voltage divider will be proportional to the light intensity. And, because different colors reflect different amounts of light, you can use them to detect the color of the reflected light if you calibrate the system accurately. 

Below is how you might configure your color sensor:

Vout  is the voltage that changes with the change in intensity. So, as the color in front of the sensor changes, Vout changes and this change is used to identify the color.

To calibrate the sensors, you'll have to do a little bit of experimentation. Put sheets of different colors in front of the sensor and note the output voltages. You can then use this values in your Arduino program.

Using heat-shrink tubing here is quite crucial. Using a heat-shrink tubing on the phototransistor or photoresistor will help block the ambient light and help ensure that the sensor only receives the light from the color sheet.

 

Setup

 

1. First, gather all the required components as shown below:

 

2. Assemble the circuit as shown in the figure below. First, we'll use a phototransistor as the color sensor.

 

 

3. After assembly, the breadboard will look like this:

 

4. Connect the Arduino to the laptop and observe the reading on the serial monitor:

 

​5. Calibrate the sensors by placing different colors in front of the sensors and recording the values.      

 

 

6. Once the values have been recorded, modify the code shown below, then upload the program to the Arduino. 

 

7. Replace the phototransistor with the photoresistor and repeat the calibration process. 

 

 

 

 

Code


int signal=A0;
int value=0;
void setup()
{
  Serial.begin(9600);          // opens serial port, sets data rate to 9600 bps
  pinMode(signal,INPUT);
}
void loop()
{
  delay(500);
  value=analogRead(signal);
  Serial.println(value);
  delay(500);

//Conditions for different colors

  if (value>100)
  {
    Serial.println("White");
  }
   else if (value<15)
  {
    Serial.println("Black");
  }
  else if (value<= 65 && value >50)
  {
    Serial.println("RED");
  } 
   else if (value>=68 && value<80) 
   {
    Serial.println("Green");
  }
  else if (value<30 && value>20)
  {
    Serial.println("Blue");
  }
}

Color_Sensing.zip

Videos

Give this project a try for yourself! Get the BOM.

3 Comments
  • hondabones August 28, 2015

    Light intensity is represented in a number of ways. Lumens, the measure of luminous flux. Candelas, measure of luminous intensity. All affected by the degree of light radiation from the device. This degree is especially important when using LEDs bought also affects other light sources. If you direct all of the light into one small area the intensity is exponentially greater.  This design is a nice idea but will fail depending on what kind of light it detects. One idea I might suggest is detecting the wavelength of the light as every color is a different wavelength. Again, I think intensity and angle of the beam may affect this.

    Like. Reply
  • V
    veerava May 11, 2016

    Extremely crude way to detect colour. Colour and intensity are two different parameters

    Like. Reply
  • P
    pictus June 14, 2016

    As already mentioned, colour & intensity are different parameters and as it stands this may be a confusing article. It does have potential, how about using 3 phototransistors, each covered with different colour film?

    Like. Reply