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 Runs in your browser. Free, and no account needed.

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.

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);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 6; Resistor, 330 Ω pin 1
  • Arduino Uno R3 pin 5V; Color sensor, TCS34725 pin VIN; Addressable RGB LEDs pin VDD
  • Ground: Arduino Uno R3 pin GND; Color sensor, TCS34725 pin GND; Addressable RGB LEDs pin VSS
  • Arduino Uno R3 pin A4; Color sensor, TCS34725 pin SDA
  • Arduino Uno R3 pin A5; Color sensor, TCS34725 pin SCL
  • Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN

Change it and keep it

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