Discrepancy in measurements using the adafruit vs control everything library and code for the TSL2561

Hello particle community.
I am having a tough time verifying the validity of the lux measurements obtained from my TSL2561 lux sensor.
When i use the adafruit code and library i obtain measurements that are, in general, half the values i measure using the control everything’s code and library.

Here is the code from adafruit:

 //#include <Adafruit_TSL2561_U.h>


/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
   which provides a common 'type' for sensor data and some helper functions.
   
   To use this driver you will also need to download the Adafruit_Sensor
   library and include it in your libraries folder.

   You should also assign a unique ID to this sensor for use with
   the Adafruit Sensor API so that you can identify this particular
   sensor in any data logs, etc.  To assign a unique ID, simply
   provide an appropriate value in the constructor below (12345
   is used by default in this example).
   
   Connections
   ===========
   Connect SCL to analog 5
   Connect SDA to analog 4
   Connect VDD to 3.3V DC
   Connect GROUND to common ground

   I2C Address
   ===========
   The address will be different depending on whether you leave
   the ADDR pin floating (addr 0x39), or tie it to ground or vcc. 
   The default addess is 0x39, which assumes the ADDR pin is floating
   (not connected to anything).  If you set the ADDR pin high
   or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW
   (0x29) respectively.
    
   History
   =======
   2013/JAN/31  - First version (KTOWN)
*/
   
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 0x39);

/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
    sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  //tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  //tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  //tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  //tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}

/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Light Sensor Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!tsl.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
  else if(tsl.begin()){
  /* Display some basic information on this sensor */
  displaySensorDetails();
  
  /* Setup the sensor gain and integration time */
  configureSensor();
  
  /* We're ready to go! */
  Serial.println("Ready");
  }
  
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void) 
{  
  /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux");
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }
  delay(250);
}



####################################################
####################################################
####################################################



This is control everything's code from github:

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// TSL2561
// This code is designed to work with the TSL2561_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Light?sku=TSL2561_I2CS#tabs-0-product_tabset-2

#include<Wire.h>

// TSL2561 I2C address is 0x39(57)
#define Addr 0x39

void setup()
{
  // Initialise I2C communication as MASTER 
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Starts I2C communication
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x00 | 0x80);
  // Power ON mode
  Wire.write(0x03);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Starts I2C communication
  Wire.beginTransmission(Addr);
  // Select timing register
  Wire.write(0x01 | 0x80);
  // Nominal integration time = 402ms
  Wire.write(0x02);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
  
}

void loop()
{ 
  unsigned int data[4];
  for(int i = 0; i < 4; i++) // *****why i=4??
  {
    // Starts I2C communication
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((140 + i));
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);
    
    // Read 1 bytes of data
    if(Wire.available() == 1)
    {
      data[i] = Wire.read();
     }
     delay(200);
  }
  
  // Convert the data
  double ch0 = ((data[1] & 0xFF) * 256) + (data[0] & 0xFF);
  double ch1 = ((data[3] & 0xFF) * 256) + (data[2] & 0xFF);

  // Output data to serial monitor
  Serial.print("Full Spectrum(IR + Visible) :");
  Serial.println(ch0);
  Serial.print("Infrared Value :");
  Serial.println(ch1);
  Serial.print("Visible Value :");
  Serial.println(ch0-ch1);
}

When i tried changing the gain and the integration time using adafruit’s code my sensor measures an overload condition. It only works when the gain is set to automatic and when the integration time is 13ms.
Also the TSL2561 I purchased is not the adafruit brand. I am not sure if this is the reason why the readings are ‘incorrect’ when compared to the readings using CE’s code. Although this should not impact the accuracy of the measurements. Has anyone else experienced this issue ?

I first became aware of the ControlEverything-Lib through your thread and I will test it with my Adafruit sensors.

My experience so far: in the summer I had even written an article about the simple use of this breakout board, but when I wanted to run the Lib example then again in the fall to run, I also had when executing the identical Example code a "sensor Overload ". The example ran smoothly with the replacement of the sensor.@Alli

There is another Particle-Lib available - did you test this also?

1 Like

@Postler
Good day

Please let me know what your findings are. I am sure the sensor is operating correctly becuase when I switch between the 2 libraries the values are consist for each library on each try. Its only when I compare the measurements using the two different libraries do I end up with 2 completely different readings. Usually the Control Everything version measures double the lux that the Adafruit . Are you using and original adafruit TSL2561 or sparkfun TSL2561 or another generic TSL2561?

No, I was not away. Thank you I will give it a look!

Here my measurements, @Alli:

Case: light, bright room at 12 o´clock, prepared with beer and coffee.

Test1: using Adafruit TSL2561 ("A") with Photon (v0.7.0) + Adafruit_TSL2561_U (v2.0.10)
13 ms: Sensor overload
101 ms: Sensor overload
402 ms: Sensor overload

Test2: "A" + CE-Lib
Sensor works and bring these values up:
Full Spec: 55040
Infra Spec: 56322
Visible Spec: -1282

Values do not seem to be plausible: the sensor seems to be defective, maybe I had supplied this time from 5V.

Test3: Using Adafruit TSL2561 ("B") with Photon (v0.7.0) + Adafruit_TSL2561_U (v2.0.10)
13 ms: 128 constant series lux
101 ms: 134 constant series lux
402 ms: round about 130 lux after a while

Test4: "B" + CE-Lib
Full Spec: 472 constant
Infra Spec: 174 constant
Visible Spec: 294 constant

Test5: Using Arduino Uno with "A"
Cant compile direct the Adafruit_TSL2561_U-Example so I switch to

Test6: Sparkfun-Lib with "A": got an I2C error.
Test7: Sparkfun-Lib with "B": works as expected
data0: 200
data1: 82
lux: 33

There is the Groove Lib and others for testing. That's not worth it to me.
Forgot the milk for coffee.

@Postler
Thank you Postler. As you can swe from tests 3 and 4 there’s quite a significant discrepancy between measurements when using the two different libraries inspite of the fact that they are measuring light intensity under the exact same conditions!
I have not got around to trying out the library you mentioned in an earlier post. I will try that and compare results to see if it corresponds with either the control everything or adafruit library measurements to determine which of the two measurements are accurate.

I see 2 options,@Alli:
a.) There is another Lib in which changes have been added to the Adafruit-Lib
b.) to seek confrontation in the Adafruit Forum, because it is not a problem with Particle Devices.

@Postler
Agreed. Thanks I will follow it up on both fronts.

@Postler
The other library you refer to, Adafruit_TSL2561_Photon, does not compile on particle ide using the example included in the library. This is the error message i get

adafruit_tls2561_u.ino:1:51: Adafruit_TSL2561_U/Adafruit_TSL2561_U.h: No such file or directory

Did you experience the same problem?

First in my mind: shorten the linked file or update your firmware - which version is on your Photon?

@Postler
I don’t think the photon’s firm ware is an issue since the programme doesn’t compile in the Web ide. Did you use this program without changing it in anyway and did it compile successfully ?

I can follow and checked the available Libs, @Alli:
lib-check
The third lib not compiles.

@Postler
Only the tsl2561 library does not compile ?
The “Adafruit_TSL2561_Photon” library compiles using the demo example it comes with ?

@Alli: First Adafruit_TSL2561_U - v2010 works
Second tsl2561 - v121 works and
Third Adafruit_TSL2561_Photon - v319 cant compile.

@Postler
I am having the same issue. I decided to just use the ControlEverything-Lib and calibrate the results according to an established Light intensity sensor.