How to read a TCS3200 color sensor with Arduino (object sorting)

Asked 2 weeks ago Modified 3 days ago Viewed 7 times
How to read a TCS3200 color sensor with Arduino (object sorting)

What it does: the TCS3200 is a square chip with 64 photodiodes covered by R/G/B/clear filters. By selecting one filter at a time and counting the square-wave output frequency, you can measure how much of that colour the surface is reflecting.

Use case: I used it in a small conveyor demo that sorts coloured beads into bins. Worked well enough for a school science fair.

Parts

  • TCS3200 / TCS230 module (white LEDs on the breakout illuminate the target)
  • Arduino UNO
  • 6 × jumpers

Pin mapping

  • S0/S1 → set output frequency scaling (use 20% — S0 HIGH, S1 LOW)
  • S2/S3 → select which filter is active (R, G, B, or clear)
  • OUT → outputs a square wave whose frequency is proportional to light intensity through the active filter
  • LED → tie HIGH to turn on the white illumination ring

Sketch logic

1. Set S2/S3 = (LOW, LOW) → red filter active. Wait 50ms for the diode to stabilise.
2. Call pulseIn(OUT, LOW) — gives you the half-period in µs. Convert to frequency.
3. Repeat for (HIGH, HIGH) for green, (LOW, HIGH) for blue.
4. Now you have three raw values (R, G, B).

Calibration (essential!)

  • Hold a white card under the sensor, record R/G/B as 'white_max'.
  • Hold a black card, record as 'black_min'.
  • Normalize: value_norm = map(raw, black_min, white_max, 0, 255).
  • Now you have a roughly device-independent RGB.

Classification: I keep a small dictionary of (target_r, target_g, target_b, label) entries and pick the closest in Euclidean distance. Works for ~6 distinct colours reliably; beyond that you need to switch to a true colour sensor like the TCS34725 which gives proper RGB over I²C.

Mechanical tip: shroud the sensor in black tape with only a small viewing aperture, otherwise ambient light dominates everything.