Skip to Content

COLOUR DETECTION AND DISPLAY SYSTEM

In our everyday lives, colour plays a vital role in how we perceive and interact with the world around us. Whether it’s ensuring the correct hues in industrial quality control, enhancing the visual appeal of interactive art installations, or simply improving the user interface of digital devices, the ability to accurately detect and interpret colours is indispensable. With recent advancements in sensor technology, we now have the opportunity to build systems that not only capture a wide range of colours but also provide immediate feedback in the form of precise RGB (Red, Green, Blue) values.

This project is designed to develop a comprehensive colour detection and display system using the TCS34725 colour sensor, which is renowned for its accuracy and sensitivity. By interfacing this sensor with an Arduino microcontroller, the system continuously monitors the intensity of red, green, and blue light present in its environment. The raw data from the sensor is then processed and converted into easily understandable RGB values, which can be displayed on an LCD screen or monitored through a serial interface. This real-time display of colour information not only facilitates quick visual feedback but also paves the way for integration into various applications, from automated sorting systems in manufacturing to interactive lighting solutions in smart homes.

Ultimately, this colour detection and display system is more than just a technical exercise, it’s a gateway to understanding how modern electronics can be used to interpret and interact with our visually rich environment, making it an ideal project for both educational purposes and practical implementations.

Components Required

  • Arduino Uno
  • TCS34725 Colour Sensor (alternatively, TCS3200 can be used)
  • LCD Display (or utilize the Serial Monitor)
  • Breadboard
  • Jumper Wires
  • USB Cable

Circuit Diagram

Component Connections

1.Connecting the TCS34725 Colour Sensor to Arduino
The TCS34725 sensor captures the intensity of red, green, and blue light through its built-in photodiodes. It communicates with the Arduino using the I2C protocol, which requires proper wiring to ensure accurate data transfer.

Wiring the TCS34725 Sensor to the Arduino:

  • VCC (Power Input): Connect to the Arduino’s 5V pin.
  • GND (Ground): Connect to the Arduino’s GND pin.
  • SDA (Serial Data Line): Connect to A4 on the Arduino.
  • SCL (Serial Clock Line): Connect to A5 on the Arduino.

Why use SDA and SCL pins?

These pins facilitate I2C communication, allowing the Arduino to efficiently exchange data with the sensor for real-time colour detection.

2. Connecting the LCD Display to Arduino
For visual output, an LCD display can be used to show the RGB values. In this example, an I2C LCD interface is assumed for simplicity.

Wiring the LCD Display to the Arduino:

  • VCC: Connect to the 5V pin on the Arduino.
  • GND: Connect to the Arduino’s GND.
  • SDA: Connect to A4 on the Arduino.
  • SCL: Connect to A5 on the Arduino.

Note: If using a parallel-interface LCD, please refer to its specific wiring instructions.

3. Powering the System
To ensure stable operation:

  • Sensor and LCD Power: The Arduino’s 5V output supplies consistent power to both the sensor and the display.
  • Arduino Power: Use a USB cable connected to a computer or a reliable power bank.
  • Important: Avoid connecting external power sources directly to the Arduino’s 5V pin unless properly regulated.

4. Installing the Components

  • Mounting the Sensor: Secure the TCS34725 sensor on a stable platform ensuring its lens is unobstructed for accurate ambient light measurement.
  • Positioning the LCD: Place the LCD where it can be easily viewed for real-time data monitoring.
  • Wiring Considerations: Arrange all components on the breadboard neatly, ensuring secure connections to prevent intermittent readings.

5. Uploading the Code to the Arduino

  • Open the Arduino IDE on your computer.
  • Copy the provided Arduino code into a new sketch.
  • Connect your Arduino to the computer via a USB cable.
  • Select the appropriate board and port in the IDE.
  • Click the Upload button to transfer the code.

Once the code is running, the Arduino will continuously read data from the TCS34725 sensor and display the RGB values either on the LCD or through the Serial Monitor.

CODE

#include <Wire.h>

#include "Adafruit_TCS34725.h"


// Initialize the TCS34725 sensor with a 50ms integration time and 4x gain

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);


void setup() {

  Serial.begin(115200);

  if (tcs.begin()) {

    Serial.println("TCS34725 sensor found.");

  } else {

    Serial.println("No TCS34725 detected. Please check your connections.");

    while (1);

  }

  // Initialize the LCD display if using one

  // For example, if using LiquidCrystal_I2C:

  // lcd.init();

  // lcd.backlight();

}


void loop() {

  uint16_t r, g, b, c;

  tcs.getRawData(&r, &g, &b, &c);

  float red = r;

  float green = g;

  float blue = b;

  // Display RGB values via Serial Monitor

  Serial.print("R: "); Serial.print(red);

  Serial.print("  G: "); Serial.print(green);

  Serial.print("  B: "); Serial.println(blue);

  // If using an LCD, update the display:

  // lcd.clear();

  // lcd.setCursor(0, 0);

  // lcd.print("R:"); lcd.print(red); lcd.print(" G:"); lcd.print(green);

  // lcd.setCursor(0, 1);

  // lcd.print("B:"); lcd.print(blue);

  delay(500);

}

CODE EXPLANATION

1. Including Libraries

#include <Wire.h>

#include "Adafruit_TCS34725.h"

  • #include <Wire.h>: This library enables I2C communication, which is essential for interfacing the Arduino with the TCS34725 colour sensor.
  • #include "Adafruit_TCS34725.h": This library provides a set of functions specifically designed for interacting with the TCS34725 sensor, making it easier to read the raw colour data.

2. Sensor Initialization

// Initialize the TCS34725 sensor with a 50ms integration time and 4x gain

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X)


  • An instance of the TCS34725 sensor is created with specific parameters:
    • Integration Time (50ms): Determines how long the sensor collects light before processing a reading. A longer integration time can improve accuracy in low-light conditions.
  • Gain (4X): Amplifies the signal from the sensor, making it more sensitive to colour variations.

3. Setup Function

void setup() {

  Serial.begin(115200);

  if (tcs.begin()) {

    Serial.println("TCS34725 sensor found.");

  } else {

    Serial.println("No TCS34725 detected. Please check your connections.");

    while (1);

  }

  // Initialize the LCD display if using one

  // For example, if using LiquidCrystal_I2C:

  // lcd.init();

  // lcd.backlight();

}

  • Serial Communication Initialization:
    • Serial.begin(115200);
      Starts serial communication at a baud rate of 115200, which is used for debugging and monitoring sensor data via the Serial Monitor.
  • Sensor Check:
    • if (tcs.begin()) { ... } else { ... }
      Attempts to initialize the TCS34725 sensor:
      • Successful Initialization:
        If the sensor is detected, a confirmation message ("TCS34725 sensor found.") is printed to the Serial Monitor.
      • Failure Handling:
        If the sensor is not detected, an error message is printed, and the program halts in an infinite loop (while (1);), prompting the user to check the connections.
  • LCD Initialization (Optional):
    • The commented-out section provides a template for initializing an LCD display if you choose to use one. This would typically involve initializing the display and turning on the backlight.

4. Loop Function – Reading, Processing, and Displaying Data

void loop() {

  uint16_t r, g, b, c;

  tcs.getRawData(&r, &g, &b, &c);

  float red = r;

  float green = g;

  float blue = b;

  // Display RGB values via Serial Monitor

  Serial.print("R: "); Serial.print(red);

  Serial.print("  G: "); Serial.print(green);

  Serial.print("  B: "); Serial.println(blue);

  // If using an LCD, update the display:

  // lcd.clear();

  // lcd.setCursor(0, 0);

  // lcd.print("R:"); lcd.print(red); lcd.print(" G:"); lcd.print(green);

  // lcd.setCursor(0, 1);

  // lcd.print("B:"); lcd.print(blue);

  delay(500);

}

  • Variable Declaration:
    • uint16_t r, g, b, c;
      These variables are declared to store the raw data from the sensor:
      • r, g, b: Represent the red, green, and blue light intensity values.
      • c: Represents the clear (ambient) light intensity, which can be used for further calculations if needed.
  • Sensor Data Acquisition:
    • tcs.getRawData(&r, &g, &b, &c);
      This function reads the raw data from the sensor and stores the values in the corresponding variables.
  • Data Conversion:
    • float red = r;
      Converts the raw integer values into floating-point numbers for more precise handling (if needed) and easier display formatting.
  • Displaying Data on the Serial Monitor:
    • Serial Print Statements:
      The code prints the RGB values to the Serial Monitor, providing immediate feedback:
      • Serial.print("R: "); Serial.print(red); prints the red value.
      • Serial.print(" G: "); Serial.print(green); prints the green value.
      • Serial.print(" B: "); Serial.println(blue); prints the blue value and moves to a new line.
  • Optional LCD Display Update:
    • The commented section provides an example of how to update an LCD display with the RGB values. If you’re using an LCD, you would uncomment and adapt these lines to suit your display's library and configuration.
  • Delay for Stability:
    • delay(500); Pauses the loop for 500 milliseconds to give a readable interval between updates, ensuring that the readings are stable and not too rapid to follow.

How It Works

The TCS34725 sensor continuously gathers light intensity data for each colour channel. The Arduino reads this data, converts it into RGB values, and then outputs these values for monitoring. This setup enables real-time colour detection and is versatile enough to be applied in various projects that require immediate and accurate colour analysis.

OBSERVING FUNCTIONALITY

  1. Real-Time Updates: As the sensor continuously measures the ambient light, the system displays updated RGB values in real time. Whether you're monitoring via the Serial Monitor or an LCD display, any changes in colour are reflected immediately.
  2. Visual Feedback: The LCD or serial output provides clear, concise numerical data for red, green, and blue intensities. This allows you to see how the sensor responds to different lighting conditions or objects, making it easier to verify that the system is functioning as intended.
  3. Dynamic Environment Response: When the surrounding light changes or when different coloured objects come into view, the system adjusts its readings accordingly. This dynamic response ensures that the displayed values accurately represent the current conditions, highlighting the sensor's sensitivity and precision.

COMMON PROBLEMS AND SOLUTIONS

1. Sensor Not Detected:

  • Cause: Incorrect wiring or insufficient power.
  • Solution: Verify that VCC is connected to 5V and GND is properly grounded.

2. Inaccurate Colour Readings:

  • Cause: Ambient lighting conditions may affect the sensor’s performance.
  • Solution: Calibrate the sensor in the environment where it will be used and consider adding a light shield if necessary.

Conclusion

This colour detection and display system exemplifies how modern sensor technology can be effectively combined with microcontroller platforms to provide real-time colour analysis. By accurately capturing and displaying RGB values, the project offers a robust solution for applications ranging from interactive art installations to automated quality control systems.


Comments

KNOB CONTROLLED LED BRIGHTNESS