Home STM32 STM32 Nucleo and TCS34725 Color Sensor Arduino example

STM32 Nucleo and TCS34725 Color Sensor Arduino example

by shedboy71

OK we are now going to look at an TCS34725 attached to the STM32 Nucleo and all development will be done on the Arduino IDE – Install STM32 support in the Arduino IDE

The TCS34725 device provides a digital return of red, green, blue (RGB), and clear light sensing values. An IR blocking filter, integrated on-chip and localized to the color sensing photodiodes, minimizes the IR spectral component of the incoming light and allows color measurements to be made accurately.

The high sensitivity, wide dynamic range, and IR blocking filter make the TCS34725 an ideal color sensor solution for use under varying lighting conditions and through attenuating materials. This data is transferred via an I2C to the host.

Connection and Layout

This example is for a STM32 Nucleo connected to the module

stm32 and TCS34725

stm32 and TCS34725

 

Code

We will use the adafruit library – https://github.com/adafruit/Adafruit_TCS34725

This is one of the default example

[codesyntax lang=”cpp”]

#include <Wire.h>
#include "Adafruit_TCS34725.h"

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

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

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

// Now we're ready to get readings!
}

void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;

tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);

Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
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(" ");
}

[/codesyntax]

 

Output

Open the serial monitor, this is what you should see

Color Temp: 2110 K – Lux: 30 – R: 94 G: 57 B: 40 C: 158
Color Temp: 2110 K – Lux: 30 – R: 94 G: 57 B: 40 C: 158
Color Temp: 1908 K – Lux: 421 – R: 1111 G: 657 B: 348 C: 2096
Color Temp: 1909 K – Lux: 3238 – R: 7594 G: 4566 B: 2054 C: 14420
Color Temp: 1990 K – Lux: 34 – R: 112 G: 66 B: 45 C: 194
Color Temp: 1952 K – Lux: 27 – R: 95 G: 55 B: 39 C: 160
Color Temp: 1833 K – Lux: 27 – R: 104 G: 58 B: 41 C: 172
Color Temp: 1776 K – Lux: 195 – R: 651 G: 359 B: 218 C: 1194
Color Temp: 1981 K – Lux: 280 – R: 761 G: 458 B: 267 C: 1458
Color Temp: 1870 K – Lux: 100 – R: 665 G: 287 B: 187 C: 1082
Color Temp: 1681 K – Lux: 141 – R: 777 G: 354 B: 225 C: 1299
Color Temp: 1621 K – Lux: 202 – R: 885 G: 434 B: 267 C: 1530
Color Temp: 1795 K – Lux: 25 – R: 100 G: 55 B: 39 C: 165

 

Links

TCS34725 RGB Light Color Sensor Recognition Module

You may also like