DIY ELECTRONICSTR

← All projects

Colour Detection with the TCS3200

Intermediate30 min

A circuit that tells the colour of an object held in front of it. The TCS3200 board measures light through red, green and blue filtered photodiodes and puts out the chosen colour's strength as a square wave; the Arduino times the three colours' pulses with pulseIn, prints them to the Serial Monitor and names the strongest. It teaches choosing a filter with digital pins and timing pulses. No extra library needed.

Build it step by step in the app

What you need

Wiring steps

  1. 1

    Power the sensor

    Power the sensor

    Plug a red jumper wire into the 5V pin on the Arduino's POWER header and its other end onto the sensor board's Vcc pin; a black one from the GND pin at the start of the digital pins (next to pin 13) to the board's Gnd pin. Once the Arduino is on USB, the board's four white LEDs light up: they light the object. Leave the OE pin free for now.

    • Arduino 5V → TCS3200 Vcc (red wire)
    • Arduino GND → TCS3200 Gnd (black wire)
  2. 2

    Wire the frequency scaling

    Wire the frequency scaling

    Run a yellow wire from the Arduino's pin 4 to the board's S0 pin and a green one from pin 5 to S1. These two pins scale the sensor's output frequency down: the code sets S0 HIGH and S1 LOW for 20%, slow enough for the Arduino to time the pulses easily.

    • Arduino pin 4 → TCS3200 S0 (yellow wire)
    • Arduino pin 5 → TCS3200 S1 (green wire)
  3. 3

    Wire the colour filter pins

    Wire the colour filter pins

    Run an orange wire from pin 6 to S2 and a cyan one from pin 7 to S3. These two choose which colour the sensor looks at: both LOW opens the red filter, both HIGH the green one, S2 LOW with S3 HIGH the blue one. The code picks the three in turn and measures each.

    • Arduino pin 6 → TCS3200 S2 (orange wire)
    • Arduino pin 7 → TCS3200 S3 (cyan wire)
  4. 4

    Wire the output

    Wire the output

    Run a purple wire from the board's Out pin to the Arduino's pin 8. The sensor puts out the chosen colour's strength on this pin as a square wave: the stronger the colour, the shorter the pulses. The Arduino times them with pulseIn.

    • Arduino pin 8 → TCS3200 Out (purple wire)

Arduino sketch

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define SENSOR_OUT 8

// How long a pulse lasts through the chosen filter (microseconds).
// Smaller = more of that colour. 0 means no pulse came.
int readColour(int s2, int s3) {
  digitalWrite(S2, s2);
  digitalWrite(S3, s3);
  delay(20);  // give the sensor a moment after switching filters
  return pulseIn(SENSOR_OUT, LOW, 100000);
}

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(SENSOR_OUT, INPUT);

  // Frequency scaling 20%: S0 HIGH, S1 LOW
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  Serial.begin(9600);
}

void loop() {
  int red = readColour(LOW, LOW);
  int green = readColour(HIGH, HIGH);
  int blue = readColour(LOW, HIGH);

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

  // The colour with the shortest pulse is the one the object is closest to.
  if (red == 0 || green == 0 || blue == 0) {
    Serial.println("  -> ?");
  } else if (red < green && red < blue) {
    Serial.println("  -> R");
  } else if (green < red && green < blue) {
    Serial.println("  -> G");
  } else {
    Serial.println("  -> B");
  }

  delay(500);
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
R: 38  G: 112  B: 95  -> R
R: 37  G: 110  B: 93  -> R

If a wire is loose

SERIAL MONITOR · 9600 BAUD
R: 0  G: 0  B: 0  -> ?

What you will see

Hold a red object 1-2 cm in front of the sensor and the R value is the smallest, the line ending in “-> R”; a green object gives “-> G”, a blue one “-> B”. The numbers change with the room's light and the distance; what counts is how the three compare. If it keeps printing “R: 0 G: 0 B: 0”, check the Out wire and connect the OE pin to GND with a wire.

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Build it step by step in the app