Arduino Projects

TCS230 Color Sensor Arduino, Code, Circuit, Arduino Color Detector

TCS230 Color Sensor Arduino, Description:

 

TCS Color Sensor Arduino Based Color Scanner And Color Recognition- In this tutorial, you will learn how to use TCS230 Color Sensor with Arduino and find the RGB values of any color under test and then how to use these RGB values to recognize any color. So basically we will be making a color scanner and a color recognizer. Using the TCS230 Color Sensor is a bit tricky; there are things that you need to take care of while using this Color Sensor. While performing the tests I found some issues including the fluctuating values which can be due to a couple reasons. We will talk about these issues in detail, but first a few words about the sponsor of this tutorial.


About the Sponsor, PCBWay:

TCS230 Color Sensor

High quality & Only 24 Hours Build time

The PCB boards used in this project are sponsored by the PCBWay Company. Only 5 dollars for 10 PCBs and 30 dollars in total for 20 PCBs Assembly.  Besides this PCBWay also provides a great variety of services including Aluminum PCB, Rigid-Flex, Metal Core, flexible, High Frequency, High-TG, Thick-Copper, HDI, and LED PCBs. The sign up process hardly takes 1 minute and you are welcomed with a 5 dollars welcome bonus, what are you waiting for go and get your first prototype order for free.

Download Gerber Files:

While making the RGB color scanner, the first most prominent thing I noticed was the fluctuating RGB values. This was due to the surrounding light, you can clearly see on the Oled display module, as I move around the values start to fluctuate a lot. For the practical demonstration watch video tutorial given at the end of this article. I kept performing my tests using white and yellow color papers. I kept observing the values how the color sensor reacts when the surrounding light changes. The values also fluctuate when the distance is changed.

To get approximately the constant RGB values, I decided to cover this sensor to reduce the effect of the surrounding light and to keep the distance constant.

TCS230 Color Sensor

For this I used the match box. Now we can use this setup as the RGB Color Scanner, we can easily find the RED, GREEN, and Blue values of any color.

TCS230 Color Sensor

For the White color I am getting quite stable values, this is amazing. Now, we can use these values in the programming to recognize this color. Anyhow I kept performing my tests on the yellow, white, and black colors.

TCS230 Color Sensor

After I was satisfied with the RGB values, I started recording these values for the color recognition. First I started with the Yellow Color, I wrote the RGB values on a paper which later I will use in the programming. Next I noted down the RGB values for the white color, and finally I did it for the Black color. Now, we can use these RGB values for the recognition purposes.

I modified my RGB Color scanner program to make the Color Recognition system. I had to add a few lines of code. I defined range for each color to get the most accurate response. Although the effect of the surrounding light is reduced, but still we get some fluctuations in the RGB values. These fluctuations can be compensated by defining a range, which of course I will explain in the programming section.

In this tutorial, we will cover

  1. TCS230 Color Sensor Pinout and technical specification
  2. TCS230 Color Sensor Calibration
  3. Complete Circuit Diagram explanation
  4. Programming

Without any further delay, let’s get started!!!



Amazon Purchase links are given below:

TCS230 Color Sensor

12v Adaptor:

Arduino Uno

Arduino Nano

SSD1306 128×64 Oled i2c display Module

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

TCS230 Color Sensor Pinout and Technical Specifications:

TCS230 Color Sensor

This is the TCS230 programmable color light-to-frequency converter module. The TCS230 Color Sensor consists of the Silicon Photodiodes and a current-to-frequency converter on a single monolithic CMOS integrated circuit.

TCS230 Color Sensor

The light-to-frequency converter reads an 8 x 8 array of photodiodes. Sixteen photodiodes have blue filters, 16 photodiodes have green filters, 16 photodiodes have red filters, and 16 photodiodes are clear with no filters. The four types (colors) of photodiodes are interdigitated to minimize the effect of non-uniformity of incident irradiance. All 16 photodiodes of the same color are connected in parallel and which type of photodiode the device uses during operation is pin-selectable. Photodiodes are 120 μm x 120 μm in size and are on 144-μm centers.

TCS230 Color Sensor

In the functional diagram you can see Photodiode Array and current-to-frequency converter, this output a square wave “50% duty cycle” with frequency directly proportional to light intensity.


TCS230 Color Sensor Pinout and Technical Specifications:

The TCS230 Color Sensor module has a total of 8 male headers out of which we will be using only 6 male headers,

GND

OE which is the output enable

VCC

OUT

S2 and

S3

TCS230 Color Sensor

Selectable Options:

TCS230 Color Sensor

TCS230 Color Sensor

The TCS230 color Sensor Module is also provided with 4 white LEDs. This module can be easily powered up using the Arduino’s 5Volts. If you want to know more about the TCS230 programmable color light-to-frequency converter then consider reading the datasheet of the of the TCS230 Color Sensor.

TCS230 Datasheet:datasheet

TCS230 Color Sensor Arduino Circuit Diagram:

TCS230 Color Sensor

This is the complete circuit diagram same for the RGB color scanner and color recognition. The 5V regulated power supply based on the LM7805 voltage regulator is used to power up the Arduino Nano. J1 is the female DC power jack used to connect the external 12V power supply. Two 470uF decoupling capacitors are connected at the input and output sides of the voltage regulator. The regulated 5 volts from the power supply is connected with the VIN pin of the Arduino Nano, while the GND of the power supply is connected with the GND pin of the Arduino board.

The 128×64 Oled display Module is connected with the Arduino Nano through the I2C bus. The SCL and SDA pins of the Oled display module are connected with the Arduino A5 and A4. While the VCC and GND pins of the Oled display module are connected with the Arduino’s 5V and GND pins. If you have never used the Oled display module then I highly recommend read my getting started tutorial on the Oled Display Modules.

The TCS230 color Sensor VCC and GND pins are connected with the Arduino’s 5V and GND pins. The OE pin which is the output enable pin is connected with the Digital pin D4, S2 is connected with D2 of the Arduino, S3 is connected with D3, and OUT pin of the of the TCS230 color Sensor module is connected with the Arduino’s Pin 5.

PCB Board Designing for the Arduino Nano and TCS230 Color Sensor:

TCS230 Color Sensor

Next, I designed a PCB for the Arduino Nano, which I will use as the Development board. I added female headers for the 3.3V, 12V, 5V, and ground. The right side area can be used as the Vero Board for soldering other electronic components. I also added female headers on the left and right sides of the Arduino Nano for connecting the jumper wires. I double checked all the connections. And finally, generated the Gerber files and placed an online order on the PCBWay official website.


TCS230 Color Sensor and Oled display module Interfacing with Arduino Nano:

TCS230 Color Sensor

These are the PCB boards I received from the PCBWay Company. As you can see the Quality is really great. The silkscreen is quite clear and the solder mask looks amazing. Next, I started off by placing the components and completed the soldering job.

TCS230 Color Sensor

This is how the final circuit look after soldering. Don’t get yourself confused with the 10k resistor and 2n2222 NPN transistor; these are used for the relay.

TCS230 Color Sensor

Finally, I added the 128×64 I2C Oled Display Module and connected the wires of the TCS230 Color Sensor module as per the circuit diagram already explained.


TCS230 Color Sensor Arduino Code:

Before you start the programming first of all, make sure you download all the necessary libraries.

Wire.h 

MD_TCS230.h 

FreqCount.h

Adafruit_GFX.h 

Adafruit_SSD1306.h

If incase you don’t see any data on the Oled LCD module, then read my article on the Oled displays. May be your are using a wrong i2c address, or a wrong library. Oled related all the issues are discussed.

To recognize any color, first you should know about its RGB values. So this is the reason I decided to first start with the Color Scanner.

Color Scanner Arduino Code:

#include <Wire.h>
#include <MD_TCS230.h>
#include <FreqCount.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 


// Pin definitions
#define  S2_OUT  2
#define  S3_OUT  3
#define  OE_OUT   4    // LOW = ENABLED 

int R = 0;
int G = 0;
int B = 0;

MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);
  


void setup() {

  Serial.begin(57600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
   CS.begin();
   CS.read();
}

void loop() {


  readSensor();

//clear display
  display.clearDisplay();

  // display R G B Values
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("R: ");

  // display humidity
  display.setTextSize(2);
  display.setCursor(35, 0);
  display.print(R);


    display.setTextSize(2);
  display.setCursor(0,23);
  display.print("G: ");

  // display humidity
  display.setTextSize(2);
  display.setCursor(35, 23);
  display.print(G);


      display.setTextSize(2);
  display.setCursor(0,46);
  display.print("B: ");

  // display humidity
  display.setTextSize(2);
  display.setCursor(35, 46);
  display.print(B);

display.display(); 
}



void readSensor()
{
  static  bool  waiting = false;
  
  if (!waiting)
  {
    CS.read();
    waiting = true;
  }
  else
  {
    if (CS.available()) 
    {
      colorData  rgb;
      
      CS.getRGB(&rgb);
      Serial.print("RGB [");
      Serial.print(rgb.value[TCS230_RGB_R]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_G]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_B]);
      Serial.println("]");
      
      R = rgb.value[TCS230_RGB_R];
      G = rgb.value[TCS230_RGB_G];
      B = rgb.value[TCS230_RGB_B];

      waiting = false;
    }
  }
}

Color Scanner Code Explaination:

The purpose of the above code is just to print the RGB values on the Oled display module. This code can be used to find the RGB values of any color which you want to recognize. After uploading the above code, select any two or three colors. If you see any fluctuations in the values, this means you need to calibrate the TCS230 Color Sensor.



TCS230 Color Sensor Calibration:

  • You can use a small box to cover the sides of the sensor to reduce the effect of the surrounding light, as I explained in the beginning of this article.
  • You will need to check carefully, if any of the 4 white LEDs are flickering? If any white LED is flickering, then you will also get fluctuated values. A solution is de-solder that LED and solder another one. Or you can use three LEDs.
  • May be one or two LEDs are faulty.

You don’t have to be worried, if any of the LEDs are damaged. Still you can use this Color Sensor.

After everything is stable, and you can get the stable values. Then you can start recording the RGB values. If still you get very minor fluctuation, then you can define ranges for every color that you want to recognize. I modified the code written for the Color Scanner and added a few lines for the color recognition. The code is given below.

TCS230 Color Sensor Based Color recognition Arduino Code:

#include <Wire.h>
#include <MD_TCS230.h>
#include <FreqCount.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
// Pin definitions
#define  S2_OUT  2
#define  S3_OUT  3
#define  OE_OUT   4    // LOW = ENABLED 

int R = 0;
int G = 0;
int B = 0;

MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);
  


void setup() {

  Serial.begin(57600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
   CS.begin();
   CS.read();
}

void loop() {


  readSensor();

// For Yellow Color

  if ( (( R >= 140) && ( R <= 200)) && (( G >= 120) && ( G <= 180)) && (( B >= 90) && ( B <= 130)) )
  {
    //clear display
  display.clearDisplay();

  // display R G B Values
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Color:");


  display.setTextSize(3);
  display.setCursor(0, 28);
  display.print("YELLOW");

  display.setTextSize(1);
  display.setCursor(0, 56);
  display.print("electroniclinic.com");
  }

// For Black Color
   if ( (( R >= 250) && ( R <= 255)) && (( G >= 250) && ( G <= 255)) && (( B >= 0) && ( B <= 50)) )
  {
    //clear display
  display.clearDisplay();

  // display R G B Values
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Color:");


  display.setTextSize(3);
  display.setCursor(0, 28);
  display.print("BLACK");

  display.setTextSize(1);
  display.setCursor(0, 56);
  display.print("electroniclinic.com");
  }

  // For White Color
   if ( (( R >= 190) && ( R <= 220)) && (( G >= 200) && ( G <= 230)) && (( B >= 250) && ( B <= 255)) )
  {
    //clear display
  display.clearDisplay();

  // display R G B Values
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Color:");


  display.setTextSize(3);
  display.setCursor(0, 28);
  display.print("White");

  display.setTextSize(1);
  display.setCursor(0, 56);
  display.print("electroniclinic.com");
  } 

 
display.display(); 
}



void readSensor()
{
  static  bool  waiting = false;
  
  if (!waiting)
  {
    CS.read();
    waiting = true;
  }
  else
  {
    if (CS.available()) 
    {
      colorData  rgb;
      
      CS.getRGB(&rgb);
      Serial.print("RGB [");
      Serial.print(rgb.value[TCS230_RGB_R]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_G]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_B]);
      Serial.println("]");
      
      R = rgb.value[TCS230_RGB_R];
      G = rgb.value[TCS230_RGB_G];
      B = rgb.value[TCS230_RGB_B];

      waiting = false;
    }
  }
}

Color Recognition testing:

I uploaded the above code and I was able to recognize different colors. I successfully did for the White color, Black color, and Yellow color. For the step by step explanation and practical demonstration watch video tutorial given below.

TCS230 Color Sensor


Watch Video Tutorial:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

  1. Hi,

    Correction needed: In the video you were identifying RGB(255, 255, 255) as black color but in reality it should represent white color.

Leave a Reply

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

Back to top button