Arduino Code

To use this sensor and calculate Lux, there's a lot of very hairy and unpleasant math. You can check out the math in the datasheet but really, its not intuitive or educational - its just how the sensor works. So we took care of all the icky math and wrapped it up into a nice Arduino library.

Install Adafruit_TSL2561 library

To begin reading sensor data, you will need to install the Adafruit_TSL2561 library (code on our github repository). It is available from the Arduino library manager so we recommend using that.

From the IDE open up the library manager...

And type in adafruit tsl2561 to locate the library. Click Install

Install Adafruit_Sensor

The TSL2561 library uses the Adafruit_Sensor support backend so that readings can be normalized between sensors.

Search the library manager for Adafruit Unified Sensor and install that too (you may have to scroll a bit)

Now you can run the File->Examples->Adafruit_TSL2561->sensorapi example program which will read and calculate the lux readings for you.

Open up the serial monitor at 9600 baud to see the measurements. Use a lamp or your hand to illuminate/shade the sensor to see the values change.

The library is fairly simple to use. The first line of code in the example is the 'constructor' where you can supply the I2C ADDR (in case you want to change it), and a unique ID to attach to this sensor (you can just leave this to the default value of 12345 for now). By modifying the I2C address we can have up to three TSL2561 sensors connected on the same board:
// The address will be different depending on whether you leave
// the ADDR pin float (addr 0x39), or tie it to ground or vcc. In those cases
// use TSL2561_ADDR_LOW (0x29) or TSL2561_ADDR_HIGH (0x49) respectively
Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);

Next up, you will want to configure the sensor with the gain and integration time.

You can have either a gain of 0 (no extra gain, good in low light situations) or a gain of 16 which will boost the light considerably in dim situations.

You can also change the integration time, which is how long it will collect light data for. The longer the integration time, the more precision the sensor has when collecting light samples.

New to v2.0 of the driver, there is also an auto-gain option that is useful when measuring in mixed lighting-situations. This will automatically enable or disable the gain depending on the light level. This is still an experimental feature and the trigger levels to switch may need to be tweaked, but this should be useful to collect light both indoors and outdoors without having to change the code yourself.

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoRange(true);          /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}
By default, the driver will return light in standard SI lux units, which are a result of some complex calculations based on both photo diodes on the TSL2561 (one for full spectrum and one for IR). The sensitivity of the two diodes can be seen in the chart below:

When you're ready to get your measurement in standard SI lux units, simply call getEvent with a reference to the 'sensors_event_t' object where the sensor data will be stored. This example assumes we are using the 'tsl' instance of Adafruit_TSL2561 at the top of the example code:
  /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux");
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }
This function will return a reading in SI lux units, which is probably the easiest unit to understand when working with light.

If you wish to manually read the individual photo diodes, though, you can still do this in the latest library by calling the getLuminosity function, and passing in two variables where the sensor data will be stored:
uint16_t broadband = 0;
uint16_t infrared = 0;

/* Populate broadband and infrared with the latest values */
getLuminosity (&broadband, &infrared);
That's it! The example should be easy to understand and work into your own projects from here!
This guide was first published on Jul 29, 2012. It was last updated on Nov 18, 2018. This page (Arduino Code) was last updated on Jan 19, 2018.