Skip to content

Commit dc4b3b8

Browse files
committed
Added 9876x gain usage
Increased usable range by including the 9876x gain setting. My bathroom, with the lights off, is ~4 mLux apparently. I didn't think this high gain setting would be needed, but it gets pretty dark at night!
1 parent 7d8e332 commit dc4b3b8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

TSL2591_AutoAdjust.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ void configureSensor(int choice)
6767
tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
6868
tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time
6969
hysteresis = 1.1;
70-
} // The device's gain can go up to 9876, but measurements with that setting seem unreliable, and there doesn't seem to be a need anyway.
70+
} else if (choice == 4) {
71+
tsl.setGain(TSL2591_GAIN_MAX); // 9876x gain
72+
tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time
73+
hysteresis = 1.4;
74+
}
7175
garbage = true; // The next measurement will be bad, and should be thrown out.
7276
}
7377

@@ -111,12 +115,14 @@ bool advancedRead(void)
111115
double newLux = tsl.calculateLux((lum & 0xFFFF), (lum >> 16));
112116

113117
// Interpret the measurement and decide if the gain should be adjusted
114-
if (newLux == 0 || newLux > 60000) { // The sensor saturated or is close
118+
if (newLux == 0 || newLux > 60000) { // The sensor saturated (leading to a returned zero) or is close
115119
if (settingsCounter > 0) {
116120
newSetting = settingsCounter - 1; // Decrease the gain
117121
}
118122
} else if (newLux > 1700 * hysteresis) {
119123
newSetting = 1; // Use low gain setting
124+
} else if (newLux < 5 * hysteresis) {
125+
newSetting = 4; // Highest gain setting
120126
} else if (newLux < 100 * hysteresis) {
121127
newSetting = 3; // Use high gain setting
122128
} else {
@@ -157,7 +163,7 @@ void loop(void)
157163
Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
158164
Serial.print("IR: "); Serial.print(ir); Serial.print(" ");
159165
Serial.print("Visible: "); Serial.print(visible); Serial.print(" ");
160-
Serial.print("Lux: "); Serial.print(lux); Serial.print(" ");
166+
Serial.print("Lux: "); Serial.print(lux, 4); Serial.print(" "); // Print lux to 4 decimal places
161167
Serial.print("Gain Config: "); Serial.println(settingsCounter);
162168
}
163169
}

0 commit comments

Comments
 (0)