0

TCS34725 odd Lux values
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

TCS34725 odd Lux values

by Systembolaget on Mon Aug 27, 2018 12:18 pm

Hej,

I'm scanning colour swatches using the sensor with a matt black 15 mm high shroud so as to block all ambient light ingress. I'm using the Euclidean distance method, rather than the common if-statement cascade, so even with some cold or warm light ingress, all 14 colours can be matched to the values in an array no problem, because the r, g and b ratio remains the same under quite different conditions. It really is a great sensor, near-same colours can be reliably discerned, like sample 3 and 4 or 12 and 13 or 20 shades of grey or pastels. So far, so good.

But the Lux (lx) values, at any combination of integration time and gain are odd, as shown in the image. I have the suspicion that calculating lx from r, g and b is not delivering values that make sense at all, considering the substrate is identical. Is there a quick-fix for that?

The reason is that I need the code to throw 1. "no sample" or 2. "colour not in series" messages. The 1. "no sample" issue I wanted to address via measuring Lux; with no sample present, the 400 - 500 lx ambient office/lab environment should have been sufficiently different, but samples 5 and 8 result in even lower lx figures, even lower than matt black.

Thanks in advance!

IMG_0431.JPG
Colour swatches
IMG_0431.JPG (204.77 KiB) Viewed 219 times

Systembolaget
 
Posts: 154
Joined: Wed Mar 08, 2017 1:01 pm

Re: TCS34725 odd Lux values

by adafruit_support_bill on Mon Aug 27, 2018 1:11 pm

Lux as it applies to color samples is somewhat complicated by the fact the luminous power is weighted differently across the visible spectrum. That said, your measurements seem to vary more than one would expect.

Are you using the lux calculation from the library, or another implementation?

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: TCS34725 odd Lux values

by Systembolaget on Mon Aug 27, 2018 2:11 pm

From the library sketch.

If you have an alternative suggestion how to trigger a "no sample" message, I could just as well skip lux. Maybe I can just use the Euclidean colour distance. Thanks to the sensor, it only varies between 42 and 174 across the entire range of swatches, including greys and pastels.

Each colour, once recognised, shall also trigger an electromechanical actuator, and I don't want it to "jerk around" when no swatch is over the sensor, or at least have it go to a "no sample" position, that's why I thought using lx was a good idea; clear poses the same problems, especially with greys.

Systembolaget
 
Posts: 154
Joined: Wed Mar 08, 2017 1:01 pm

Re: TCS34725 odd Lux values

by adafruit_support_bill on Mon Aug 27, 2018 2:35 pm

I think that the issue with lux is that the value is calculated as a float, then returned as an unsigned 16 bit integer. I suspect that there may be some integer overflow happening there.

If your 'no-sample' background is black, I would expect that the absolute value of the clear channel would a reliable indicator.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: TCS34725 odd Lux values

by adafruit_support_bill on Mon Aug 27, 2018 2:45 pm

Also - due to the weighting - the lux calculation can return negative numbers for readings heavy in the red or blue part of the spectrum. That would explain the extreme numbers seen in your read and purple patches.

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: TCS34725 odd Lux values

by Systembolaget on Mon Aug 27, 2018 6:28 pm

Thanks, yes, I had negative readings indeed at one point (using an int instead of a uint16_t).

In any case, the 1st issue is solved. I used the Euclidean colour distance (a threshold gleaned from the calibration readings plus some extra buffer) to toggle a flag when there is nothing in front of the sensor. Now I can find and sort very many colours (also pastel colours or muddy colours) reliably, compared to the "skittles sorters" one sees on YouTube and elsewhere. For other beginners, I leave the basic code here. Now I have to work out the 2nd issue ; )

Code: Select all | TOGGLE FULL SIZE
/******* Colour identifier and sorter *******/

/******* Adafruit Metro Mini, ams TCS34725 *******/

/******* LIBRARIES *******/

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

/******* VARIABLES *******/

const int SAMPLES[14][5] = { // Values from colour training (averaged raw r, g and b; actuator movement)
  {8771, 6557, 3427,  10},
  {7013, 2766, 1563,  20},
  {4092, 1118, 1142,  30},
  {4488, 1302, 1657,  40},
  {3009, 1846, 2235,  50},
  {7546, 6686, 6323,  60},
  {2650, 3139, 4116,  70},
  { 857,  965, 1113,  80},
  {2926, 5012, 5331,  90},
  { 964, 2014, 2418, 100},
  {1260, 2200, 1459, 110},
  {4784, 5898, 3138, 120},
  {5505, 5242, 2409, 130},
  {5406, 3893, 1912, 140}, // When adding more samples no particular order is required
};

bool sensorTriggered = false; // Sample present yes or no

byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]); // Determine number of samples in array

const byte pinLED = A0; // Connect ams TCS34725 breakout board LED pin to pin A0 on Adafruit Metro Mini

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); // Longer integration time for higher colour fidelity

/******* FUNCTIONS *******/

void setup(void) {

  Serial.begin(9600);

  pinMode (pinLED, OUTPUT);

} // End of setup

void loop(void) {

  digitalWrite(pinLED, HIGH); // Turn on-board LED on; set to LOW to turn off or when using alternative illumination

  uint16_t r, g, b, c;

  tcs.getRawData(&r, &g, &b, &c); // Change Adafruit_TCS34725.cpp line 223 to omit &c (only needed for colour temperature and Lux calculation)

  byte foundColour = findColour(r, g, b);

  if (sensorTriggered) {

    // Do something

  } else {

    // Do nothing, or something else

  }

} // End of loop

byte findColour(int r, int g, int b) {

  int distance = 10000; // Raw distance from white to black (change depending on selected integration time and gain)
  byte foundColour;

  for (byte i = 0; i < samplesCount; i++) {

    int temp = sqrt(pow(r - SAMPLES[i][0], 2) + pow(g - SAMPLES[i][1], 2) + pow(b - SAMPLES[i][2], 2)); // Calculate Euclidean distance between colours

    if (temp < distance) {

      distance = temp;

      foundColour = i;

    }

  }

  if (distance > 500) { // Threshold distance from SAMPLES calibration process

    sensorTriggered = false;

  } else {

    sensorTriggered = true;

  }

  return foundColour;

} // End of findColour

Systembolaget
 
Posts: 154
Joined: Wed Mar 08, 2017 1:01 pm

Re: TCS34725 odd Lux values

by adafruit_support_bill on Mon Aug 27, 2018 7:27 pm

Now I have to work out the 2nd issue ; )

Which is?

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: TCS34725 odd Lux values

by Systembolaget on Tue Aug 28, 2018 1:25 am

How to throw a message for a swatch that has no match in the array, a swatch that does not belong.

Systembolaget
 
Posts: 154
Joined: Wed Mar 08, 2017 1:01 pm

Re: TCS34725 odd Lux values

by adafruit_support_bill on Tue Aug 28, 2018 6:01 am

Initialize foundColor to an invalid value like -1. If your findColour() function returns a -1, emit the message.

Code: Select all | TOGGLE FULL SIZE
  byte foundColour = -1;

adafruit_support_bill
 
Posts: 74337
Joined: Sat Feb 07, 2009 10:11 am

Re: TCS34725 odd Lux values

by Systembolaget on Tue Aug 28, 2018 7:29 am

That's what I have not figured out yet; how to ensure that an "alien" sample that may have a colour distance in the range of all others can be shown the door, so to speak. Because even after very careful calibration with all "allowed" samples, the colour distance is within a range. Excluding "alien" samples by Lux (lx) value obviously does not work, as I found out yesterday.

I am getting a Vantablack-coated sensor shroud next month. Maybe that will change everything, because of an entirely new level of "allowed" swatch calibration accuracy.

Systembolaget
 
Posts: 154
Joined: Wed Mar 08, 2017 1:01 pm

Please be positive and constructive with your questions and comments.