luxsensor.ino 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* TSL25911 Digital Light Sensor */
  2. /* Dynamic Range: 600M:1 */
  3. /* Maximum Lux: 88K */
  4. #include <Wire.h>
  5. #include <Adafruit_Sensor.h>
  6. #include "Adafruit_TSL2591.h"
  7. Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
  8. /**************************************************************************/
  9. /*
  10. Configures the gain and integration time for the TSL2561
  11. */
  12. /**************************************************************************/
  13. void configureSensor(void)
  14. {
  15. // You can change the gain on the fly, to adapt to brighter/dimmer light situations
  16. tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
  17. //tsl.setGain(TSL2591_GAIN_MED); // 25x gain
  18. //tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
  19. // Changing the integration time gives you a longer time over which to sense light
  20. // longer timelines are slower, but are good in very low light situtations!
  21. tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
  22. //tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
  23. //tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
  24. //tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
  25. //tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
  26. //tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
  27. }
  28. /**************************************************************************/
  29. /*
  30. Program entry point for the Arduino sketch
  31. */
  32. /**************************************************************************/
  33. void initLuxSensor(void)
  34. {
  35. if (tsl.begin())
  36. {
  37. }
  38. else
  39. {
  40. Serial.println("No sensor found ... check your wiring?");
  41. while (1);
  42. }
  43. /* Configure the sensor */
  44. configureSensor();
  45. }
  46. /**************************************************************************/
  47. /*
  48. Show how to read IR and Full Spectrum at once and convert to lux
  49. */
  50. /**************************************************************************/
  51. uint32_t readLuxSensor(void)
  52. {
  53. // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  54. // That way you can do whatever math and comparisons you want!
  55. uint32_t lum = tsl.getFullLuminosity();
  56. uint16_t ir, full;
  57. ir = lum >> 16;
  58. full = lum & 0xFFFF;
  59. //Serial.print("[ "); Serial.print(millis()); Serial.print(" ms ] ");
  60. //Serial.print("IR: "); Serial.print(ir); Serial.print(" ");
  61. //Serial.print("Full: "); Serial.print(full); Serial.print(" ");
  62. //Serial.print("Visible: "); Serial.print(full - ir); Serial.print(" ");
  63. uint32_t lux = tsl.calculateLux(full, ir);
  64. if ( (lux > 4294966000.0) ||
  65. (lux <-4294966000.0) )
  66. {
  67. return 0;
  68. }
  69. else {
  70. return lux;
  71. }
  72. }