Adafruit MicroPython TSL2561 Library

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
mnvk52
Posts: 8
Joined: Sat Sep 05, 2020 5:31 pm

Adafruit MicroPython TSL2561 Library

Post by mnvk52 » Sun Sep 06, 2020 11:37 am

Hi,
I have an TSL2561 connected to an ESP32 with this library: https://github.com/adafruit/micropython ... it-tsl2561
Documentation: https://micropython-tsl2561.readthedocs ... index.html

I don't understand the output of the TSL2561 sensor. For example:

Code: Select all

import tsl2561
from machine import I2C, Pin
i2c = I2C(scl=Pin(22), sda=Pin(21))
sensor = tsl2561.TSL2561(i2c, address=41)

sensor.gain(1)
sensor.integration_time(13)
print(sensor.read())
print(sensor.read(autogain=True))
print(sensor.read(autogain=True, raw=True))
print()

sensor.gain(16)
sensor.integration_time(13)
print(sensor.read())
print(sensor.read(autogain=True))
print(sensor.read(autogain=True, raw=True))
print()

sensor.gain(1)
sensor.integration_time(402)
print(sensor.read())
print(sensor.read(autogain=True))
print(sensor.read(autogain=True, raw=True))
print()

sensor.gain(16)
sensor.integration_time(402)
print(sensor.read())
print(sensor.read(autogain=True))
print(sensor.read(autogain=True, raw=True))
That means I read the sensor with different settings of gain and integration time.

Code: Select all

%Run test.py
3.480131
2.995391
(90, 33)

2.995391
2.995391
(90, 33)

3.078674
3.09314
(2677, 964)

3.095333
3.09314
(2673, 962)
I get different values for the luminosity in lux (sensor.read()).
As luminosity is a physical quantity, shouldn't it be independent of the setting of gain and integration time?

There's another thing confusing me. The output of line 8 is 3.480131 and the output of line 15 is 2.995391. The gain in line 15 is 16 and 1 in line 8. I would expect the luminosity to be larger with higher gain. Am I wrong?

IHOXOHI
Posts: 100
Joined: Sat Apr 25, 2020 7:31 am

Re: Adafruit MicroPython TSL2561 Library

Post by IHOXOHI » Tue Sep 08, 2020 10:15 am

Hi mnvk52,

I'm not an expert but I think that 3.4 and 2.99 are closes values, especially for lux values wich could be upper than 40000 lux with a tsl2561, and maybe this difference is true because light is changing every time.

Otherwise, gain is used for have betters values for DIFFERENTS situations. So, it's normal that differents gains give you differents results in the same situation.

mnvk52
Posts: 8
Joined: Sat Sep 05, 2020 5:31 pm

Re: Adafruit MicroPython TSL2561 Library

Post by mnvk52 » Tue Sep 15, 2020 10:08 am

IHOXOHI wrote:
Tue Sep 08, 2020 10:15 am
especially for lux values wich could be upper than 40000 lux with a tsl2561
I usually don't put the sensor into direct sunlight. In my garden the value doesn't exceed 40 lx on sunny summer days - without direct sunlight. But shouldn't the value be between 10.000 and 25.000?
http://stjarnhimlen.se/comp/radfaq.html#10

If I put the sensor to direct sunlight it's saturated:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/read_tls2561.py", line 17, in <module>
  File "tsl2561.py", line 153, in read
  File "tsl2561.py", line 124, in _lux
ValueError: sensor saturated
It seems to me there's something wrong. If it's not exposed to direct sunlight the value is way too small and in the sunlight the sensor is saturated.

There's another strange thing. In this document there is the lux calculation on page 22
https://files.seeedstudio.com/wiki/Grov ... L2561T.pdf

I tried to verify the output of

Code: Select all

print(sensor.read())
it should show the calculated lux

The output of

Code: Select all

print(sensor.read(autogain=True, raw=True))
shows the raw data that is ch0 and ch1 (if I got everything right)

In my first post I had
lux = 3.480131 with (ch0 = 90, ch1 = 33),
lux = 2.995391 with (ch0 = 90, ch1 = 33),
lux = 3.078674 with (ch0 = 2677, ch1 = 964)
So let's say lux is around 3

Trying to verify these with this script based on the forumlas in the manual:

Code: Select all

def chipscale(ch0,ch1):
    x = ch0
    y = ch1
    z = ch1/ch0
    if 0 < z <= 0.52:
        Lux = 0.0315 * x - 0.0593 * x * (z ** 1.4)
    elif 0.52 < z <= 0.65:
        Lux = 0.0229 * x - 0.0291 * y
    elif 0.65 < z <= 0.8:
        Lux = 0.0157 * x - 0.018 * y
    elif 0.8 < z <= 1.3:
        Lux = 0.00338 * x - 0.0026 * y
    elif z > 1.3:
        Lux = 0  
    else:
        Lux = "error"
    
    print("Chipscale Package: CHO = " + str(x) + ", CH1 = " + str(y) + ", Lux = " + str(Lux))
        
    
def TMB(ch0,ch1):
    x = ch0
    y = ch1
    z = ch1/ch0
    if 0 < z <= 0.5:
        Lux = 0.0304 * x - 0.062 * x * (z ** 1.4)
    elif 0.5 < z <= 0.61:
        Lux = 0.0224 * x - 0.031 * y
    elif 0.61 < z <= 0.8:
        Lux = 0.0128 * x - 0.0153 * y
    elif 0.8 < z <= 1.3:
        Lux = 0.00146 * x - 0.00112 * y
    elif z > 1.3:
        Lux = 0  
    else:
        Lux = "error"
        
    print("TMB Package: CHO = " + str(x) + ", CH1 = " + str(y) + ", Lux = " + str(Lux))
    
    
chipscale(90, 33)
chipscale(2677, 964)
chipscale(2673, 962)
print()
TMB(90, 33)
TMB(2677, 964)
TMB(2673, 962)
the output is:

Code: Select all

Chipscale Package: CHO = 90, CH1 = 33, Lux = 1.524982173753135
Chipscale Package: CHO = 2677, CH1 = 964, Lux = 46.33253457980946
Chipscale Package: CHO = 2673, CH1 = 962, Lux = 46.29417621449765

TMB Package: CHO = 90, CH1 = 33, Lux = 1.366335493637342
TMB Package: CHO = 2677, CH1 = 964, Lux = 41.6579693751802
TMB Package: CHO = 2673, CH1 = 962, Lux = 41.62800143842924
My own calculation based on the manual says lux is either around 1.5 or around 45 - both is far away from 3

All that confuses me very much and I don't know to interpret the data coming from that sensor :?:

IHOXOHI
Posts: 100
Joined: Sat Apr 25, 2020 7:31 am

Re: Adafruit MicroPython TSL2561 Library

Post by IHOXOHI » Tue Sep 15, 2020 8:10 pm

Yep,

That's an interesting subject.
For light and many others physics parameters, it seems that there is a big difference between senses and physics values for historical reasons. (It's why we use logarithmics scales in many times. Strangely not for the light). An eye is a very good thing because it could works with low light and hight light, but there is a very big difference between them. For exemple a display recorder start from 0.0001 lux, and direct light from sun is between 50000 and 100000 lux. 1000000000 is the size of scale.
So, 3 and 40 are closes values for light, especially with, maybe, differents calculation's methods.
I have a tsl2591 wich works fine. I will test the precision and report.

IHOXOHI
Posts: 100
Joined: Sat Apr 25, 2020 7:31 am

Re: Adafruit MicroPython TSL2561 Library

Post by IHOXOHI » Tue Sep 15, 2020 9:57 pm

Yup,

With my tsl2591:
- in "normal use" (under my saloon usual light (40 lux)), it's easy to seen a difference of 10-200 (it depend of distance) lux with just a lighter light in addition. Gain is low.
- with just the light of display of lcd160 with angle, I can see a difference of 0.01 lux when I hide the sensor by my hand. Gain is max.

Otherwise, when I use the low gain in low light situation I have wrongs values.

So, it's important to have a gain in adequacy of light situation for precision measurements.

Post Reply