SOP for Calibration and Setup of TSL2591 OD Sensor

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

Overview

Calibration is done by adjusting BOTH the hardware (resistor sets LED current) and the software (gain and integration time in the Arduino code).

Procedure

Material Prep


Hardware Setup

  1. TSL2591 ↔ Arduino
    1. VIN: 5V
    2. GND: GND
    3. SDA: SDA
    4. SCL: SCL
  2. LED circuit: Arduino D7 → resistor → LED (+ to resistor side) → GND
    1. Start with the largest resistor
    2. D7 is the pin used in the code
  3. Position the LED directly facing the TSL2591 with a gap between them
  4. Install Adafruit TSL2591 Library and Adafruit Unified Sensor in Arduino IDE
  5. 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.

  1. Keep the LED OFF and record CH0 in room light
  2. Cover the sensor with your hand or place it in darkness: CH0 should decrease significantly
  3. 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

  1. Start with a large resistor
  2. LED OFF (0): record CH0
  3. LED ON (1): record CH0
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):

Iled=Vsource−VLEDRI_{led} = \frac {V_{source} - V_{LED}} {R} R=Vsource−VLEDILEDR = \frac {V_{source} - V_{LED}} {I_{LED}}

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.

  1. Start with a large resistor, LED ON, record CH0
  2. Swap to the next smaller resistor and repeat
  3. 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.

  1. Cover the sensor from room light, send d to capture dark (LED off)
  2. Leave the LED and sensor in their fixed positions with nothing in the path, send b to capture blank (LED on)
  3. OD should read ~0 with nothing in the path
  4. Place translucent material in the path: OD should be above 0 and increase with more layers
  5. Place opaque material in the path: OD should be very high or nan (reading at or below dark)

OD is calculated as:

OD=−log10(Isample−IdarkIblank−Idark)OD = -log_{10} ( \frac {I_{sample} - I_{dark}} {I_{blank} - I_{dark}})
🥼

Operator notes: For this test trial, I_dark read CH0 = 2, I_bright read CH0 = 478, and I_sample read CH0 = 415