Adafruit TSL2561 Light Sensor Library
Adafruit_TSL2561_U.h
Go to the documentation of this file.
1 
21 #ifndef ADAFRUIT_TSL2561_H_
22 #define ADAFRUIT_TSL2561_H_
23 
24 #include <Adafruit_Sensor.h>
25 #include <Arduino.h>
26 #include <Wire.h>
27 
28 #define TSL2561_VISIBLE 2
29 #define TSL2561_INFRARED 1
30 #define TSL2561_FULLSPECTRUM 0
31 
32 // I2C address options
33 #define TSL2561_ADDR_LOW (0x29)
34 #define TSL2561_ADDR_FLOAT (0x39)
35 #define TSL2561_ADDR_HIGH (0x49)
36 
37 // Lux calculations differ slightly for CS package
38 //#define TSL2561_PACKAGE_CS ///< Chip scale package
39 #define TSL2561_PACKAGE_T_FN_CL
40 
41 #define TSL2561_COMMAND_BIT (0x80)
42 #define TSL2561_CLEAR_BIT \
43  (0x40)
44 #define TSL2561_WORD_BIT (0x20)
45 #define TSL2561_BLOCK_BIT (0x10)
46 
47 #define TSL2561_CONTROL_POWERON (0x03)
48 #define TSL2561_CONTROL_POWEROFF \
49  (0x00)
50 
51 #define TSL2561_LUX_LUXSCALE (14)
52 #define TSL2561_LUX_RATIOSCALE (9)
53 #define TSL2561_LUX_CHSCALE (10)
54 #define TSL2561_LUX_CHSCALE_TINT0 (0x7517)
55 #define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7)
56 
57 // T, FN and CL package values
58 #define TSL2561_LUX_K1T (0x0040)
59 #define TSL2561_LUX_B1T (0x01f2)
60 #define TSL2561_LUX_M1T (0x01be)
61 #define TSL2561_LUX_K2T (0x0080)
62 #define TSL2561_LUX_B2T (0x0214)
63 #define TSL2561_LUX_M2T (0x02d1)
64 #define TSL2561_LUX_K3T (0x00c0)
65 #define TSL2561_LUX_B3T (0x023f)
66 #define TSL2561_LUX_M3T (0x037b)
67 #define TSL2561_LUX_K4T (0x0100)
68 #define TSL2561_LUX_B4T (0x0270)
69 #define TSL2561_LUX_M4T (0x03fe)
70 #define TSL2561_LUX_K5T (0x0138)
71 #define TSL2561_LUX_B5T (0x016f)
72 #define TSL2561_LUX_M5T (0x01fc)
73 #define TSL2561_LUX_K6T (0x019a)
74 #define TSL2561_LUX_B6T (0x00d2)
75 #define TSL2561_LUX_M6T (0x00fb)
76 #define TSL2561_LUX_K7T (0x029a)
77 #define TSL2561_LUX_B7T (0x0018)
78 #define TSL2561_LUX_M7T (0x0012)
79 #define TSL2561_LUX_K8T (0x029a)
80 #define TSL2561_LUX_B8T (0x0000)
81 #define TSL2561_LUX_M8T (0x0000)
82 
83 // CS package values
84 #define TSL2561_LUX_K1C (0x0043)
85 #define TSL2561_LUX_B1C (0x0204)
86 #define TSL2561_LUX_M1C (0x01ad)
87 #define TSL2561_LUX_K2C (0x0085)
88 #define TSL2561_LUX_B2C (0x0228)
89 #define TSL2561_LUX_M2C (0x02c1)
90 #define TSL2561_LUX_K3C (0x00c8)
91 #define TSL2561_LUX_B3C (0x0253)
92 #define TSL2561_LUX_M3C (0x0363)
93 #define TSL2561_LUX_K4C (0x010a)
94 #define TSL2561_LUX_B4C (0x0282)
95 #define TSL2561_LUX_M4C (0x03df)
96 #define TSL2561_LUX_K5C (0x014d)
97 #define TSL2561_LUX_B5C (0x0177)
98 #define TSL2561_LUX_M5C (0x01dd)
99 #define TSL2561_LUX_K6C (0x019a)
100 #define TSL2561_LUX_B6C (0x0101)
101 #define TSL2561_LUX_M6C (0x0127)
102 #define TSL2561_LUX_K7C (0x029a)
103 #define TSL2561_LUX_B7C (0x0037)
104 #define TSL2561_LUX_M7C (0x002b)
105 #define TSL2561_LUX_K8C (0x029a)
106 #define TSL2561_LUX_B8C (0x0000)
107 #define TSL2561_LUX_M8C (0x0000)
108 
109 // Auto-gain thresholds
110 #define TSL2561_AGC_THI_13MS (4850)
111 #define TSL2561_AGC_TLO_13MS (100)
112 #define TSL2561_AGC_THI_101MS (36000)
113 #define TSL2561_AGC_TLO_101MS (200)
114 #define TSL2561_AGC_THI_402MS (63000)
115 #define TSL2561_AGC_TLO_402MS (500)
116 
117 // Clipping thresholds
118 #define TSL2561_CLIPPING_13MS \
119  (4900)
120 #define TSL2561_CLIPPING_101MS \
121  (37000)
122 #define TSL2561_CLIPPING_402MS \
123  (65000)
124 
125 // Delay for integration times
126 #define TSL2561_DELAY_INTTIME_13MS (15)
127 #define TSL2561_DELAY_INTTIME_101MS (120)
128 #define TSL2561_DELAY_INTTIME_402MS (450)
129 
130 
131 enum {
132  TSL2561_REGISTER_CONTROL = 0x00, // Control/power register
133  TSL2561_REGISTER_TIMING = 0x01, // Set integration time register
134  TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02, // Interrupt low threshold low-byte
135  TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03, // Interrupt low threshold high-byte
136  TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04, // Interrupt high threshold low-byte
137  TSL2561_REGISTER_THRESHHOLDH_HIGH =
138  0x05, // Interrupt high threshold high-byte
139  TSL2561_REGISTER_INTERRUPT = 0x06, // Interrupt settings
140  TSL2561_REGISTER_CRC = 0x08, // Factory use only
141  TSL2561_REGISTER_ID = 0x0A, // TSL2561 identification setting
142  TSL2561_REGISTER_CHAN0_LOW = 0x0C, // Light data channel 0, low byte
143  TSL2561_REGISTER_CHAN0_HIGH = 0x0D, // Light data channel 0, high byte
144  TSL2561_REGISTER_CHAN1_LOW = 0x0E, // Light data channel 1, low byte
145  TSL2561_REGISTER_CHAN1_HIGH = 0x0F // Light data channel 1, high byte
146 };
147 
149 typedef enum {
150  TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms
151  TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms
152  TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms
154 
156 typedef enum {
157  TSL2561_GAIN_1X = 0x00, // No gain
158  TSL2561_GAIN_16X = 0x10, // 16x gain
159 } tsl2561Gain_t;
160 
161 /**************************************************************************/
166 /**************************************************************************/
167 class Adafruit_TSL2561_Unified : public Adafruit_Sensor {
168 public:
169  Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1);
170  boolean begin(void);
171  boolean begin(TwoWire *theWire);
172  boolean init();
173 
174  /* TSL2561 Functions */
175  void enableAutoRange(bool enable);
177  void setGain(tsl2561Gain_t gain);
178  void getLuminosity(uint16_t *broadband, uint16_t *ir);
179  uint32_t calculateLux(uint16_t broadband, uint16_t ir);
180 
181  /* Unified Sensor API Functions */
182  bool getEvent(sensors_event_t *);
183  void getSensor(sensor_t *);
184 
185 private:
186  TwoWire *_i2c;
187 
188  int8_t _addr;
189  boolean _tsl2561Initialised;
190  boolean _tsl2561AutoGain;
191  tsl2561IntegrationTime_t _tsl2561IntegrationTime;
192  tsl2561Gain_t _tsl2561Gain;
193  int32_t _tsl2561SensorID;
194 
195  void enable(void);
196  void disable(void);
197  void write8(uint8_t reg, uint8_t value);
198  uint8_t read8(uint8_t reg);
199  uint16_t read16(uint8_t reg);
200  void getData(uint16_t *broadband, uint16_t *ir);
201 };
202 
203 #endif // ADAFRUIT_TSL2561_H
uint32_t calculateLux(uint16_t broadband, uint16_t ir)
Converts the raw sensor values to the standard SI lux equivalent.
Definition: Adafruit_TSL2561_U.cpp:338
void enableAutoRange(bool enable)
Enables or disables the auto-gain settings when reading data from the sensor.
Definition: Adafruit_TSL2561_U.cpp:130
void setGain(tsl2561Gain_t gain)
Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
Definition: Adafruit_TSL2561_U.cpp:166
void getLuminosity(uint16_t *broadband, uint16_t *ir)
Gets the broadband (mixed lighting) and IR only values from the TSL2561, adjusting gain if auto-gain ...
Definition: Adafruit_TSL2561_U.cpp:194
tsl2561IntegrationTime_t
Definition: Adafruit_TSL2561_U.h:149
void getSensor(sensor_t *)
Gets the sensor_t data.
Definition: Adafruit_TSL2561_U.cpp:504
tsl2561Gain_t
Definition: Adafruit_TSL2561_U.h:156
Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID=-1)
Constructor.
Definition: Adafruit_TSL2561_U.cpp:56
boolean begin(void)
Initializes I2C and configures the sensor with default Wire I2C (call this function before doing anyt...
Definition: Adafruit_TSL2561_U.cpp:77
boolean init()
Initializes I2C connection and settings. Attempts to determine if the sensor is contactable, then sets up a default integration time and gain. Then powers down the chip.
Definition: Adafruit_TSL2561_U.cpp:105
void setIntegrationTime(tsl2561IntegrationTime_t time)
Sets the integration time for the TSL2561. Higher time means more light captured (better for low ligh...
Definition: Adafruit_TSL2561_U.cpp:142
bool getEvent(sensors_event_t *)
Gets the most recent sensor event.
Definition: Adafruit_TSL2561_U.cpp:476
Class that stores state and functions for interacting with TSL2561 Light Sensor.
Definition: Adafruit_TSL2561_U.h:167