├── library.properties ├── README.txt ├── .github ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE.md ├── examples └── tsl2561 │ └── tsl2561.ino ├── TSL2561.h └── TSL2561.cpp /library.properties: -------------------------------------------------------------------------------- 1 | name=TSL2561 Arduino Library 2 | version=1.0.0 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for using the TSL2561 Luminosity sensor 6 | paragraph=Arduino library for using the TSL2561 Luminosity sensor 7 | category=Sensors 8 | url=https://github.com/adafruit/TSL2561-Arduino-Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | **DEPRECATED** 2 | Please use new version of library: 3 | https://github.com/adafruit/Adafruit_TSL2561 4 | 5 | --------------------------- 6 | This is an Arduino library for the TSL2561 digital luminosity (light) sensors. 7 | 8 | Pick one up at http://www.adafruit.com/products/439 9 | 10 | To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder TSL2561. Check that the TSL2561 folder contains TSL2561.cpp and TSL2561.h 11 | 12 | Place the TSL2561 library folder your /libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. 13 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /examples/tsl2561/tsl2561.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "TSL2561.h" 3 | 4 | // Example for demonstrating the TSL2561 library - public domain! 5 | 6 | // connect SCL to analog 5 7 | // connect SDA to analog 4 8 | // connect VDD to 3.3V DC 9 | // connect GROUND to common ground 10 | // ADDR can be connected to ground, or vdd or left floating to change the i2c address 11 | 12 | // The address will be different depending on whether you let 13 | // the ADDR pin float (addr 0x39), or tie it to ground or vcc. In those cases 14 | // use TSL2561_ADDR_LOW (0x29) or TSL2561_ADDR_HIGH (0x49) respectively 15 | TSL2561 tsl(TSL2561_ADDR_FLOAT); 16 | 17 | void setup(void) { 18 | Serial.begin(9600); 19 | 20 | if (tsl.begin()) { 21 | Serial.println("Found sensor"); 22 | } else { 23 | Serial.println("No sensor?"); 24 | while (1); 25 | } 26 | 27 | // You can change the gain on the fly, to adapt to brighter/dimmer light situations 28 | //tsl.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations) 29 | tsl.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations) 30 | 31 | // Changing the integration time gives you a longer time over which to sense light 32 | // longer timelines are slower, but are good in very low light situtations! 33 | tsl.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light) 34 | //tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium light) 35 | //tsl.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim light) 36 | 37 | // Now we're ready to get readings! 38 | } 39 | 40 | void loop(void) { 41 | // Simple data read example. Just read the infrared, fullspecrtrum diode 42 | // or 'visible' (difference between the two) channels. 43 | // This can take 13-402 milliseconds! Uncomment whichever of the following you want to read 44 | uint16_t x = tsl.getLuminosity(TSL2561_VISIBLE); 45 | //uint16_t x = tsl.getLuminosity(TSL2561_FULLSPECTRUM); 46 | //uint16_t x = tsl.getLuminosity(TSL2561_INFRARED); 47 | 48 | Serial.println(x, DEC); 49 | 50 | // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum 51 | // That way you can do whatever math and comparisons you want! 52 | uint32_t lum = tsl.getFullLuminosity(); 53 | uint16_t ir, full; 54 | ir = lum >> 16; 55 | full = lum & 0xFFFF; 56 | Serial.print("IR: "); Serial.print(ir); Serial.print("\t\t"); 57 | Serial.print("Full: "); Serial.print(full); Serial.print("\t"); 58 | Serial.print("Visible: "); Serial.print(full - ir); Serial.print("\t"); 59 | 60 | Serial.print("Lux: "); Serial.println(tsl.calculateLux(full, ir)); 61 | 62 | delay(100); 63 | } 64 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /TSL2561.h: -------------------------------------------------------------------------------- 1 | 2 | /**************************************************************************/ 3 | /*! 4 | @file tsl2561.h 5 | @author K. Townsend (microBuilder.eu) 6 | 7 | @section LICENSE 8 | 9 | Software License Agreement (BSD License) 10 | 11 | Copyright (c) 2010, microBuilder SARL 12 | All rights reserved. 13 | 14 | Redistribution and use in source and binary forms, with or without 15 | modification, are permitted provided that the following conditions are met: 16 | 1. Redistributions of source code must retain the above copyright 17 | notice, this list of conditions and the following disclaimer. 18 | 2. Redistributions in binary form must reproduce the above copyright 19 | notice, this list of conditions and the following disclaimer in the 20 | documentation and/or other materials provided with the distribution. 21 | 3. Neither the name of the copyright holders nor the 22 | names of its contributors may be used to endorse or promote products 23 | derived from this software without specific prior written permission. 24 | 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 26 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 29 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 32 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 | */ 36 | /**************************************************************************/ 37 | 38 | #ifndef _TSL2561_H_ 39 | #define _TSL2561_H_ 40 | 41 | #if ARDUINO >= 100 42 | #include 43 | #else 44 | #include 45 | #endif 46 | #include 47 | 48 | #define TSL2561_VISIBLE 2 // channel 0 - channel 1 49 | #define TSL2561_INFRARED 1 // channel 1 50 | #define TSL2561_FULLSPECTRUM 0 // channel 0 51 | 52 | // 3 i2c address options! 53 | #define TSL2561_ADDR_LOW 0x29 54 | #define TSL2561_ADDR_FLOAT 0x39 55 | #define TSL2561_ADDR_HIGH 0x49 56 | 57 | // Lux calculations differ slightly for CS package 58 | //#define TSL2561_PACKAGE_CS 59 | #define TSL2561_PACKAGE_T_FN_CL 60 | 61 | #define TSL2561_READBIT (0x01) 62 | 63 | #define TSL2561_COMMAND_BIT (0x80) // Must be 1 64 | #define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear) 65 | #define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte) 66 | #define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write 67 | 68 | #define TSL2561_CONTROL_POWERON (0x03) 69 | #define TSL2561_CONTROL_POWEROFF (0x00) 70 | 71 | #define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14 72 | #define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9 73 | #define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10 74 | #define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE 75 | #define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE 76 | 77 | // T, FN and CL package values 78 | #define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE 79 | #define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE 80 | #define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE 81 | #define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE 82 | #define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE 83 | #define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE 84 | #define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE 85 | #define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE 86 | #define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE 87 | #define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE 88 | #define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE 89 | #define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE 90 | #define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE 91 | #define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE 92 | #define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE 93 | #define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE 94 | #define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE 95 | #define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE 96 | #define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE 97 | #define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE 98 | #define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE 99 | #define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE 100 | #define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE 101 | #define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE 102 | 103 | // CS package values 104 | #define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE 105 | #define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE 106 | #define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE 107 | #define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE 108 | #define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE 109 | #define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE 110 | #define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE 111 | #define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE 112 | #define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE 113 | #define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE 114 | #define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE 115 | #define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE 116 | #define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE 117 | #define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE 118 | #define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE 119 | #define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE 120 | #define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE 121 | #define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE 122 | #define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE 123 | #define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE 124 | #define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE 125 | #define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE 126 | #define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE 127 | #define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE 128 | 129 | enum 130 | { 131 | TSL2561_REGISTER_CONTROL = 0x00, 132 | TSL2561_REGISTER_TIMING = 0x01, 133 | TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02, 134 | TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03, 135 | TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04, 136 | TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05, 137 | TSL2561_REGISTER_INTERRUPT = 0x06, 138 | TSL2561_REGISTER_CRC = 0x08, 139 | TSL2561_REGISTER_ID = 0x0A, 140 | TSL2561_REGISTER_CHAN0_LOW = 0x0C, 141 | TSL2561_REGISTER_CHAN0_HIGH = 0x0D, 142 | TSL2561_REGISTER_CHAN1_LOW = 0x0E, 143 | TSL2561_REGISTER_CHAN1_HIGH = 0x0F 144 | }; 145 | 146 | typedef enum 147 | { 148 | TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms 149 | TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms 150 | TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms 151 | } 152 | tsl2561IntegrationTime_t; 153 | 154 | typedef enum 155 | { 156 | TSL2561_GAIN_0X = 0x00, // No gain 157 | TSL2561_GAIN_16X = 0x10, // 16x gain 158 | } 159 | tsl2561Gain_t; 160 | 161 | 162 | class TSL2561 { 163 | public: 164 | TSL2561(uint8_t addr); 165 | boolean begin(void); 166 | void enable(void); 167 | void disable(void); 168 | void write8(uint8_t r, uint8_t v); 169 | uint16_t read16(uint8_t reg); 170 | 171 | uint32_t calculateLux(uint16_t ch0, uint16_t ch1); 172 | void setTiming(tsl2561IntegrationTime_t integration); 173 | void setGain(tsl2561Gain_t gain); 174 | uint16_t getLuminosity (uint8_t channel); 175 | uint32_t getFullLuminosity (); 176 | 177 | private: 178 | int8_t _addr; 179 | tsl2561IntegrationTime_t _integration; 180 | tsl2561Gain_t _gain; 181 | 182 | boolean _initialized; 183 | }; 184 | #endif 185 | -------------------------------------------------------------------------------- /TSL2561.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file tsl2561.c 4 | @author K. Townsend (microBuilder.eu / adafruit.com) 5 | 6 | @section LICENSE 7 | 8 | Software License Agreement (BSD License) 9 | 10 | Copyright (c) 2010, microBuilder SARL, Adafruit Industries 11 | All rights reserved. 12 | 13 | Redistribution and use in source and binary forms, with or without 14 | modification, are permitted provided that the following conditions are met: 15 | 1. Redistributions of source code must retain the above copyright 16 | notice, this list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 3. Neither the name of the copyright holders nor the 21 | names of its contributors may be used to endorse or promote products 22 | derived from this software without specific prior written permission. 23 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | /**************************************************************************/ 36 | 37 | #if defined ( ESP8266 ) 38 | #include 39 | #else 40 | #include 41 | #include 42 | #endif 43 | #include 44 | 45 | #include "TSL2561.h" 46 | 47 | TSL2561::TSL2561(uint8_t addr) { 48 | _addr = addr; 49 | _initialized = false; 50 | _integration = TSL2561_INTEGRATIONTIME_13MS; 51 | _gain = TSL2561_GAIN_16X; 52 | 53 | // we cant do wire initialization till later, because we havent loaded Wire yet 54 | } 55 | 56 | boolean TSL2561::begin(void) { 57 | Wire.begin(); 58 | 59 | // Initialise I2C 60 | Wire.beginTransmission(_addr); 61 | #if ARDUINO >= 100 62 | Wire.write(TSL2561_REGISTER_ID); 63 | #else 64 | Wire.send(TSL2561_REGISTER_ID); 65 | #endif 66 | Wire.endTransmission(); 67 | Wire.requestFrom(_addr, 1); 68 | #if ARDUINO >= 100 69 | int x = Wire.read(); 70 | #else 71 | int x = Wire.receive(); 72 | #endif 73 | //Serial.print("0x"); Serial.println(x, HEX); 74 | if (x & 0x0A ) { 75 | //Serial.println("Found TSL2561"); 76 | } else { 77 | return false; 78 | } 79 | _initialized = true; 80 | 81 | // Set default integration time and gain 82 | setTiming(_integration); 83 | setGain(_gain); 84 | // Note: by default, the device is in power down mode on bootup 85 | disable(); 86 | 87 | return true; 88 | } 89 | 90 | void TSL2561::enable(void) 91 | { 92 | if (!_initialized) begin(); 93 | 94 | // Enable the device by setting the control bit to 0x03 95 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); 96 | } 97 | 98 | void TSL2561::disable(void) 99 | { 100 | if (!_initialized) begin(); 101 | 102 | // Disable the device by setting the control bit to 0x03 103 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); 104 | } 105 | 106 | 107 | void TSL2561::setGain(tsl2561Gain_t gain) { 108 | if (!_initialized) begin(); 109 | 110 | enable(); 111 | _gain = gain; 112 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); 113 | disable(); 114 | } 115 | 116 | void TSL2561::setTiming(tsl2561IntegrationTime_t integration) 117 | { 118 | if (!_initialized) begin(); 119 | 120 | enable(); 121 | _integration = integration; 122 | write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); 123 | disable(); 124 | } 125 | 126 | uint32_t TSL2561::calculateLux(uint16_t ch0, uint16_t ch1) 127 | { 128 | unsigned long chScale; 129 | unsigned long channel1; 130 | unsigned long channel0; 131 | 132 | switch (_integration) 133 | { 134 | case TSL2561_INTEGRATIONTIME_13MS: 135 | chScale = TSL2561_LUX_CHSCALE_TINT0; 136 | break; 137 | case TSL2561_INTEGRATIONTIME_101MS: 138 | chScale = TSL2561_LUX_CHSCALE_TINT1; 139 | break; 140 | default: // No scaling ... integration time = 402ms 141 | chScale = (1 << TSL2561_LUX_CHSCALE); 142 | break; 143 | } 144 | 145 | // Scale for gain (1x or 16x) 146 | if (!_gain) chScale = chScale << 4; 147 | 148 | // scale the channel values 149 | channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE; 150 | channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE; 151 | 152 | // find the ratio of the channel values (Channel1/Channel0) 153 | unsigned long ratio1 = 0; 154 | if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0; 155 | 156 | // round the ratio value 157 | unsigned long ratio = (ratio1 + 1) >> 1; 158 | 159 | unsigned int b, m; 160 | 161 | #ifdef TSL2561_PACKAGE_CS 162 | if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) 163 | {b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;} 164 | else if (ratio <= TSL2561_LUX_K2C) 165 | {b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;} 166 | else if (ratio <= TSL2561_LUX_K3C) 167 | {b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;} 168 | else if (ratio <= TSL2561_LUX_K4C) 169 | {b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;} 170 | else if (ratio <= TSL2561_LUX_K5C) 171 | {b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;} 172 | else if (ratio <= TSL2561_LUX_K6C) 173 | {b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;} 174 | else if (ratio <= TSL2561_LUX_K7C) 175 | {b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;} 176 | else if (ratio > TSL2561_LUX_K8C) 177 | {b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;} 178 | #else 179 | if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) 180 | {b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} 181 | else if (ratio <= TSL2561_LUX_K2T) 182 | {b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} 183 | else if (ratio <= TSL2561_LUX_K3T) 184 | {b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} 185 | else if (ratio <= TSL2561_LUX_K4T) 186 | {b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} 187 | else if (ratio <= TSL2561_LUX_K5T) 188 | {b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} 189 | else if (ratio <= TSL2561_LUX_K6T) 190 | {b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} 191 | else if (ratio <= TSL2561_LUX_K7T) 192 | {b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} 193 | else if (ratio > TSL2561_LUX_K8T) 194 | {b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} 195 | #endif 196 | 197 | unsigned long temp; 198 | temp = ((channel0 * b) - (channel1 * m)); 199 | 200 | // do not allow negative lux value 201 | if (temp < 0) temp = 0; 202 | 203 | // round lsb (2^(LUX_SCALE-1)) 204 | temp += (1 << (TSL2561_LUX_LUXSCALE-1)); 205 | 206 | // strip off fractional portion 207 | uint32_t lux = temp >> TSL2561_LUX_LUXSCALE; 208 | 209 | // Signal I2C had no errors 210 | return lux; 211 | } 212 | 213 | uint32_t TSL2561::getFullLuminosity (void) 214 | { 215 | if (!_initialized) begin(); 216 | 217 | // Enable the device by setting the control bit to 0x03 218 | enable(); 219 | 220 | // Wait x ms for ADC to complete 221 | switch (_integration) 222 | { 223 | case TSL2561_INTEGRATIONTIME_13MS: 224 | delay(14); 225 | break; 226 | case TSL2561_INTEGRATIONTIME_101MS: 227 | delay(102); 228 | break; 229 | default: 230 | delay(403); 231 | break; 232 | } 233 | 234 | uint32_t x; 235 | x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); 236 | x <<= 16; 237 | x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); 238 | 239 | disable(); 240 | 241 | return x; 242 | } 243 | uint16_t TSL2561::getLuminosity (uint8_t channel) { 244 | 245 | uint32_t x = getFullLuminosity(); 246 | 247 | if (channel == 0) { 248 | // Reads two byte value from channel 0 (visible + infrared) 249 | return (x & 0xFFFF); 250 | } else if (channel == 1) { 251 | // Reads two byte value from channel 1 (infrared) 252 | return (x >> 16); 253 | } else if (channel == 2) { 254 | // Reads all and subtracts out just the visible! 255 | return ( (x & 0xFFFF) - (x >> 16)); 256 | } 257 | 258 | // unknown channel! 259 | return 0; 260 | } 261 | 262 | 263 | uint16_t TSL2561::read16(uint8_t reg) 264 | { 265 | uint16_t x; uint16_t t; 266 | 267 | Wire.beginTransmission(_addr); 268 | #if ARDUINO >= 100 269 | Wire.write(reg); 270 | #else 271 | Wire.send(reg); 272 | #endif 273 | Wire.endTransmission(); 274 | 275 | Wire.requestFrom(_addr, 2); 276 | #if ARDUINO >= 100 277 | t = Wire.read(); 278 | x = Wire.read(); 279 | #else 280 | t = Wire.receive(); 281 | x = Wire.receive(); 282 | #endif 283 | x <<= 8; 284 | x |= t; 285 | return x; 286 | } 287 | 288 | 289 | 290 | void TSL2561::write8 (uint8_t reg, uint8_t value) 291 | { 292 | Wire.beginTransmission(_addr); 293 | #if ARDUINO >= 100 294 | Wire.write(reg); 295 | Wire.write(value); 296 | #else 297 | Wire.send(reg); 298 | Wire.send(value); 299 | #endif 300 | Wire.endTransmission(); 301 | } 302 | --------------------------------------------------------------------------------