/* TSL2591 Digital Light Sensor /
/ Dynamic Range: 600M:1 /
/ Maximum Lux: 88K */
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// Example for demonstrating the TSL2591 library - public domain!
// connect SCL to I2C Clock
// connect SDA to I2C Data
// connect Vin to 3.3-5V DC
// connect GROUND to common ground
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
#define SSD1306_LCDHEIGHT 64
//
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
//
//
/*
Configures the gain and integration time for the TSL2591
*/
//
void configureSensor(void)
{
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
//tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
}
//
/*
Program entry point for the Arduino sketch
*/
//
void setup(void)
{
Serial.begin(9600);
Serial.println(F("Starting Adafruit TSL2591 Test!"));
if (tsl.begin())
{
Serial.println(F("Found a TSL2591 sensor"));
}
else
{
Serial.println(F("No sensor found ... check your wiring?"));
while (1);
}
/* Configure the sensor */
configureSensor();
// Now we're ready to get readings ... move on to loop()!
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
// display.display();
// Clear the buffer.
display.clearDisplay();
}
//
/*
Show how to read IR and Full Spectrum at once and convert to lux
*/
//
void advancedRead(void)
{
// More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
// That way you can do whatever math and comparisons you want!
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full, vis;
float ndvi;
ir = lum >> 16;
full = lum & 0xFFFF;
vis = (full - ir);
ndvi = (1.*ir-1.*vis)/(1.*ir+1.*vis);
// Clear the buffer.
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("All: "); display.println(full);
display.setTextColor(BLACK,WHITE);
display.print("Vis: "); display.println(vis);
display.print("Nir: "); display.println(ir);
display.print("NDVI:"); display.println(ndvi);
display.display();
Serial.print(full); Serial.print(F(" ")); Serial.print(vis); Serial.print(F(" ")); Serial.print(ir); Serial.print(F(" ")); Serial.println(ndvi);
}
//
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
//
void loop(void)
{
if( tsl2591.getBrightness() < 1234 ) { // enter your threshold in LUX
digitalWrite( ledPin, HIGH ) ;
}
else {
digitalWrite( ledPin, LOW) ;
}
advancedRead();
delay(500);
}