adafruit / Adafruit_TSL2591_Library

This is an Arduino library for the TSL2591 digital luminosity (light) sensors.
58 stars 48 forks source link

Fix possible division by zero #45

Open Hoiss opened 3 years ago

Hoiss commented 3 years ago

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.

DeqingSun commented 10 months ago

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

caternuson commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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

Hoiss commented 10 months ago

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.

caternuson commented 10 months ago

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.

tbattisti commented 1 month ago

No progress here?