first of all thank you for this thread.
It helped me a lot getting started with my Pi and the TSL2561.
After I had several day-logs with a pattern Static referred to as "vampire fang" ;) I tried to solve the problem.
I don't know if you have fixed it already yourself - here is my approach:
I knew it had to be something with the lux calculation but after having logged the IR and ambient output which also made the curious "rapid drops" - it had to be the sensor itself.
So I read the datasheet again and again and I think I've found the problem:
First I understand that the calculation that is used only works if the Integration time is 402mSec - otherwise the result has to be scaled
As Static's sourcecode uses 101 mSec integration this would result in wrong lux values - so I switched to 402 mSec ( 0x12 #high gain 402mSec OR 0x02 #low gain 402 mSec ).
However, it would not result in "vampire fangs".
Second I learned that the caluculation does only work if the gain is 16x - again otherwise it has to be scaled
And I think thats the source of the vampire fangs: The moment the autorange function switches to 1x gain - the read IR/ambient values drop (rapidly) - and so does the resulting lux value...
So I guess the read*auto functions should multiply the read words with 16 (scaling) if it is using lowGain.
That way the values look much better.
I do not know if I am right - correct me elsewise. :wink:
FYI: The corresponding code in the datasheet is:
// first, scale the channel values depending on the gain and integration time
// 16X, 402mS is nominal. <--- !!!!!!
// scale if integration time is NOT 402 msec <--- !!!!!!
case 0: // 13.7 msec
chScale = CHSCALE_TINT0;
case 1: // 101 msec
chScale = CHSCALE_TINT1;
default: // assume no scaling
chScale = (1 << CH_SCALE);
// scale if gain is NOT 16X <--- !!!!!!
if (!iGain) chScale = chScale << 4; // scale 1X to 16X [huelke: shift 4 bits left=> multiply with 2**4 = 16]
// scale the channel values
channel0 = (ch0 * chScale) >> CH_SCALE;
channel1 = (ch1 * chScale) >> CH_SCALE;
I chose to deactivate autorange and ran a whole day on lowGain which produced a pretty pattern.
If you use autogain, you have to insure that you wait long enough after switching gain!
I almost got crazy because the lowGain * 16 values did not match the highGain values.
Problem was that I aquired the values too soon after switching the gain so the sensor could not complete its integration and returned wrong values.
Now - with enough "sleep" - lowGain and highGain return nearly the same lux calculation. :mrgreen: