Objective
Confirm that the TSL2591 light sensor and LED work, choose an LED current, gain, and integration time that give a strong, non-saturated signal, and set dark and blank references so OD readings are correct.
Product
TSL2591 High Dynamic Range Digital Light Sensor (Adafruit breakout) + ~600 nm LED + Arduino
Documentation
- TSL2591 datasheet: https://look.ams-osram.com/m/c901de8e97608f8/original/TSL2591-DS000338.pdf
- Adafruit wiring & test: https://learn.adafruit.com/adafruit-tsl2591/wiring-and-test
- Adafruit library: https://github.com/adafruit/Adafruit_TSL2591_Library
Overview
Calibration is done by adjusting BOTH the hardware (resistor sets LED current) and the software (gain and integration time in the Arduino code).
- Read CH0 (visible + IR), not CH1 (IR only)
- ADC can be read at either 100ms integration time or 200+ms. This can be adjusted through gain, as it amplifies the readout signal
- LED and sensor position must be fixed during all tests
Procedure
Material Prep
- Arduino, TSL2591 breakout, ~600 nm LED, breadboard, jumper wires
- Various resistors (e.g. 1 kΩ, 680 Ω, 470 Ω, 330 Ω, 220 Ω)
- Translucent material (e.g. tape) and opaque material (e.g. foil)
Hardware Setup
- TSL2591 ↔ Arduino
- LED circuit: Arduino D7 → resistor → LED (+ to resistor side) → GND
- Position the LED directly facing the TSL2591 with a gap between them
- Install Adafruit TSL2591 Library and Adafruit Unified Sensor in Arduino IDE
- Connect Arduino to laptop, flash v1
Code
v1: Instantanous Readout
Serial Monitor at 9600 baud. Type 1 for LED ON, 0 for LED OFF.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#define LED_PIN 7
#define SAT_COUNT 36863 // 100 ms; use 65535 for 200 ms or longer
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
if (!tsl.begin()) { Serial.println("TSL2591 not found"); while (1); }
tsl.setGain(TSL2591_GAIN_MED); // LOW, MED, HIGH, MAX
tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // 100MS ... 600MS
Serial.println("Send 1 = LED ON, 0 = LED OFF");
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if (c == '1') digitalWrite(LED_PIN, HIGH);
if (c == '0') digitalWrite(LED_PIN, LOW);
}
uint16_t ch0 = tsl.getFullLuminosity() & 0xFFFF; // CH0 only
Serial.print("LED "); Serial.print(digitalRead(LED_PIN) ? "ON" : "OFF");
Serial.print(" CH0: "); Serial.print(ch0);
if (ch0 >= SAT_COUNT) Serial.print(" SATURATED");
Serial.println();
}
v2: Averaged Values for Calibration
Averages 10 readings per line and prints mean, standard deviation, and OD as CSV. Commands: 1 / 0 = LED ON / OFF, d = capture dark (LED off), b = capture blank (LED on). OD prints nan until both are captured.
#include <Wire.h>
#include <math.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#define LED_PIN 7
#define GAIN TSL2591_GAIN_MED
#define INTEGRATION TSL2591_INTEGRATIONTIME_100MS
#define SAT_COUNT 36863 // 100 ms; use 65535 for 200 ms or longer
#define N_SAMPLES 10
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
float darkVal = NAN, blankVal = NAN;
void measure(float &mean, float &sd, bool &sat) {
float sum = 0, sumsq = 0;
sat = false;
for (int i = 0; i < N_SAMPLES; i++) {
float ch0 = tsl.getFullLuminosity() & 0xFFFF; // CH0 only
if (ch0 >= SAT_COUNT) sat = true;
sum += ch0; sumsq += ch0 * ch0;
}
mean = sum / N_SAMPLES;
float var = (sumsq - N_SAMPLES * mean * mean) / (N_SAMPLES - 1);
sd = var > 0 ? sqrt(var) : 0;
}
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
if (!tsl.begin()) { Serial.println("TSL2591 not found"); while (1); }
tsl.setGain(GAIN);
tsl.setTiming(INTEGRATION);
Serial.println("1/0 = LED on/off, d = dark, b = blank");
Serial.println("time_s,led,CH0_mean,CH0_SD,sat,OD");
}
void loop() {
float mean, sd; bool sat;
if (Serial.available()) {
char c = Serial.read();
if (c == '1') digitalWrite(LED_PIN, HIGH);
if (c == '0') digitalWrite(LED_PIN, LOW);
if (c == 'd') { // dark: LED off
digitalWrite(LED_PIN, LOW); delay(500);
measure(mean, sd, sat); darkVal = mean;
Serial.print("# DARK "); Serial.println(darkVal, 1);
digitalWrite(LED_PIN, HIGH);
}
if (c == 'b') { // blank: LED on
digitalWrite(LED_PIN, HIGH); delay(500);
measure(mean, sd, sat); blankVal = mean;
Serial.print("# BLANK "); Serial.println(blankVal, 1);
}
}
measure(mean, sd, sat);
// OD = -log10((I_sample - I_dark) / (I_blank - I_dark))
float od = NAN;
if (!isnan(darkVal) && !isnan(blankVal) && blankVal > darkVal) {
float ratio = (mean - darkVal) / (blankVal - darkVal);
if (ratio > 0) od = -log10(ratio);
}
Serial.print(millis() / 1000.0, 1); Serial.print(",");
Serial.print(digitalRead(LED_PIN) ? "ON" : "OFF"); Serial.print(",");
Serial.print(mean, 1); Serial.print(",");
Serial.print(sd, 1); Serial.print(",");
Serial.print(sat ? "SAT" : "ok"); Serial.print(",");
Serial.println(od, 4);
}
Sensor Test (LED Off)
Flash v1. Settings: 100 ms integration, medium gain, CH0.
- Keep the LED OFF and record CH0 in room light
- Cover the sensor with your hand or place it in darkness: CH0 should decrease significantly
- Shine room light / phone flashlight at it: CH0 should increase
Dark → low ADC
Room light → higher ADC
Bright light → much higher ADC
Verify a drastic difference in CH0 is observed. This directly tells us if the sensor is working.
LED Test
LED OFF → low ADC
LED ON → much higher ADC
Light Blocking Test
Keep LED, sensor, gain, and integration time fixed. LED ON.
No blockage → highest ADC
Translucent → medium ADC
Opaque → near the LED OFF reading
This confirms that less light reaching the sensor gives a lower reading, which is what the OD calculation uses.
LED Current Test
LED current is set by the resistor (Ohm’s law):
The LED’s rated current is not known, so step the resistor down gradually and watch how much brighter the LED looks and how much CH0 changes.
- Start with a large resistor, LED ON, record CH0
- Swap to the next smaller resistor and repeat
- Stop when the LED ON vs LED OFF difference is sufficently large and CH0 is below saturation
Operator notes: The LED rated current and voltage were unknown. With ~470 Ω the difference between dim and bright was small. Stepping the resistor down gave a much larger difference. Settled on ~250 Ω. Since this setup has already verified the sensor to work and produced a large range of values, integration time does not need to be adjusted.
Dark And Blank (OD Check)
Flash v2 with the chosen GAIN, INTEGRATION, and SAT_COUNT.
- Cover the sensor from room light, send
dto capture dark (LED off) - Leave the LED and sensor in their fixed positions with nothing in the path, send
bto capture blank (LED on) - OD should read ~0 with nothing in the path
- Place translucent material in the path: OD should be above 0 and increase with more layers
- Place opaque material in the path: OD should be very high or
nan(reading at or below dark)