Git Product home page Git Product logo

adafruit_tsl2561's Introduction

Adafruit TSL2561 Light Sensor Driver Build StatusDocumentation

This driver is for the Adafruit TSL2561 Breakout, and is based on Adafruit's Unified Sensor Library (Adafruit_Sensor).

The driver supports manual or 'auto' gain. Adjusting the gain allows you to make the sensor more or less 'sensitive' to light (depending on if you are indoors or outdoors, for example):

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.enableAutoGain(true);          /* Auto-gain ... switches automatically between 1x and 16x */

The driver also supports as automatic clipping detection, and will return '65536' lux when the sensor is saturated and data is unreliable. tsl.getEvent will return false in case of saturation and true in case of valid light data.

About the TSL2561

The TSL2561 is a 16-bit digital (I2C) light sensor, with adjustable gain and 'integration time'.

Adjusting the 'integration time' essentially increases the resolution of the device, since the analog converter inside the chip has time to take more samples. The integration time can be set as follows:

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 */

One of the big advantages of the TSL2561 is that it is capable of measuring both broadband (visible plus infrared) and infrared light thanks to two distinct sensing units on the device. This is important in certain lighting environments to be able to read the light level reliably.

More information on the TSL2561 can be found in the datasheet: http://www.adafruit.com/datasheets/TSL2561.pdf

What is the Adafruit Unified Sensor Library?

The Adafruit Unified Sensor Library (Adafruit_Sensor) provides a common interface and data type for any supported sensor. It defines some basic information about the sensor (sensor limits, etc.), and returns standard SI units of a specific type and scale for each supported sensor type.

It provides a simple abstraction layer between your application and the actual sensor HW, allowing you to drop in any comparable sensor with only one or two lines of code to change in your project (essentially the constructor since the functions to read sensor data and get information about the sensor are defined in the base Adafruit_Sensor class).

This is imporant useful for two reasons:

1.) You can use the data right away because it's already converted to SI units that you understand and can compare, rather than meaningless values like 0..1023.

2.) Because SI units are standardised in the sensor library, you can also do quick sanity checks working with new sensors, or drop in any comparable sensor if you need better sensitivity or if a lower cost unit becomes available, etc.

Light sensors will always report units in lux, gyroscopes will always report units in rad/s, etc. ... freeing you up to focus on the data, rather than digging through the datasheet to understand what the sensor's raw numbers really mean.

About this Driver

Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit!

Written by Kevin (KTOWN) Townsend for Adafruit Industries.

adafruit_tsl2561's People

Contributors

beegee-tokyo avatar cujomalainey avatar driverblock avatar evaherrada avatar ladyada avatar maditnerd avatar microbuilder avatar mjs513 avatar mvn23 avatar sethtroisi avatar tdicola avatar theintel avatar tyeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_tsl2561's Issues

U8GILB compatibility wth TSL2561

Hi,

I've tried U8GILB with TSL2561 unified library with Arduino due and oled 0.96". It seems that after the first output ok it is not possible to refresh the screen and the output is ever 65536.
Removing U8GLIB instructions the sketch works well.
May someone help me about this issue ?

Which Adafruit TSL2561 library should I use?

I just noticed that Adafruit has a second library for using the TSL2561:
https://github.com/adafruit/TSL2561-Arduino-Library

The other library is clearly older (when you look at the commits), but neither library's readme mentions the other library, so I'm not sure which library I should use... The older library was the first Google hit for me, so other people might find that one first, too.

If the older library is no longer being developed, maybe a note should be added to its readme? I think I remember seeing "archived" repositories on GitHub, so that's a possibility, if the older library should no longer be used.

Is the older library deprecated? What are the differences between the libraries, is there some reason why I might want to use the older library instead of this one?

getting 65536.00 value from event.light

I'm building a room sensor with DHT22, a TSL2561, and an Adafruit PIR sensor (not yet hooked up). These are being plugged into a wemos d1 mini with wifiManager and MQTT libs included.

When I run the simple sensorapi application, unadjusted, I get a reasonable value for the lux reading.

When I run it with my fairly complicated code, I get 65536.

lux: 65536.00
broadband: 65535
infrared: 65535

I added the getLuminosity() call to see what they were showing. Here's the code that does the reading.

  /* Get a new sensor event */
  sensors_event_t event;
  tsl.getEvent(&event);

  tsl.getLuminosity(&broadband, &infrared);

  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light);
    Serial.print(" lux\t");

    Serial.print("broadband:\t");
    Serial.print(broadband);

    Serial.print("\tinfrared:\t");
    Serial.println(infrared);

  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }
  delay(250);

At the top of the sketch, I'm loading the following libs:

#include <FS.h> //this needs to be first, or it all crashes and burns...

#include <Wire.h>
#include <Adafruit_Sensor.h>  // Adafruit Unified Sensor Driver - https://github.com/adafruit/Adafruit_Sensor

#include <ESP8266WiFi.h>      // https://github.com/esp8266/Arduino
#include <WiFiManager.h>      // https://github.com/tzapu/WiFiManager
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <ArduinoJson.h>      //https://github.com/bblanchon/ArduinoJson

#include <PubSubClient.h>
#ifdef ESP32
#include <SPIFFS.h>
#endif

#include <Adafruit_TSL2561_U.h>
#include <DHT.h>

The compile results:

Sketch uses 385052 bytes (36%) of program storage space. Maximum is 1044464 bytes.
Global variables use 33352 bytes (40%) of dynamic memory, leaving 48568 bytes for local variables.
Maximum is 81920 bytes.

It seems to compile just fine. Perhaps there are some conflicts within these libs?

Thanks for any advice,
Chris.

Pgmspace warnings with Teensy's

Using Arduino IDE 1.8.4 with Teensyduino 1.39 beta1 (happens with other versions as well). Compiling for Teensy 3.5. When compiling receiving warnings on pgmspace macros being redefined. Example sketch does run no problem even with the warnings.

Recommend changing line 25 in the .cpp file to:

#elif !defined(TEENSYDUINO)

to avoid the unnecessary warnings. There maybe a better way but this worked for me.

Respectfully
Mike

Init() Fix #11 only half fixed the problem

In the fix for #11, c913785#diff-fb32052893c4375234c2bcd52e31e8edR110 if (x & 0xF0 != 0x10) { was added to ::Init()

This is equivalent to if (x & 1) {
I believe the desired code is to add more parenthesis, e.g. `if ((x & 0xF0) != 0x10) {

I tested with this simple program:

#include<iostream>
using namespace std;
int main() {
    for (int x = 0; x < 128; x++) {
        bool current = (x & 0xF0 != 0x10);
        bool test    = ((x & 0xF0) != 0x10);
        cout << x << " " << current << " " << test << endl;
    }
}

I'm not sure this actually fixes the problem because my TSL2561 (ordered in 2016) returns 0x0A similiar to @beegee-tokyo.

Questions about this new library version

Hi,

I'm updating my TSL2561 library on arduino IDE and notice the many changes and have some questions:

(1) - Is it still possible to get Channel 0 and channel 1 (Full light and IR light) datas instead of getting only the result (global Lux)? If yes, how to do that?
-Edit- I tried to adapt the example like this:

void loop(void) 
{  
   uint16_t broadband, infrared;
  /* 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.print(" lux\t");
    tsl.getLuminosity(&broadband, &infrared);
    Serial.print(broadband); Serial.print(" full\t");
    Serial.print(infrared); Serial.println(" Ir");
  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
       and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }
  delay(250);
  
}

Is It a right way? I'm not familiar with this event call. Will getluminosity() results corresponding to the event time? Or will it return before/after measurements?

(2) - How is the events part working? when I set the sensor integration time to 402ms, the event shouldn't comes before at least 402ms, but the results are coming right after the tsl.getEvent(&event); call

(3) - The disable() function that powering off the sensor is call without waiting the sensor event ?
(PS: In cpp enable() / disable() functions shouldn't be after "private" comment ? )
-Edit- OKay, there is a delay in getData function. So isn't it a "real" event work? (with timeout for example?)

(4) - Why is the I2C address set at declaration of the object and not at the begin() call like many other sensors library's? I would like to do some multiple sensors detection by scanning I2C bus and then begin each of them with their own address. But if the declaration already set the address I can't set it in the setup() section.

PS : Sorry for my poor English level.

Adafruit_TSL2561_Unified::init() return true even if no sensor is attached!

Adafruit_TSL2561_Unified::init() checks the value read from TSL2561_REGISTER_ID against !(x & 0x0A).
This is always true if there is no sensor is attached, because then the returned value is 0xFF.

According to the TSL2561 datasheet, the high 4 bits of TSL2561_REGISTER_ID should be
0000
0001
0100
0101
So there is always at least two 0's in it. An additional check of the returned value agains 0xFF could detect if there is no sensor attached.
e.g.:

boolean Adafruit_TSL2561_Unified::init()
{
  /* Make sure we're actually connected */
  uint8_t x = read8(TSL2561_REGISTER_ID);
  if (x == 0xFF) {
    return false;
  }
  if (!(x & 0x0A))
  {
    return false;
  }
  _tsl2561Initialised = true;

  /* Set default integration time and gain */
  setIntegrationTime(_tsl2561IntegrationTime);
  setGain(_tsl2561Gain);

  /* Note: by default, the device is in power down mode on bootup */
  disable();

  return true;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.