Table of Contents

ESP32 TCS34725

This is an example of the TCS34725 reading rgb color code from neopixel, and displaying it on lcd

Prequisits

lib_deps = adafruit/Adafruit LiquidCrystal@^2.0.2

Example

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_LiquidCrystal.h>
#include <Adafruit_TCS34725.h>
#include <Adafruit_NeoPixel.h>

/* NEOPIXEL */
void colorWipe(uint32_t color, int wait);
#define LED_PIN 12
#define LED_COUNT 8
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN);

#define SDA_PIN 17
#define SCL_PIN 16

#define LCD_RS 48
#define LCD_ENABLE 47
#define LCD_D4 34
#define LCD_D5 33
#define LCD_D6 26
#define LCD_D7 21

static Adafruit_LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

/* TCS34725 */
#define commonAnode true
byte gammatable[256];
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);


void setup() {
  Serial.begin(152000);

  // Initialize I2C on custom pins
  Wire.begin(SDA_PIN, SCL_PIN);

  // Initialize LCD
  if (!lcd.begin(16, 2)) {
    Serial.println("Could not init LCD");
    while (1);
  }
  Serial.println("LCD ready.");
  lcd.print("IOT-OPEN");

  strip.begin();
  strip.show();

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

  // it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;

    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;
    }
    //Serial.println(gammatable[i]);
  }
}
<refnotes>
back-ref-format : none
</refnotes>

void loop() {

  strip.clear();
  colorWipe(strip.Color(0, 0, 255), 50);
  strip.show();
  

  //tcs code
  float red, green, blue;
  tcs.setInterrupt(false);
  delay(60);  // takes 50ms to read
  tcs.getRGB(&red, &green, &blue);
  tcs.setInterrupt(true);

  lcd.setCursor(0, 1);
  lcd.print("r: ");
  lcd.print(int(red));
  lcd.print("g: ");
  lcd.print(int(green));
  lcd.print("b: ");
  lcd.print(int(blue));
  delay(3000);

  strip.clear();
  colorWipe(strip.Color(255, 0, 0), 50);
  strip.show();

  tcs.setInterrupt(false);
  delay(60);  // takes 50ms to read
  tcs.getRGB(&red, &green, &blue);
  tcs.setInterrupt(true);

  lcd.setCursor(0, 1);
  lcd.print("r: ");
  lcd.print(int(red));
  lcd.print("g: ");
  lcd.print(int(green));
  lcd.print("b: ");
  lcd.print(int(blue));


  delay(3000);
}
//for neopixel
void colorWipe(uint32_t color, int wait) {
  for(int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
    strip.show();
    delay(wait);
  }
}