Skip to content

Commit 24a54ff

Browse files
committed
Fix bugs in the channel scale calculations.
I was dividing where I should have multipled, and vice versa. :(
1 parent 22bddfd commit 24a54ff

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Adafruit_TSL2561/Adafruit_TSL2561.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,22 @@ def lux(self):
249249
"""
250250
(chan0, chan1) = self._getData()
251251

252-
chscale = self._integrationTime.scale * self.gain
253-
schan0 = chan0 / chscale
254-
schan1 = chan1 / chscale
252+
# The channel readings are pretty raw numbers. Given a steady light
253+
# source, a (hypothetical) 10ms integration time would give a raw
254+
# reading that was ten times higher than a 1ms integration time.
255+
# Similarly, if the sensor is operating at 16x gain, the raw readings
256+
# will by 16 times higher than a 1x gain.
257+
#
258+
# The calculations in the datasheet are based on a 402ms integration
259+
# time and a 16x gain. If those aren't the parameters that were used
260+
# for this reading, we need to scale the raw numbers to their 402ms/16x
261+
# equivalents for purposes of doing the calculation. That's what the
262+
# `chscale` variable accomplishes. The integration time scaling is
263+
# precomputed. The gain scaling is simply based on the current gain
264+
# relative the the 16x reference.
265+
chscale = self._integrationTime.scale * (16.0 / self.gain)
266+
schan0 = chan0 * chscale
267+
schan1 = chan1 * chscale
255268

256269
if schan0 != 0:
257270
ratio = schan1 / schan0

0 commit comments

Comments
 (0)