Color Recognition w/ TCS230 Sensor and Arduino [Calibration Code included]

Overview

In this tutorial, you’ll learn about the TCS230 sensor and how to use it with Arduino to recognize colors. 

At the end of this tutorial, you’ll find a fascinating idea to create a color picker pen. With this pen, you can scan the colors of the objects around youand start painting on an LCD using that color. 

What You Will Learn

  • An introduction of TCS230 
  • How to use the TCS230 module with Arduino and recognize different colors 

What Is the TSC230 Sensor?

TSC230 chip contains an 8×8 array of silicon photodiodes, which can be used to recognize colors. 16 of these photodiodes have red filter, 16 have green filter, 16 have a blue filter and the other 16 have no filter.  

The TCS230 module has 4 white LEDs. Photodiodes receive the reflected light of these LEDs from the surface of the object, then generate an electric current depending on the color they received. 

In addition to photodiodes, there is also a current-to-frequency converter in this sensorIt converts the current generated by photodiodes to the frequency. 

The output of this module is in the form of square pulses with a duty cycle of 50%. 

The best measuring range for this sensor is about 2 to 4 cm. 

TCS230 Pinout

The TCS230 has 4 control pins. S0 and S1 are used for output frequency scaling, and S2 and S3 are used for selecting the type of the photodiode. (red, green, blue, no filter) 

The current-to-frequency converter circuit has frequency dividers. You can control this frequency divider with S0 and S1 control pins.

S0S1OUTPUT FREQUENCY SCALING (f0)S2S3PHOTODIODE TYPE
LLPower downLLRed
LH2%LHBlue
HL20%HLClear (no filter)
HH100%HHgreen

For example, if you want to measure the value of blue color in an object, you should set the S2 pin state to low, and the S3 pin state to high simultaneously. 

Required Materials

Hardware Components

Arduino UNO R3×1
TCS230 Color Recognition Sensor Module×1
Breadboard×1
RGB LED×1
2.4” TFT LCD **×1
Male to Female jumper wire×1
220 Ohm Resistor×3

Software Apps

Arduino IDE

TCS239 Color Sensor and Arduino interfacing

Connect the sensor to Arduino as you see in the following picture. Then analyze the output of different colors by initializing the pins S0 to S4. 

Circuit

Connect the sensor to the Arduino according to the following circuit.

Code

The following code measures the output signal for each of the three colors and displays the result on the serial port. 

The color function controls the S2 and S3 pins to read all the colors of the object. This function uses the pulseln command to receive the transmitted pulses by the color sensor. 

For more information, you can read this page. 

?: conditional operator

This command acts like if and else command.

If the condition is true, exp1, and else the exp2 will be executed.

TCS230 Color Sensor Calibration

In order to calibrate the sensor, you need a white object. 

The calibrate function performs the calibration of the sensor. To do this, simply enter the character “c” in the serial window. Then remove all the colored objects around the sensor and re-enter “c”. Now take a white object near the sensor and enter “c” again. 

After the calibration, if you keep the white object in front of the sensor, you should see the value of 255 (or around 255) for each of the three red, green, and blue colors in the serial window. 

The Calibrate function calculates and stores the maximum and minimum changes in the sensor output frequency in both non-colored and white colored environment. 

Then in the loop part, it maps the color changing range to 0-255 (or any other range you define). 

You can find more information about the map command here. 

Code

Make a Color Picker Pen with TCS230 Sensor and Arduino

If you use Arduino UNO, you have to solder the color sensor pins to the Arduino board using wires. But if you use Arduino MEGA, you can use the last pins of the board to connect the color sensor to it. 

If you are using the LCD shield for the first time, you can see the setup tutorial here. 

The following code creates a painting page on the LCDThe default color of the pen is red. Hold the key and close the color sensor to the desired object to select its color. Then the color of your pen changes to the color of that object.

Circuit

Code

The pick_color function is called when the key is pressed. It reads the color of the object located near the sensor and changes the pen color to that color.

What’s Next?

  • Make a small color sorter to sort lego pieces based on their color. 

Similar Tutorials

Share this post

Leave a Reply

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