Build it step by step
0 of 6 doneWire the sensor
SDA and SCL to A4 and A5, power from 5V, and LED to D4.
Wire the display and button
OLED on the same I2C pins, button between D5 and GND.
Shield the sensor from outside light
Make a short black tube or hold the object very close, so room light does not change the reading.
White balance
Hold the sensor on a sheet of plain white paper, press the button and release.
Read samples
Hold the sensor on coloured paper or fabric. Note the RGB numbers on the display.
Replace the swatch table
Put your own readings into the table for names that match your samples.
How do you build a colour reader and matcher?
Hold objects up to a colour sensor and see the name and RGB value, matching paint or fabric swatches. You need Arduino Nano, TCS34725 colour sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| TCS34725 VIN | Nano 5V | Sensor supply (the module has a regulator) |
| TCS34725 GND | Nano GND | Common ground |
| TCS34725 SDA | Nano A4 | I2C data |
| TCS34725 SCL | Nano A5 | I2C clock |
| TCS34725 LED | Nano D4 | Switch the on-board white LED |
| OLED SDA and SCL | Nano A4 and A5 | Display, shared bus |
| White-balance button | Nano D5 and GND | Hold on white paper |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "Adafruit TCS34725", "Adafruit SSD1306" and "Adafruit GFX Library". The swatch values are starting points, and the best matches come from reading your own samples and putting those numbers in the table.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_TCS34725.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_TCS34725 tcs(TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_4X);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
struct Swatch { const char *name; byte r, g, b; };
const Swatch swatches[] = {
{"Red", 200, 40, 40}, {"Orange", 220, 110, 40}, {"Yellow", 210, 190, 50},
{"Green", 50, 150, 70}, {"Blue", 50, 80, 180}, {"Purple", 110, 60, 140},
{"White", 210, 210, 200}, {"Black", 30, 30, 30}
};
float refR = 1, refG = 1, refB = 1; // white reference, set with the button
void setup() {
pinMode(4, OUTPUT); digitalWrite(4, HIGH);
pinMode(5, INPUT_PULLUP);
tcs.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
}
void loop() {
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
if (c == 0) return;
if (digitalRead(5) == LOW) { // white balance: store the white sheet's ratios
refR = (float)r / c; refG = (float)g / c; refB = (float)b / c;
delay(300);
}
float rn = constrain((float)r / c / refR * 210.0, 0, 255);
float gn = constrain((float)g / c / refG * 210.0, 0, 255);
float bn = constrain((float)b / c / refB * 210.0, 0, 255);
int best = 0; float bestD = 1e9;
for (byte i = 0; i < 8; i++) {
float d = sq(rn - swatches[i].r) + sq(gn - swatches[i].g) + sq(bn - swatches[i].b);
if (d < bestD) { bestD = d; best = i; }
}
oled.clearDisplay();
oled.setTextSize(2); oled.setCursor(0, 0); oled.print(swatches[best].name);
oled.setTextSize(1); oled.setCursor(0, 28); oled.print("R "); oled.print((int)rn);
oled.print(" G "); oled.print((int)gn); oled.print(" B "); oled.print((int)bn);
oled.display();
delay(300);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| White paper after balance | Shows White and values near 210 |
| Red card | Name is Red and R is much larger than G and B |
| Same card with the room lights on and off | Roughly the same colour, which shows the shielding works |
| Black cloth | Shows Black |
If it does not work
Colours look washed out
The sensor is too far from the object. Hold it a few millimetres away.
Always shows the same colour
The white balance was set on a coloured surface. Redo it on white paper.
Sensor is not found
Check SDA and SCL are not swapped and the board is powered.
Values change with room light
Add a shroud around the sensor and the object.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the colour reader and matcher?
Arduino Nano, TCS34725 colour sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the colour reader and matcher take to build?
About 90 minutes for a beginner maker, in 6 steps.
How do I know the colour reader and matcher works?
White paper after balance: Shows White and values near 210. Red card: Name is Red and R is much larger than G and B. Same card with the room lights on and off: Roughly the same colour, which shows the shielding works.
Colours look washed out: what should I check?
The sensor is too far from the object. Hold it a few millimetres away.
Always shows the same colour: what should I check?
The white balance was set on a coloured surface. Redo it on white paper.
Sensor is not found: what should I check?
Check SDA and SCL are not swapped and the board is powered.
Values change with room light: what should I check?
Add a shroud around the sensor and the object.
