Color Recognition w/ TCS230 Sensor and Arduino © GPL3+

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

  • 2,491 views
  • 1 comment
  • 19 respects

Components and supplies

Apps and online services

About this project

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 you, and 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 a red filter, 16 have a 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 sensor. It 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.

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

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.

/*
TCS230 color recognition sensor
modified on 7 May 2019
by Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
Color Sensor Arduino
----------- --------
VCC 5V
GND GND
s0 8
s1 9
s2 10
s3 11
OUT 12
OE GND
*/
const int s0 = 8;
const int s1 = 9;
const int s2 = 10;
const int s3 = 11;
const int out = 12;
// LED pins connected to Arduino
int redLed = 2;
int greenLed = 3;
int blueLed = 4;
// Variables
int red = 0;
int green = 0;
int blue = 0;
void color()
{
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
//count OUT, pRed, RED
red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
//count OUT, pBLUE, BLUE
blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
//count OUT, pGreen, GREEN
green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
}
void loop()
{
color();
Serial.print("R Intensity:");
Serial.print(red, DEC);
Serial.print(" G Intensity: ");
Serial.print(green, DEC);
Serial.print(" B Intensity : ");
Serial.print(blue, DEC);
//Serial.println();
if (red < blue && red < green && red < 20)
{
Serial.println(" - (Red Color)");
digitalWrite(redLed, HIGH); // Turn RED LED ON
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}
else if (blue < red && blue < green)
{
Serial.println(" - (Blue Color)");
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, HIGH); // Turn BLUE LED ON
}
else if (green < red && green < blue)
{
Serial.println(" - (Green Color)");
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH); // Turn GREEN LED ON
digitalWrite(blueLed, LOW);
}
else{
Serial.println();
}
delay(300);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}

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.

/*
// TCS230 color recognition with calibration
modified on 7 May 2019
by Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
Color Sensor Arduino
----------- --------
VCC 5V
GND GND
s0 8
s1 9
s2 10
s3 11
OUT 12
OE GND
*/
const int s0 = 8;
const int s1 = 9;
const int s2 = 10;
const int s3 = 11;
const int out = 12;
// LED pins connected to Arduino
int redLed = 5;
int greenLed = 6;
int blueLed = 3;
// Variables
int red = 0;
int green = 0;
int blue = 0;
// Calibrated value
int cal_min = 5;
int cal_max_r = 50;
int cal_max_g = 50;
int cal_max_b = 50;
void calibrate() {
Serial.println("Clear sensor area. Then enter c again");
while (Serial.read() != 'c') {
//do nothing
;
}
color();
cal_max_r = red;
cal_max_g = green;
cal_max_b = blue;
Serial.println("Put white color infront of sensor, Then enter c again");
while (Serial.read() != 'c') {
//do nothing
;
}
color();
cal_min = (red + green + blue) / 3;
Serial.println("calibrated successfully.");
delay(300);
}
void color() {
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
//count OUT, pRed, RED
red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
//count OUT, pBLUE, BLUE
blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
//count OUT, pGreen, GREEN
green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
}
void loop()
{
color();
if (Serial.read() == 'c') {
calibrate();
}
red = map(red, cal_min, cal_max_r, 255, 0);
green = map(green, cal_min, cal_max_g, 255, 0);
blue = map(blue, cal_min, cal_max_b, 255, 0);
Serial.print("R Intensity:");
Serial.print(red);
Serial.print(" G Intensity: ");
Serial.print(green);
Serial.print(" B Intensity : ");
Serial.println(blue);
delay(200);
}

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 LCD. The 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

/*
TCS230 color picker + paint
Modified on 8 May 2019
By Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
Color Sensor Arduino
----------- --------
VCC 5V
GND GND
s0 5V
s1 5V
s2 10
s3 11
OUT 12
OE GND
*/
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
//LCD
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, ccolor;
#define MINPRESSURE 10
#define MAXPRESSURE 1000
//Color sensor
const int s2 = 10;
const int s3 = 11;
const int out = 12;
const int button = 13;
// Color variables
int red = 0;
int green = 0;
int blue = 0;
// Calibrated value
int cal_min = 5;
int cal_max_r = 50;
int cal_max_g = 50;
int cal_max_b = 50;
uint16_t my_color;
void calibrate() {
Serial.println("Clear sensor area. Then enter c again");
while (Serial.read() != 'c') {
//do nothing
;
}
color();
cal_max_r = red;
cal_max_g = green;
cal_max_b = blue;
Serial.println("Put white color infront of sensor, Then enter c again");
while (Serial.read() != 'c') {
//do nothing
;
}
color();
cal_min = (red + green + blue) / 3;
Serial.println("calibrated successfully.");
delay(300);
}
void color() {
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
//count OUT, pRed, RED
red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
//count OUT, pBLUE, BLUE
blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
//count OUT, pGreen, GREEN
green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
void pick_color() {
color();
red = map(red, cal_min, cal_max_r, 255, 0);
green = map(green, cal_min, cal_max_g, 255, 0);
blue = map(blue, cal_min, cal_max_b, 255, 0);
Serial.print("R Intensity:");
Serial.print(red);
Serial.print(" G Intensity: ");
Serial.print(green);
Serial.print(" B Intensity : ");
Serial.println(blue);
//my_color = tft.color565(abs(255 - red), abs(255 - green), abs(255 - blue));
my_color = tft.color565(red, green, blue);
tft.fillRect(0, 0, 6*BOXSIZE, BOXSIZE, my_color);
delay(100);
}
void setup()
{
Serial.begin(9600);
tft.reset();
delay(1000);
uint16_t identifier = tft.readID();
if (identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if (identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if (identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if (identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if (identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(F("#define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
return;
}
tft.begin(identifier);
tft.fillScreen(BLACK);
tft.fillRect(0, 0, 6*BOXSIZE, BOXSIZE, RED);
my_color = RED;
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
pinMode(button, INPUT_PULLUP);
}
void loop()
{
if (Serial.read() == 'c') {
calibrate();
}
while (digitalRead(button) == LOW) {
pick_color();
}
TSPoint p = ts.getPoint();
// if sharing pins, you'll need to fix the directions of the touchscreen pins
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
/*
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
*/
if (p.y < (TS_MINY-5)) {
Serial.println("erase");
// press the bottom of the screen to erase
tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
}
// scale from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
/*
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
*/
if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
tft.fillCircle(p.x, p.y, PENRADIUS, my_color);
}
}
}

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.

Code

Code 1Arduino
/*
  TCS230 color recognition sensor
  modified on 7 May 2019
  by Mohammadreza Akbari @ Electropeak
  https://electropeak.com/learn/

Color Sensor      Arduino
-----------      --------
 VCC               5V
 GND               GND
 s0                8
 s1                9
 s2                10
 s3                11
 OUT               12
 OE                GND
*/
const int s0 = 8;  
const int s1 = 9;  
const int s2 = 10;  
const int s3 = 11;  
const int out = 12;   
// LED pins connected to Arduino
int redLed = 2;  
int greenLed = 3;  
int blueLed = 4;
// Variables  
int red = 0;  
int green = 0;  
int blue = 0;

void color()  
{    
  digitalWrite(s2, LOW);  
  digitalWrite(s3, LOW);  
  //count OUT, pRed, RED  
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s3, HIGH);  
  //count OUT, pBLUE, BLUE  
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s2, HIGH);  
  //count OUT, pGreen, GREEN  
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
}
    
void setup()   
{  
  Serial.begin(9600); 
  pinMode(s0, OUTPUT);  
  pinMode(s1, OUTPUT);  
  pinMode(s2, OUTPUT);  
  pinMode(s3, OUTPUT);  
  pinMode(out, INPUT);  
  pinMode(redLed, OUTPUT);  
  pinMode(greenLed, OUTPUT);  
  pinMode(blueLed, OUTPUT);  
  digitalWrite(s0, HIGH);  
  digitalWrite(s1, HIGH);  
}  
    
void loop() 
{  
  color(); 
  Serial.print("R Intensity:");  
  Serial.print(red, DEC);  
  Serial.print(" G Intensity: ");  
  Serial.print(green, DEC);  
  Serial.print(" B Intensity : ");  
  Serial.print(blue, DEC);  
  //Serial.println();  

  if (red < blue && red < green && red < 20)
  {  
   Serial.println(" - (Red Color)");  
   digitalWrite(redLed, HIGH); // Turn RED LED ON 
   digitalWrite(greenLed, LOW);  
   digitalWrite(blueLed, LOW);  
  }  

  else if (blue < red && blue < green)   
  {  
   Serial.println(" - (Blue Color)");  
   digitalWrite(redLed, LOW);  
   digitalWrite(greenLed, LOW);  
   digitalWrite(blueLed, HIGH); // Turn BLUE LED ON  
  }  

  else if (green < red && green < blue)  
  {  
   Serial.println(" - (Green Color)");  
   digitalWrite(redLed, LOW);  
   digitalWrite(greenLed, HIGH); // Turn GREEN LED ON 
   digitalWrite(blueLed, LOW);  
  }  
  else{
  Serial.println();  
  }
  delay(300);   
  digitalWrite(redLed, LOW);  
  digitalWrite(greenLed, LOW);  
  digitalWrite(blueLed, LOW);  
 }
Code 2Arduino
/*
  // TCS230 color recognition with calibration
  modified on 7 May 2019
  by Mohammadreza Akbari @ Electropeak
  https://electropeak.com/learn/

  Color Sensor      Arduino
  -----------      --------
  VCC               5V
  GND               GND
  s0                8
  s1                9
  s2                10
  s3                11
  OUT               12
  OE                GND
*/
const int s0 = 8;
const int s1 = 9;
const int s2 = 10;
const int s3 = 11;
const int out = 12;
// LED pins connected to Arduino
int redLed = 5;
int greenLed = 6;
int blueLed = 3;
// Variables
int red = 0;
int green = 0;
int blue = 0;

// Calibrated value
int cal_min = 5;
int cal_max_r = 50;
int cal_max_g = 50;
int cal_max_b = 50;

void calibrate() {
  Serial.println("Clear sensor area. Then enter c again");
    while (Serial.read() != 'c') {
      //do nothing
      ;
    }
    color();
    cal_max_r = red;
    cal_max_g = green;
    cal_max_b = blue;
    Serial.println("Put white color infront of sensor, Then enter c again");
    while (Serial.read() != 'c') {
      //do nothing
      ;
    }
    color();
    cal_min = (red + green + blue) / 3;
    Serial.println("calibrated successfully.");
    delay(300);
}

void color() {
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}

void setup()
{
  Serial.begin(9600);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
}

void loop()
{
  color();
  if (Serial.read() == 'c') {
    calibrate();
  }


  red = map(red, cal_min, cal_max_r, 255, 0);
  green = map(green, cal_min, cal_max_g, 255, 0);
  blue = map(blue, cal_min, cal_max_b, 255, 0);
  Serial.print("R Intensity:");
  Serial.print(red);
  Serial.print(" G Intensity: ");
  Serial.print(green);
  Serial.print(" B Intensity : ");
  Serial.println(blue);
  delay(200);
}
Code 3Arduino
/*
  TCS230 color picker + paint
  Modified on 8 May 2019
  By Mohammadreza Akbari @ Electropeak
  https://electropeak.com/learn/

  Color Sensor      Arduino
  -----------      --------
  VCC               5V
  GND               GND
  s0                5V
  s1                5V
  s2                10
  s3                11
  OUT               12
  OE                GND
*/

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

//LCD
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, ccolor;

#define MINPRESSURE 10
#define MAXPRESSURE 1000

//Color sensor
const int s2 = 10;
const int s3 = 11;
const int out = 12;

const int button = 13;


// Color variables
int red = 0;
int green = 0;
int blue = 0;

// Calibrated value
int cal_min = 5;
int cal_max_r = 50;
int cal_max_g = 50;
int cal_max_b = 50;

uint16_t my_color;

void calibrate() {
  Serial.println("Clear sensor area. Then enter c again");
  while (Serial.read() != 'c') {
    //do nothing
    ;
  }
  color();
  cal_max_r = red;
  cal_max_g = green;
  cal_max_b = blue;
  Serial.println("Put white color infront of sensor, Then enter c again");
  while (Serial.read() != 'c') {
    //do nothing
    ;
  }
  color();
  cal_min = (red + green + blue) / 3;
  Serial.println("calibrated successfully.");
  delay(300);
}

void color() {
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}

void pick_color() {
  color();
  red = map(red, cal_min, cal_max_r, 255, 0);
  green = map(green, cal_min, cal_max_g, 255, 0);
  blue = map(blue, cal_min, cal_max_b, 255, 0);

  Serial.print("R Intensity:");
  Serial.print(red);
  Serial.print(" G Intensity: ");
  Serial.print(green);
  Serial.print(" B Intensity : ");
  Serial.println(blue);

  //my_color = tft.color565(abs(255 - red), abs(255 - green), abs(255 - blue));
  my_color = tft.color565(red, green, blue);
  tft.fillRect(0, 0, 6*BOXSIZE, BOXSIZE, my_color);
  

  delay(100);

}

void setup()
{
  Serial.begin(9600);
  tft.reset();
  delay(1000);
  uint16_t identifier = tft.readID();

  if (identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if (identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if (identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if (identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if (identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("#define USE_ADAFRUIT_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    return;
  }

  tft.begin(identifier);

  tft.fillScreen(BLACK);

  tft.fillRect(0, 0, 6*BOXSIZE, BOXSIZE, RED);
  my_color = RED;

  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop()
{
  if (Serial.read() == 'c') {
    calibrate();
  }

  while (digitalRead(button) == LOW) {
    pick_color();
  }

  TSPoint p = ts.getPoint();

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    /*
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
    */
    
    if (p.y < (TS_MINY-5)) {
      Serial.println("erase");
      // press the bottom of the screen to erase 
      tft.fillRect(0, BOXSIZE, tft.width(), tft.height()-BOXSIZE, BLACK);
    }
    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    /*
    Serial.print("("); Serial.print(p.x);
    Serial.print(", "); Serial.print(p.y);
    Serial.println(")");
    */
    
    if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
      tft.fillCircle(p.x, p.y, PENRADIUS, my_color);
    }
  }
}

Comments

Similar projects you might like

Color Sensor Prototype: Detect Color Name from Objects

Project tutorial by moretticb

  • 8,946 views
  • 7 comments
  • 33 respects

Arduino Color Recognition

by millerman4487

  • 13,523 views
  • 11 comments
  • 34 respects

Easy Motion and Gesture Detection by PIR Sensor & Arduino

Project tutorial by ElectroPeak

  • 67,046 views
  • 13 comments
  • 308 respects

Displaying Sensor Values on LCD

by Wimpie van den Berg

  • 73,588 views
  • 6 comments
  • 52 respects

DHT11 Temperature/Humidity Sensor

Project tutorial by Zachary Haslam

  • 58,843 views
  • 5 comments
  • 61 respects

Arduino - Have Fun with Color Sensor

Project tutorial by khanhhs

  • 9,088 views
  • 2 comments
  • 45 respects
Add projectSign up / Login