Projeto 21
Sensor de luz
Projeto 21 - Sensor de luz
Código (download)
//Projeto 21 - Sensor de luz Adafruit TSL2591
#include <Wire.h>
#include <Adafruit_Sensor.h> //Pesquisar por Adafruit Unified Sensor
#include "Adafruit_TSL2591.h" //Pesquisar por tsl2591
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // Número para identificar o sensor
void displaySensorDetails() //Algumas informações sobre o sensor
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.print (F("Sensor: "));
Serial.println(sensor.name);
Serial.print (F("Driver Ver: "));
Serial.println(sensor.version);
Serial.print (F("Unique ID: "));
Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: "));
Serial.print(sensor.max_value);
Serial.println(F(" lux"));
Serial.print (F("Min Value: "));
Serial.print(sensor.min_value);
Serial.println(F(" lux"));
Serial.print (F("Resolution: "));
Serial.print(sensor.resolution, 4);
Serial.println(F(" lux"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
delay(500);
}
void configureSensor() // Configurar o ganho "gain" e o tempo de integração "integration time" para o sensor TSL2591
{ // Podemos alterar o "gain" para se adaptar a situações de luz mais brilhantes ou mais escuras
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (Muita luz)
tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain (Pouca luz)
// Alterar o tempo de integração "integration time" oferece mais tempo para o sensor medir a luz
// Maiores valores são úteis para situações de pouca luz
//tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // Valor pequeno (Muita luz)
// 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); // Valor grande (Pouca luz)
// Valores de ganho "gain" e de tempo de integração "integration time" de referência
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(""));
}
void setup()
{
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);
}
displaySensorDetails(); //Algumas informações sobre o sensor
configureSensor(); // Configuração do sensor
}
void simpleRead() //Shows how to perform a basic read on visible, full spectrum or infrared light (returns raw 16-bit ADC values)
{
// Simple data read example. Just read the infrared, fullspecrtrum diode
// or 'visible' (difference between the two) channels.
// This can take 100-600 milliseconds! Uncomment whichever of the following you want to read
uint16_t x = tsl.getLuminosity(TSL2591_VISIBLE);
//uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
//uint16_t x = tsl.getLuminosity(TSL2591_INFRARED);
Serial.print(F("[ "));
Serial.print(millis());
Serial.print(F(" ms ] "));
Serial.print(F("Luminosity: "));
Serial.println(x, DEC);
}
void advancedRead() //Show how to read IR and Full Spectrum at once and convert to lux
{
// 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;
ir = lum >> 16;
full = lum & 0xFFFF;
Serial.print(F("[ "));
Serial.print(millis());
Serial.print(F(" ms ] "));
Serial.print(F("IR: "));
Serial.print(ir);
Serial.print(F(" "));
Serial.print(F("Full: "));
Serial.print(full);
Serial.print(F(" "));
Serial.print(F("Visible: "));
Serial.print(full - ir);
Serial.print(F(" "));
Serial.print(F("Lux: "));
Serial.println(tsl.calculateLux(full, ir), 6);
}
void unifiedSensorAPIRead() //Performs a read using the Adafruit Unified Sensor API.
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
Serial.print(F("[ ")); Serial.print(event.timestamp); Serial.print(F(" ms ] "));
if ((event.light == 0) |
(event.light > 4294966000.0) |
(event.light <-4294966000.0))
{
/* If event.light = 0 lux the sensor is probably saturated */
/* and no reliable data could be generated! */
/* if event.light is +/- 4294967040 there was a float over/underflow */
Serial.println(F("Invalid data (adjust gain or timing)"));
}
else
{
Serial.print(event.light);
Serial.println(F(" lux"));
}
}
void loop()
{
//simpleRead();
advancedRead();
//unifiedSensorAPIRead();
delay(500);
}