Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based RGB Color Detection System with OLED and LCD Displays

View and Edit Project In Cirkit DesignerImage of Arduino-Based RGB Color Detection System with OLED and LCD Displays

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller interfacing with a 128x64 OLED Display, a 16x2 I2C LCD, and an Adafruit TCS34725 RGB Color Sensor. The circuit is designed to read color data from the RGB sensor and display the results on both the OLED and LCD screens.

Component List

128x64 OLED Display (I2C IIC SPI Serial)

Adafruit TCS34725 RGB Color Sensor

Arduino UNO

16x2 I2C LCD

Wiring Details

128x64 OLED Display (I2C IIC SPI Serial)

Adafruit TCS34725 RGB Color Sensor

Arduino UNO

16x2 I2C LCD

Code Documentation

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define WIDTH 128
#define HEIGHT 64

LiquidCrystal_I2C lcd(0x27, 20, 4);
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
Adafruit_SSD1306 display(WIDTH, HEIGHT, &Wire, 1);

void setup(void) {
  Serial.begin(9600);
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } 
  else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }

  lcd.init();
  lcd.clear();
  lcd.backlight();

  lcd.setCursor(0, 0);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)){
     Serial.println("OLED not found");
   }

  Serial.println("OLED FIND");
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,28);
  display.println("hello");
  display.display();
  
  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);
}

void loop(void) {
  uint16_t red, green, blue, c; 
  tcs.getRawData(&red, &green, &blue, &c); // Get raw data from the color sensor

  // Map the raw data to a usable range
  int r = map(red, 0, 21504, 0, 1025);
  int g = map(green, 0, 21504, 0, 1025);
  int b = map(blue, 0, 21504, 0, 1025);

  Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");

  if(r > g && r > b){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("RED");
  }
  else if(g > r && g > b){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("GREEN");
  }
  else if(b > r && b > g){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("BLUE");
  }
  else if(g == b && r == b){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ALL");
  }
  else if(g == b && g > r){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("GREEN & BLUE");
  }
  else if(g == r && g > b){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("GREEN & RED");
  }
  else if(r == b && r > g){
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("RED & BLUE");
  }
  else{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("else");
  }

  display.clearDisplay();

  // Draw red bar
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("R:");
  display.fillRect(20, 0, r, 10, SSD1306_WHITE);

  // Draw green bar
  display.setCursor(0, 15);
  display.print("G:");
  display.fillRect(20, 15, g, 10, SSD1306_WHITE);

  // Draw blue bar
  display.setCursor(0, 30);
  display.print("B:");
  display.fillRect(20, 30, b, 10, SSD1306_WHITE);

  display.display();

  delay(500);
}

This code initializes the RGB color sensor, the OLED display, and the LCD display. It reads color data from the sensor and displays the results on both the OLED and LCD screens. The color data is also printed to the serial monitor.