Adafruit_TSL2591_Library icon indicating copy to clipboard operation
Adafruit_TSL2591_Library copied to clipboard

Fix possible division by zero

Open Hoiss opened this issue 3 years ago • 7 comments

Adafruit_TSL2591::calculateLux function: It's dangerous in case ch0 is 0! In case it is 0 use the next greater number 1 for the formula, which should least falsify the calculated result.

Hoiss avatar Jul 27 '21 21:07 Hoiss

I got a lot inf in late night, this fix is necessary.

DeqingSun avatar Nov 06 '23 15:11 DeqingSun

Recreated issue using library example: https://github.com/adafruit/Adafruit_TSL2591_Library/blob/master/examples/tsl2591/tsl2591.ino and setting gain and integration time to lowest settings.

In dim light with finger over sensor: Screenshot from 2023-11-07 08-18-59

caternuson avatar Nov 07 '23 17:11 caternuson

Just to point out, the lux calculation for this sensor is a point of on going discussion: https://github.com/adafruit/Adafruit_TSL2591_Library/issues/14 with the current assessment that it should be taken as a nominal lux value.

Would a better behavior here be just to return 0 for lux if the ch0 value is 0 (or below some low threshold)?

caternuson avatar Nov 07 '23 17:11 caternuson

It also looks like #42 is an attempt to fix this same behavior.

caternuson avatar Nov 07 '23 18:11 caternuson

Ha, some progress after +2years! Thx @DeqingSun

@caternuson It's....not.....the most frequent discussion in #14 :-D I think the result is that the current implementation is not good but there is no better one. This is not a good thing.

Hoiss avatar Nov 07 '23 20:11 Hoiss

OK, we can still improve the current low light behavior. I'd suggest checking the values and returning 0 instead of altering the value so it's not dividing by zero.

caternuson avatar Nov 07 '23 22:11 caternuson

No progress here?

tbattisti avatar Jul 29 '24 21:07 tbattisti