TCS230 - connection scheme of the color sensor to the Arduino

Daniil Zhuk
In this manual, we will connect the TCS230 color sensor to the Arduino platform. To demonstrate the capabilities of the color sensor, we will build a detector system that can detect color on an object in front of the sensor and display it on a TFT display. In this project, we will use the TCS230 sensor and display the color on the ST7735 1.44-inch display.

To implement the project from this article, we need the following components:
  • Arduino Uno R3
  • TCS230 Color Sensor (TCS3200)
  • Color TFT-display 1.44 ′ (128 × 128px) SPI
  • 400-point development board
  • Daddy-Dummy Wire Kit

BRIEF OVERVIEW OF THE MODULE

TCS230 TCS3200 color sensor
The TCS230 sensor is a programmable light-to-frequency converter for color, which combines configurable silicon photodiodes and a current-to-frequency converter on one monolithic CMOS integrated circuit.

The color sensor module consists of the color sensor itself and four infrared LEDs that are used to illuminate an object located in front of the sensor to ensure the accuracy of the measurement results and their independence from ambient lighting. The sensor consists of a set of photodiodes with color filters for red, blue and green colors and with an anti-reflection filter on top of them.

To determine the color, the sensor reads data from a set of photodiodes arranged in an 8 × 8 matrix, which includes 16 photodiodes with blue filters, 16 with green filters, 16 with red filters, and 16 photodiodes without filters. Four types (colors) of photodiodes alternate with each other to minimize the effect of the irregularity of the intensity of the incident radiation. All 16 photodiodes for the same color are connected in parallel, and the photodiode used during operation is selected by means of pins.

The output of the TCS230 color sensor is a square wave with a 50% -filling factor, whose frequency is proportional to the intensity of light on the selected light filter.

CONNECTION SCHEME
Connect the TCS230 sensor and TFT display to the Arduino board, as shown in the diagram. The following shows how to connect the color sensor and TFT display leads to the Arduino board.

Connect the ST7735 (ST7735S) display to the Arduino:

  • LED – 3.3V
  • SCK – D13
  • SDA – D11
  • DC – D9
  • CS – D10
  • Reset – D8
  • GND – GND
  • VCC – 5V

Connect TCS230 color sensor to Arduino:

  • VCC – 5V
  • GND – GND
  • OUT – D5
  • S2 – D2
  • S3 – D3
  • OE – D4

Some models of the TCS230 module have a separate contact for powering the LEDs (LEDs); if present, you need to connect it to the 5V (5V) output of the Arduino board. Recheck the circuit again to make sure everything is connected correctly before moving on.

CONNECTING TO ARDUINO IDE
To make it easier to write code for this project, we need four libraries, two of which are needed to work with the color sensor and read data, and the other two to work with the TFT display.

The required libraries are as follows:

  1. Color Sensor Library
  2. Frequency count
  3. Adafruit ST7735
  4. Adafruit GFX

Libraries can be downloaded from the corresponding links in the list. After installing the libraries, we can start writing the code for the project.

The first thing we do is include the libraries we are going to use. In our case, these are the libraries mentioned above.

#include "MD_TCS230.h"
#include "FreqCount.h"
#include "Adafruit_ST7735.h"
#include "Adafruit_GFX.h"

Next, we determine the Arduino board pins to which the CS, DC and RST pins from our display will be connected.

#define cs 10
#define dc 9
#define rst 8

Next, we determine the color identifiers, matching them with hexadecimal values, and also determine the general-purpose contacts for the GPIO I / O board to which the leads from the color sensor will be connected.

// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GREY 0xC618

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

Next, we create three variables for the red (R - red), green (G - green) and blue (B - blue) colors. These variables will contain the numeric value received from the color sensor corresponding to each of the three colors.

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

Next, we create an instance of the library for the color sensor TCS230 and an instance of the library for the ST7735 display, indicating the necessary contacts.

MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);
int state = 0;
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

Having done all the preliminary work, we move on to the void setup () function, in which we perform the initial setup of the display and output the welcome text to it. Following we establish a connection with the color sensor and begin to read its data.

void setup() {

Serial.begin(57600);

tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab

drawBlackScreen();

tft.setCursor(20,30);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("Color");

tft.setCursor(20,60);
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.print("Sensor");

tft.setCursor(20,90);
tft.setTextColor(BLUE);
tft.setTextSize(2);
tft.print("Tutorial");

delay(3000);

drawBlackScreen();

tft.setCursor(10,30);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("SET BLACK");
delay(5000);

CS.begin();
CS.read();
}

Next, go to the function void loop (). The first thing we do in this function is to calibrate the color sensor, substituting for it black and white. If our device has recognized these colors, then the sensor is calibrated and is able to recognize the rest, intermediate colors.

void loop() {

if(state ==0)
{
if (CS.available()) {
sensorData sd;
CS.getRaw(&sd);
CS.setDarkCal(&sd);
Serial.println("Black Calibration Set");
state++;
drawBlackScreen();

tft.setCursor(10,30);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("SET WHITE");

delay(5000);
CS.read();
}
}

After the calibration is completed, the state variables get a value greater than zero and the program starts. From this point on, the module can read any color that will be provided to it.

The full program code for this project is presented below:

#include "MD_TCS230.h"
#include "FreqCount.h"
#include "Adafruit_ST7735.h"
#include "Adafruit_GFX.h"
 
#define cs   10  
#define dc   9  
#define rst  8  
 
// Color definitions
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0
#define WHITE    0xFFFF
#define GREY     0xC618
 
// 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);
  
int state = 0;
 
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
 
void setup() {
 
  Serial.begin(57600);
  
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
 
  drawBlackScreen();
  
  tft.setCursor(20,30);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.print("Color");
 
  tft.setCursor(20,60);
  tft.setTextColor(GREEN);
  tft.setTextSize(2);
  tft.print("Sensor");
 
  tft.setCursor(20,90);
  tft.setTextColor(BLUE);
  tft.setTextSize(2);
  tft.print("Tutorial");
 
  delay(3000);
  
  drawBlackScreen();
 
   tft.setCursor(10,30);
   tft.setTextColor(WHITE);
   tft.setTextSize(2);
   tft.print("SET BLACK");
   delay(5000);
  
   CS.begin();
   CS.read();
}
 
void loop() {
 
if(state ==0)
{
  if (CS.available()) {
   sensorData  sd;
   CS.getRaw(&sd);
   CS.setDarkCal(&sd);
   Serial.println("Black Calibration Set");
   state++;
   drawBlackScreen();
 
   tft.setCursor(10,30);
   tft.setTextColor(WHITE);
   tft.setTextSize(2);
   tft.print("SET WHITE");
 
   delay(5000);
   CS.read();
  }
}
else if(state == 1)
{
if (CS.available()) {
   sensorData  sd;
   CS.getRaw(&sd);
   CS.setWhiteCal(&sd);
   Serial.println("White Calibration Set");
   drawBlackScreen();
 
   tft.setCursor(35,30);
   tft.setTextColor(WHITE);
   tft.setTextSize(2);
   tft.print("COLOR");
   tft.drawRect(30,70,70,60,WHITE);
   state++;
  }
 
}else
{
  readSensor();
}
 
}
 
void drawBlackScreen()
{
  tft.fillScreen(BLACK);
  //Draw white frame
  tft.drawRect(0,0,127,159,WHITE);
  tft.drawRect(1,1,127,159,WHITE);
}
 
uint16_t convertRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)
{
  return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}
 
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];
      int color = convertRGB24toRGB565(R,G,B); //Convertion to 16bit color for the display
      
      delay(100);
      
      tft.fillRect(31,71,68,58,color);
 
      waiting = false;
    }
  }
}

Check the connections again to make sure everything is correct, then load the code into the Arduino board and place any colored material in front of the color sensor. Do not forget to first calibrate the sensor with black and white before starting to recognize other colors. In the figure below, the device recognizes a yellow color.

This is where our instruction ends. Color sorting has several uses in real life, and we are sure that this is very useful when it can be done with an inexpensive solution like the Arduino platform. What problems did you solve using this sensor? Write your ideas and projects in the comments! 

Read more →

Driver L298N and Arduino - wiring diagram

Daniil Zhuk
The microcontroller installed on the Arduino board is not capable of delivering high current through its pins. What to do if you need to manage relatively powerful engines, for example, to move the robot?

Read more →

LED matrix MAX7219 - connection to Arduino

Daniil Zhuk
Sooner or later there is a need to output data from the Arduino and display them on a relatively large display. Large size displays cost a lot for hobby projects, so one of the best options is an LED display.

Read more →

OLED I2C 128 x 64 px - Arduino connection diagram

Daniil Zhuk
In small devices, too, it is necessary to display any useful information, while maintaining compact dimensions. Regular screens, like the Nokia 3310, do not provide sufficient resolution, besides, they are not visible in the dark.

Read more →

Camera OV7670 - Connection to Arduino

Daniil Zhuk
Many are accustomed to the erroneous stereotype that on an eight-bit microcontroller it is impossible to do something more complicated than a weather station. And indeed, most projects on Arduino do not go beyond connecting primitive and annoying sensors to everyone, the potential of the controller is not revealed, so we decided to destroy this stereotype.

Read more →

☁️ DHT11, DHT22 and DHT21 sensors comparison

Daniil Zhuk
Today there are many sensors and modules that can be used to measure temperature and other indicators related to the maintenance of optimal human activity, as well as other things and organisms.

Read more →

🔘 Connecting button to Arduino

Daniil Zhuk
When you are just starting to get acquainted with Arduino, then your first scheme will most likely be connecting the LED to the board and flashing it at the frequency you specify in the program code.

Read more →

DS1302 - connection scheme for Arduino

Daniil Zhuk
Many projects or tasks require precise, temporary execution. For example, in the system of automatic watering there can be several modes: morning watering, day and evening.

Read more →

🔧 MPU 9250 and Arduino - wiring diagram

Daniil Zhuk
In this article, we will introduce the MPU 9250 9-axis 3D position sensor. This module can be used in aircraft (various drones and quadrocopters) and smartphones as a compass and navigation, as well as in robotics, various manipulators and devices associated with 3D control and gesture recognition.

Read more →

OLED 128x64px SPI - Arduino Connection Diagram

Daniil Zhuk
Information output from the microcontroller often creates more difficulties than the work of the main program itself - after all, it is necessary to “give” a few pins to connect, and a certain amount of memory for the graphics processing library.

Read more →