TCS230 RGB color sensor

Posted by

This post is in a series of ones published a long while ago in another blog of mine, which I will be revisiting here. We will be seeing how to use TCS230 RGB color sensor.

The code for the color sensor code is here: https://github.com/FritzenLab/TCS230-RGB-Color-Sensor

Today post and video is all about the TCS230, a light-to-frequency converter. In order to demonstrate its hability to sense color in RGB (Red, Green, Blue) I have made an Arduino sketch to convert its readings to a WS2812b, Neopixel RGB LED.

tcs230

Source: Arduino&Cia

   According to TCS230 datasheet its output frequency is up to 600Khz (square wave), with the possibility to reduce it to 20% (120Khz) and 2% (12Khz). It features two inputs (S2 and S3) to binary select which color will be read at the moment (either Red, Green or blue). Its supply voltage is up to 5V (same as Arduino during my tests).

   You can see  below both the schematic diagram of the test (drawn in Fritzing) and the code I used for my Arduino UNO (which is also available here). Notice that the sketch can control a Neopixel (WS2812b) and also send RGB color to the serial monitor of Arduino IDE.

tcs230 with arduino, schematic
/*     Arduino Color Sensing Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  Modified by Clovis Fritzen http://www.FritzenLab.com.br in May 1st, 2017
 *  
 *  Also using Adafuit NeoPixel code
 *  
 *  - This code reads a color sensor (TCS230) converts it to RGB and shows it
 *  back in an Adafruit NeoPixel. The color is read and then showed back!
 */
 
#include <Adafruit_NeoPixel.h>  
#ifdef __AVR__  
#include <avr/power.h>  
#endif  
   
 // Which pin on the Arduino is connected to the NeoPixels?  
#define PIN      9  
   
 // How many NeoPixels are attached to the Arduino?  
#define NUMPIXELS   1  
   
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);  
   
int delayval = 333; // delay  
 
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
 
int frequency = 0;
int Red, Green, Blue;
 
void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
 
  // Setting frequency-scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
 
  Serial.begin(9600);
  pixels.begin(); // This initializes the NeoPixel library.  
}
 
void loop() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of 0 to 255
  frequency = map(frequency, 25,72,255,0);
  if (frequency < 0) {
    frequency = 0;
  }
  if (frequency > 255) {
    frequency = 255;
  }
  Red= frequency;
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(100);
 
  // Setting Green filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of 0 to 255
  frequency = map(frequency, 30,90,255,0);
  if (frequency < 0) {
    frequency = 0;
  }
  if (frequency > 255) {
    frequency = 255;
  }
  Green = frequency;
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(100);
 
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of 0 to 255
  frequency = map(frequency, 25,70,255,0);
  if (frequency < 0) {
    frequency = 0;
  }
  if (frequency > 255) {
    frequency = 255;
  }
  Blue = frequency;
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  pixels.setPixelColor(0, pixels.Color(Red,Green,Blue)); // Moderately bright green color.
  pixels.setBrightness(64);  
  pixels.show(); // This sends the updated pixel color to the hardware.
  delay(100);
}

The target with this post was to show you that it is possible to identify RGB color “on the cheap”, based on a simple color sensor and some Arduino code. The precision of the TCS230 is not great, but it cuts for what we need. Its performance could be improved by better controlling room lighting and the quality of those white LEDs as well. 

The video of the test on the TCS230 color sensor is here:

Leave a Reply

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

CAPTCHA