Code: Select all
// --------------------------------------------------------------------------------
// Displays visible light and lux levels.
//
//
// ******************************************************
// Designed for the Adafruit TSL2561 Digital Light Sensor,
// http://www.adafruit.com/products/439
// 4-channel I2C Safe Bi-directional Logic Level Converter,
// http://www.adafruit.com/products/757
// 16×2 LCD,
// http://www.adafruit.com/products/181
// and LCD I2C Backpack
// http://www.adafruit.com/products/292
// ******************************************************
//
//
// --------------------------------------------------------------------------------
// Dependencies
// --------------------------------------------------------------------------------
// Adafruit Industries's Liquid Crystal library:
// https://github.com/adafruit/LiquidCrystal
// Adafruit Industries's TSL2561 library:
// https://github.com/adafruit/TSL2561-Arduino-Library
// --------------------------------------------------------------------------------
// Libraries
#include "Wire.h"
#include "LiquidCrystal.h"
#include "TSL2561.h"
// LCD Constants
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(1, 2, 3, 4, 5, 6, 7);
// 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
//tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);
//! Light sensor configured to default i2c address.
TSL2561 tsl(TSL2561_ADDR_HIGH);
//! Buffer for printing light readings to the LCD.
char output_buffer[6];
//! Initialize the sketch.
void setup() {
// Initialize the serial connection for debugging.
Serial.begin(9600);
Serial.println(F("Serial initialized"));
// Initialize the LCD.
// uncomment the following if desired to control LED background light
// set pim 13 as LED background
// pinMode(13, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
// Print the top line label.
lcd.setCursor(0, 0);
lcd.print(F("Vis:"));
// Print the bottom line label.
lcd.setCursor(0, 1);
lcd.print(F("Lux:"));
// Initialize the light sensor.
if (tsl.begin())
{
Serial.println(F("Sensor initialized"));
}
else
{
Serial.println(F("Sensor failed to initialize"));
}
// Measuring relatively bright light, so gain of 0 is reasonable
tsl.setGain(TSL2561_GAIN_0X);
// Measuring relatively bright light, so short sampling time is reasonable
tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS);
// For low light level, comment out the above configuration and uncomment the below lines
// tsl.setGain(TSL2561_GAIN_16X);
// tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS);
}
//! Main execution loop.
void loop() {
// From the Adafruit TSL2561 Example sketch, get both IR and
// full spectrum values in one read.
uint32_t full_luminosity = tsl.getFullLuminosity();
uint16_t ir_spectrum = full_luminosity >> 16;
uint16_t full_spectrum = full_luminosity & 0xFFFF;
// Set the cursor to the data column on the top line.
lcd.setCursor(5, 0);
// Format the output using a format string stored in program memory.
snprintf_P(output_buffer, 6, PSTR("%5d"), (full_spectrum - ir_spectrum));
// Print to the LCD.
lcd.print(output_buffer);
// Set the cursor to the data column on the bottom line.
lcd.setCursor(5, 1);
// Format the output using a format string stored in program memory.
snprintf_P(output_buffer, 6, PSTR("%5d"), tsl.calculateLux(full_spectrum, ir_spectrum));
// Print to the LCD.
lcd.print(output_buffer);
// 0.5 second delay to keep the display from being unreadable in edge conditions.
delay(500);
}