This shows you the differences between two versions of the page.
| en:iot-open:practical:hardware:rtu:esp32:scenarios:tcs [2025/07/31 18:04] – created kivilands6 | en:iot-open:practical:hardware:rtu:esp32:scenarios:tcs [2025/10/28 17:47] (current) – [Example] raivo.sell | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== ESP32 TCS34725 ===== | ||
| + | This is an example of the TCS34725 reading rgb color code from neopixel, and displaying it on lcd | ||
| + | ===== Prequisits ==== | ||
| + | < | ||
| + | lib_deps = adafruit/ | ||
| + | </ | ||
| + | ===== Example ==== | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | /* NEOPIXEL */ | ||
| + | void colorWipe(uint32_t color, int wait); | ||
| + | #define LED_PIN 12 | ||
| + | #define LED_COUNT 8 | ||
| + | Adafruit_NeoPixel strip(LED_COUNT, | ||
| + | |||
| + | #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, | ||
| + | |||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(152000); | ||
| + | |||
| + | // Initialize I2C on custom pins | ||
| + | Wire.begin(SDA_PIN, | ||
| + | |||
| + | // Initialize LCD | ||
| + | if (!lcd.begin(16, | ||
| + | Serial.println(" | ||
| + | while (1); | ||
| + | } | ||
| + | Serial.println(" | ||
| + | lcd.print(" | ||
| + | |||
| + | strip.begin(); | ||
| + | strip.show(); | ||
| + | |||
| + | /* TCS34725 setup */ | ||
| + | if (tcs.begin()) { | ||
| + | // | ||
| + | } else { | ||
| + | Serial.println(" | ||
| + | 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; | ||
| + | } | ||
| + | // | ||
| + | } | ||
| + | } | ||
| + | < | ||
| + | back-ref-format : none | ||
| + | </ | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | strip.clear(); | ||
| + | colorWipe(strip.Color(0, | ||
| + | strip.show(); | ||
| + | | ||
| + | |||
| + | //tcs code | ||
| + | float red, green, blue; | ||
| + | tcs.setInterrupt(false); | ||
| + | delay(60); | ||
| + | tcs.getRGB(& | ||
| + | tcs.setInterrupt(true); | ||
| + | |||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | lcd.print(int(red)); | ||
| + | lcd.print(" | ||
| + | lcd.print(int(green)); | ||
| + | lcd.print(" | ||
| + | lcd.print(int(blue)); | ||
| + | delay(3000); | ||
| + | |||
| + | strip.clear(); | ||
| + | colorWipe(strip.Color(255, | ||
| + | strip.show(); | ||
| + | |||
| + | tcs.setInterrupt(false); | ||
| + | delay(60); | ||
| + | tcs.getRGB(& | ||
| + | tcs.setInterrupt(true); | ||
| + | |||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | lcd.print(int(red)); | ||
| + | lcd.print(" | ||
| + | lcd.print(int(green)); | ||
| + | lcd.print(" | ||
| + | lcd.print(int(blue)); | ||
| + | |||
| + | |||
| + | delay(3000); | ||
| + | } | ||
| + | //for neopixel | ||
| + | void colorWipe(uint32_t color, int wait) { | ||
| + | for(int i = 0; i < strip.numPixels(); | ||
| + | strip.setPixelColor(i, | ||
| + | strip.show(); | ||
| + | delay(wait); | ||
| + | } | ||
| + | } | ||
| + | </ | ||