«

»

Playing with a color sensor

Web

I’ve spent a day playing with the TCS3200 color sensor from TAOS (now ams AG). I’ve connected it to an Arduino and sent the color over the serial port to an application written in Processing. The application outputs the color found by drawing a square.

The hardware

  • Arduino Pro Mini (or any Arduino) (dx.com)
  • Color recognition sensor module (dx.com)

The datasheet for the TCS3200 is found here.

The Arduino Application

The Arduino application sets up the color sensor and retrieves the detected color every ~250ms. The color is sent through the serial port (USB) to the processing application.

The code

How to set up

Connect the color sensor to the Arduino following the pin configurations, or change the pin configuration to match your hardware set up (lines 5-10). All pins should be at digital pins, except Vcc and ground. I’ve powered my device from the Arduino USB power, and it worked nicely.

How it works

The color sensor is set up with a 2% frequency scaling, which means that the frequency range of the output pin is between 250Hz and 400Hz, where low frequencies are dark and high frequencies are bright. The frequency scaling type is chosen by toggeling the S1 and S2 pins. Each color is read separately, and which color to read is chosen by toggling the S2 and S3 pins.  The frequency of each color is read by the pulseIn call, which gives the period of the pulse. The values are calculated to match the values used to print colors on a computer screen, and sent by the serial port to the Processing application.

The Processing Application

This application receives the color value from the serial port and draws a rectangle from it.

ColorDetectionGui

The code

How to set up

Choose the correct COM port name for your Arduino. Mine was at “COM7”. This is set up in line 19.

How it works

The loop polls the serial line continuously for the pattern FFAA in hex. When this is found, the following three bytes will be the red, green and blue values. These bytes are read and used to color the rectangle.

 

2 comments

  1. Jack Nguyen

    ” red = 85000/tcs3200_getRed();
    green = 100000/tcs3200_getGreen();
    blue = 100000/tcs3200_getBlue(); ”

    could you please explain, why 85000 and 100000?

    Thanks

    1. larsivar

      Hi!
      I tweaked it so that the color I measured looked more accurate at the screen. It may be that the light in my workshop is more yellow than it looks, so it works like a white balance adjustment. I didn’t spend a whole lot of time on it, so it might be wrong. Go right ahead and tweak, you might get better results using 100000, or any other value 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">