Advertisement
baldengineer

TSL2591 Example with F()

Apr 14th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.04 KB | None | 0 0
  1. /* TSL2591 Digital Light Sensor */
  2. /* Dynamic Range: 600M:1 */
  3. /* Maximum Lux: 88K */
  4.  
  5. #include <Wire.h>
  6. #include <Adafruit_Sensor.h>
  7. #include "Adafruit_TSL2591.h"
  8.  
  9. // Example for demonstrating the TSL2591 library - public domain!
  10.  
  11. // connect SCL to analog 5
  12. // connect SDA to analog 4
  13. // connect Vin to 3.3-5V DC
  14. // connect GROUND to common ground
  15.  
  16. Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
  17.  
  18. /**************************************************************************/
  19. /*
  20.     Displays some basic information on this sensor from the unified
  21.     sensor API sensor_t type (see Adafruit_Sensor for more information)
  22. */
  23. /**************************************************************************/
  24. void displaySensorDetails(void)
  25. {
  26.   sensor_t sensor;
  27.   tsl.getSensor(&sensor);
  28.   Serial.println("------------------------------------");
  29.   Serial.print  (F("Sensor:       ")); Serial.println(sensor.name);
  30.   Serial.print  (F("Driver Ver:   ")); Serial.println(sensor.version);
  31.   Serial.print  (F("Unique ID:    ")); Serial.println(sensor.sensor_id);
  32.   Serial.print  (F("Max Value:    ")); Serial.print(sensor.max_value); Serial.println(F(" lux"));
  33.   Serial.print  (F("Min Value:    ")); Serial.print(sensor.min_value); Serial.println(F(" lux"));
  34.   Serial.print  (F("Resolution:   ")); Serial.print(sensor.resolution); Serial.println(F(" lux"));  
  35.   Serial.println(F("------------------------------------"));
  36.   Serial.println(F(""));
  37.   delay(500);
  38. }
  39.  
  40. /**************************************************************************/
  41. /*
  42.     Configures the gain and integration time for the TSL2591
  43. */
  44. /**************************************************************************/
  45. void configureSensor(void)
  46. {
  47.   // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  48.   //tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
  49.   tsl.setGain(TSL2591_GAIN_MED);      // 25x gain
  50.   //tsl.setGain(TSL2591_GAIN_HIGH);   // 428x gain
  51.  
  52.   // Changing the integration time gives you a longer time over which to sense light
  53.   // longer timelines are slower, but are good in very low light situtations!
  54.   tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
  55.   //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
  56.   //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
  57.   //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
  58.   //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
  59.   //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS);  // longest integration time (dim light)
  60.  
  61.   /* Display the gain and integration time for reference sake */  
  62.   Serial.println(F("------------------------------------"));
  63.   Serial.print  (F("Gain:         "));
  64.   tsl2591Gain_t gain = tsl.getGain();
  65.   switch(gain)
  66.   {
  67.     case TSL2591_GAIN_LOW:
  68.       Serial.println(F("1x (Low)"));
  69.       break;
  70.     case TSL2591_GAIN_MED:
  71.       Serial.println(F("25x (Medium)"));
  72.       break;
  73.     case TSL2591_GAIN_HIGH:
  74.       Serial.println(F("428x (High)"));
  75.       break;
  76.     case TSL2591_GAIN_MAX:
  77.       Serial.println(F("9876x (Max)"));
  78.       break;
  79.   }
  80.   Serial.print  (F("Timing:       "));
  81.   Serial.print((tsl.getTiming() + 1) * 100, DEC);
  82.   Serial.println(F(" ms"));
  83.   Serial.println(F("------------------------------------"));
  84.   Serial.println(F(""));
  85. }
  86.  
  87. /**************************************************************************/
  88. /*
  89.     Program entry point for the Arduino sketch
  90. */
  91. /**************************************************************************/
  92. void setup(void)
  93. {
  94.   Serial.begin(9600);
  95.  
  96.   Serial.println(F("Starting Adafruit TSL2591 Test!"));
  97.  
  98.   if (tsl.begin())
  99.   {
  100.     Serial.println(F("Found a TSL2591 sensor"));
  101.   }
  102.   else
  103.   {
  104.     Serial.println(F("No sensor found ... check your wiring?"));
  105.     while (1);
  106.   }
  107.    
  108.   /* Display some basic information on this sensor */
  109.   displaySensorDetails();
  110.  
  111.   /* Configure the sensor */
  112.   configureSensor();
  113.    
  114.   // Now we're ready to get readings ... move on to loop()!
  115. }
  116.  
  117. /**************************************************************************/
  118. /*
  119.     Shows how to perform a basic read on visible, full spectrum or
  120.     infrared light (returns raw 16-bit ADC values)
  121. */
  122. /**************************************************************************/
  123. void simpleRead(void)
  124. {
  125.   // Simple data read example. Just read the infrared, fullspecrtrum diode
  126.   // or 'visible' (difference between the two) channels.
  127.   // This can take 100-600 milliseconds! Uncomment whichever of the following you want to read
  128.   uint16_t x = tsl.getLuminosity(TSL2591_VISIBLE);
  129.   //uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
  130.   //uint16_t x = tsl.getLuminosity(TSL2591_INFRARED);
  131.  
  132.   Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
  133.   Serial.print(F("Luminosity: "));
  134.   Serial.println(x, DEC);
  135. }
  136.  
  137. /**************************************************************************/
  138. /*
  139.     Show how to read IR and Full Spectrum at once and convert to lux
  140. */
  141. /**************************************************************************/
  142. void advancedRead(void)
  143. {
  144.   // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  145.   // That way you can do whatever math and comparisons you want!
  146.   uint32_t lum = tsl.getFullLuminosity();
  147.   uint16_t ir, full;
  148.   ir = lum >> 16;
  149.   full = lum & 0xFFFF;
  150.   Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
  151.   Serial.print(F("IR: ")); Serial.print(ir);  Serial.print(F("  "));
  152.   Serial.print(F("Full: ")); Serial.print(full); Serial.print(F("  "));
  153.   Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F("  "));
  154.   Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir));
  155. }
  156.  
  157. /**************************************************************************/
  158. /*
  159.     Performs a read using the Adafruit Unified Sensor API.
  160. */
  161. /**************************************************************************/
  162. void unifiedSensorAPIRead(void)
  163. {
  164.   /* Get a new sensor event */
  165.   sensors_event_t event;
  166.   tsl.getEvent(&event);
  167.  
  168.   /* Display the results (light is measured in lux) */
  169.   Serial.print(F("[ ")); Serial.print(event.timestamp); Serial.print(F(" ms ] "));
  170.   if ((event.light == 0) |
  171.       (event.light > 4294966000.0) |
  172.       (event.light <-4294966000.0))
  173.   {
  174.     /* If event.light = 0 lux the sensor is probably saturated */
  175.     /* and no reliable data could be generated! */
  176.     /* if event.light is +/- 4294967040 there was a float over/underflow */
  177.     Serial.println(F("Invalid data (adjust gain or timing)"));
  178.   }
  179.   else
  180.   {
  181.     Serial.print(event.light); Serial.println(F(" lux"));
  182.   }
  183. }
  184.  
  185. /**************************************************************************/
  186. /*
  187.     Arduino loop function, called once 'setup' is complete (your own code
  188.     should go here)
  189. */
  190. /**************************************************************************/
  191. void loop(void)
  192. {
  193.   // simpleRead();
  194.   // advancedRead();
  195.   unifiedSensorAPIRead();
  196.  
  197.   delay(250);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement