Electronics 101

Wiring & Testing the Colour Sensor

Before we perform the final mechanical assembly, let's verify that your colour sensor is wired correctly and working. This test will help you identify any issues early.

Wiring Diagram

Wiring diagram showing ESP32 connected to TCS34725 colour sensor

Interactive Wiring Guide

Connection Guide

ESP PinSensor PinWire Colour
VinVin
Red WireRed
GNDGND
Black WireBlack
D1SDA
Blue WireBlue
D2SCL
Green WireGreen

Test Code

Upload this code to verify your wiring. Open the Serial Monitor at 9600 baud to see the results.

colour_test.ino
1#include <Wire.h>
2#include "Adafruit_TCS34725.h"
3
4// Initialize with specific integration time and gain
5Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
6
7void setup() {
8  Serial.begin(9600);
9  
10  // Initialize I2C with specific pins: SDA=D1, SCL=D2 (Common for ESP8266)
11  Wire.begin(D1, D2);
12
13  Serial.println();
14  Serial.println("########## TCS34725 Color Sensor Test ##########");
15
16  
17  while (!tcs.begin()) {
18    Serial.println("Sensor not found! Check wiring!");
19    Serial.println("Retrying in 5 seconds.");
20    Serial.println("-------------------------------------");
21        delay(5000);
22  }
23
24  Serial.println("Sensor found! Wiring is correct!");
25  Serial.println("-------------------------------------");
26}
27
28void loop() {
29  uint16_t r, g, b, c;
30
31  // Read raw data from the sensor
32  tcs.getRawData(&r, &g, &b, &c);
33
34  // Avoid divide by zero errors if it is pitch black
35  if (c == 0) c = 1;
36
37  // Calculate standard RGB values (0-255) based on the clear channel
38  int redVal   = (int)((float)r / c * 255.0);
39  int greenVal = (int)((float)g / c * 255.0);
40  int blueVal  = (int)((float)b / c * 255.0);
41
42  // Print RGB values
43  Serial.print("R: "); Serial.print(redVal);
44  Serial.print(" G: "); Serial.print(greenVal);
45  Serial.print(" B: "); Serial.print(blueVal);
46  Serial.print(" -> Detected: ");
47  
48  // Determine and print the color name
49  String colorName = getColorName(redVal, greenVal, blueVal);
50  Serial.println(colorName);
51  Serial.println("--------");
52
53  delay(1000); // Wait 1 second before next reading
54}
55
56// Function to compare read RGB to known colors
57String getColorName(int r, int g, int b) {
58  struct Color { String name; int r; int g; int b; };
59  Color palette[] = {
60    {"Red",     255, 0,   0},
61    {"Green",   0,   255, 0},
62    {"Blue",    0,   0,   255},
63    {"Yellow",  255, 255, 0},
64    {"Cyan",    0,   255, 255},
65    {"Magenta", 255, 0,   255},
66    {"White",   255, 255, 255},
67    {"Black",   0,   0,   0},
68    {"Orange",  255, 165, 0},
69    {"Purple",  128, 0,   128},
70    {"Brown",   165, 42,  42},
71    {"Gray",    128, 128, 128}
72  };
73
74  String closestColor = "Unknown";
75  long minDistance = 200000L;
76
77  for (int i = 0; i < 12; i++) {
78    long dist = pow(r - palette[i].r, 2) +
79                pow(g - palette[i].g, 2) +
80                pow(b - palette[i].b, 2);
81               
82    if (dist < minDistance) {
83      minDistance = dist;
84      closestColor = palette[i].name;
85    }
86  }
87
88  // Simple check for "Black" based on low light levels
89  if (r < 30 && g < 30 && b < 30) {
90    return "Black";
91  }
92
93  return closestColor;
94}
Troubleshooting

If you see "Sensor not found, check wiring" in the Serial Monitor:

  • Check that wires are fully inserted into the breadboard
  • Ensure SDA is connected to D1 and SCL to D2
  • Verify VIN and GND are powered correctly
NEXT STEPS

Once verified, you can disconnect the wires from the breadboard to proceed with the mechanical assembly. Keep the sensor attached to the wires!