Built-in project · Arduino Uno R3

Holi color catcher on an Arduino Uno

Hold anything colorful to a TCS34725 sensor and a strip of eight WS2812B LEDs catches its color, names it on the Serial Monitor and keeps a row of the last eight. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $37. On Mokxi they're free.

Holi color catcher · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading color with an I2C sensor
  • Comparing colors by their share of the total
  • Finding the nearest match in a list
  • Shifting values along an array
  • Using the clear channel to detect an object
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A TCS34725 sensor measures red, green, blue and clear light, with its own white LED to light what you hold up. The sketch turns the three colors into shares of their total, so a dim red and a bright red both read as red.

It compares those shares to a list of seven named colors and picks the nearest. A new color is added to the strip of 8 WS2812B LEDs, and the older ones shift along. Holding one thing still catches it only once.

Parts list

5 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 330 Ω pin 1Arduino Uno R3 pin 6
Color sensor, TCS34725 pin VINArduino Uno R3 pin 5V
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Color sensor, TCS34725 pin GNDArduino Uno R3 pin GND
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Color sensor, TCS34725 pin SDAArduino Uno R3 pin A4
Color sensor, TCS34725 pin SCLArduino Uno R3 pin A5
Addressable RGB LEDs pin DINResistor, 330 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Holi color catcher: hold anything colorful to the sensor, a flower, a shirt,
// a pinch of colored powder in a clear bag, and the strip catches its color,
// names it and keeps it in a row of the last eight colors you caught.
//
//   TCS34725  SDA -> A4, SCL -> A5, VIN -> 5 V, GND -> GND
//   palette   8 WS2812B LEDs, DIN <- 330 ohms <- pin 6
//
// The TCS34725 measures red, green, blue and clear light through four sets of
// filtered photodiodes, lit by its own white LED. The sketch turns the three
// colors into shares of their total, so a dim red and a bright red both read
// as red, and picks the nearest name from a short list.
//
// A new color is caught only when it is different from the last one and the
// sensor sees something close (the clear count is high), so holding one thing
// still catches it once, not over and over.
//
// Things to try. Change the color in front of the sensor. Add a color to
// NAMES with its red, green and blue shares in thousandths.

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <Adafruit_NeoPixel.h>

const int LEDS = 8;
const uint16_t NOTHING_BELOW = 1500;   // clear count with nothing close

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
Adafruit_NeoPixel palette(LEDS, 6, NEO_GRB + NEO_KHZ800);

struct Shade {
  const char *name;
  uint16_t red, green, blue;   // shares of the total, in thousandths
  uint32_t show;               // what the strip shows for it
};
const Shade NAMES[] = {
  {"red", 650, 200, 150, 0xFF0000},
  {"orange", 560, 340, 100, 0xFF6000},
  {"yellow", 470, 430, 100, 0xFFC000},
  {"green", 200, 520, 280, 0x00D040},
  {"blue", 140, 310, 550, 0x0040FF},
  {"purple", 330, 240, 430, 0x9000FF},
  {"pink", 480, 220, 300, 0xFF2080},
};
const int COUNT = sizeof(NAMES) / sizeof(NAMES[0]);

int last = -1;
int caught = 0;
uint32_t row[LEDS];   // the colors caught, newest first

int nearest(uint16_t r, uint16_t g, uint16_t b) {
  uint32_t total = (uint32_t)r + g + b;
  if (total == 0) return -1;
  long rs = (uint32_t)r * 1000 / total, gs = (uint32_t)g * 1000 / total, bs = (uint32_t)b * 1000 / total;
  int best = 0;
  long bestDistance = 0x7FFFFFFF;
  for (int i = 0; i < COUNT; i++) {
    long dr = rs - NAMES[i].red, dg = gs - NAMES[i].green, db = bs - NAMES[i].blue;
    long d = dr * dr + dg * dg + db * db;
    if (d < bestDistance) {
      bestDistance = d;
      best = i;
    }
  }
  return best;
}

void setup() {
  Serial.begin(9600);
  palette.begin();
  palette.setBrightness(150);
  palette.show();
  if (!tcs.begin()) {
    Serial.println("No TCS34725 found: check SDA and SCL");
    while (1) delay(10);
  }
  Serial.println("Color catcher ready: hold something colorful to the sensor");
}

void loop() {
  uint16_t r, g, b, c;
  tcs.getRawData(&r, &g, &b, &c);
  if (c < NOTHING_BELOW) {
    last = -1;   // nothing there: the next color counts as new
    delay(100);
    return;
  }
  int shade = nearest(r, g, b);
  if (shade < 0 || shade == last) {
    delay(100);
    return;
  }
  last = shade;
  caught++;
  // Slide the row along and put the new color first.
  for (int i = LEDS - 1; i > 0; i--) row[i] = row[i - 1];
  row[0] = NAMES[shade].show;
  for (int i = 0; i < LEDS; i++) palette.setPixelColor(i, row[i]);
  palette.show();
  Serial.print("Caught ");
  Serial.print(NAMES[shade].name);
  Serial.print(", color ");
  Serial.println(caught);
  delay(100);
}

Code walkthrough

The key functions of the sketch, and what each one does.

nearest sketch.ino

Adds red, green and blue, turns each into thousandths of that total, then measures the squared distance to every entry in NAMES. It returns the index with the smallest distance, or -1 if the sensor saw nothing.

int nearest(uint16_t r, uint16_t g, uint16_t b) {
  uint32_t total = (uint32_t)r + g + b;
  if (total == 0) return -1;
  long rs = (uint32_t)r * 1000 / total, gs = (uint32_t)g * 1000 / total, bs = (uint32_t)b * 1000 / total;
  int best = 0;
  long bestDistance = 0x7FFFFFFF;
  for (int i = 0; i < COUNT; i++) {
    long dr = rs - NAMES[i].red, dg = gs - NAMES[i].green, db = bs - NAMES[i].blue;
    long d = dr * dr + dg * dg + db * db;
    if (d < bestDistance) {
      bestDistance = d;
      best = i;
    }
  }
  return best;
}

setup sketch.ino

Starts the serial port and the LED strip, then looks for the sensor. If it is not found the sketch prints a message and stops, since nothing else can work.

void setup() {
  Serial.begin(9600);
  palette.begin();
  palette.setBrightness(150);
  palette.show();
  if (!tcs.begin()) {
    Serial.println("No TCS34725 found: check SDA and SCL");
    while (1) delay(10);
  }
  Serial.println("Color catcher ready: hold something colorful to the sensor");
}

loop sketch.ino

Reads the raw values. If the clear count is below NOTHING_BELOW nothing is close, so the next color counts as new. Otherwise it finds the nearest shade, and if it differs from the last one, slides the row along and shows the new color first.

void loop() {
  uint16_t r, g, b, c;
  tcs.getRawData(&r, &g, &b, &c);
  if (c < NOTHING_BELOW) {
    last = -1;   // nothing there: the next color counts as new
    delay(100);
    return;
  }
  int shade = nearest(r, g, b);
  if (shade < 0 || shade == last) {
    delay(100);
    return;
  }
  last = shade;
  caught++;
  // Slide the row along and put the new color first.
  for (int i = LEDS - 1; i > 0; i--) row[i] = row[i - 1];
  row[0] = NAMES[shade].show;
  for (int i = 0; i < LEDS; i++) palette.setPixelColor(i, row[i]);
  palette.show();
  Serial.print("Caught ");
  Serial.print(NAMES[shade].name);
  Serial.print(", color ");
  Serial.println(caught);
  delay(100);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • It says no TCS34725 found This project
    Check SDA on A4, SCL on A5, and VIN and GND on the rails.
  • Nothing is caught This project
    The object is too far away. The clear count must reach NOTHING_BELOW, which is 1500, so bring it close to the sensor.
  • The same color is not caught twice This project
    It only counts again after the sensor sees something different or sees nothing. Pull the object away first.
  • The colors are wrong or far too bright. Color sensor
    Set the integration time and gain for the light, since counts saturate easily. Keep the distance and light constant, and use the onboard white LED.
  • The sensor is not found. Color sensor
    The address is 0x29, fixed. Power it at 3.3 V and check SDA and SCL. The LED pin must be driven high to turn the white LED on.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
    Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.