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 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("------------------------------------"); }
/* 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"); }
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);
Text editor powered by tinymce.