A python library for accessing the VEML6070 digital UV light sensor from Vishay
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import time
  2. import smbus # pylint: disable=import-error
  3. ADDR_L = 0x38 # 7bit address of the VEML6070 (write, read)
  4. ADDR_H = 0x39 # 7bit address of the VEML6070 (read)
  5. RSET_240K = 240000
  6. RSET_270K = 270000
  7. RSET_300K = 300000
  8. RSET_600K = 600000
  9. SHUTDOWN_DISABLE = 0x00
  10. SHUTDOWN_ENABLE = 0x01
  11. INTEGRATIONTIME_1_2T = 0x00
  12. INTEGRATIONTIME_1T = 0x01
  13. INTEGRATIONTIME_2T = 0x02
  14. INTEGRATIONTIME_4T = 0x03
  15. # Scale factor in seconds / Ohm to determine refresh time for any RSET
  16. # Note: 0.1 seconds (100 ms) are applicable for RSET_240K and INTEGRATIONTIME_1T
  17. RSET_TO_REFRESHTIME_SCALE = 0.1 / RSET_240K
  18. # The refresh time in seconds for which NORMALIZED_UVA_SENSITIVITY
  19. # is applicable to a step count
  20. NORMALIZED_REFRESHTIME = 0.1
  21. # The UVA sensitivity in W/(m*m)/step which is applicable to a step count
  22. # normalized to the NORMALIZED_REFRESHTIME, for RSET_240K and INTEGRATIONTIME_1T
  23. NORMALIZED_UVA_SENSITIVITY = 0.05
  24. class Veml6070(object): # pylint: disable=bad-option-value,useless-object-inheritance
  25. def __init__(self, i2c_bus=1, sensor_address=ADDR_L, rset=RSET_270K, integration_time=INTEGRATIONTIME_1T):
  26. self.bus = smbus.SMBus(i2c_bus)
  27. self.sendor_address = sensor_address
  28. self.rset = rset
  29. self.shutdown = SHUTDOWN_DISABLE # before set_integration_time()
  30. self.set_integration_time(integration_time)
  31. self.disable()
  32. def set_integration_time(self, integration_time):
  33. self.integration_time = integration_time
  34. self.bus.write_byte(self.sendor_address, self.get_command_byte())
  35. # constant offset determined experimentally to allow sensor to readjust
  36. time.sleep(0.2)
  37. def get_integration_time(self):
  38. return self.integration_time
  39. def enable(self):
  40. self.shutdown = SHUTDOWN_DISABLE
  41. self.bus.write_byte(self.sendor_address, self.get_command_byte())
  42. def disable(self):
  43. self.shutdown = SHUTDOWN_ENABLE
  44. self.bus.write_byte(self.sendor_address, self.get_command_byte())
  45. def get_uva_light_intensity_raw(self):
  46. self.enable()
  47. # wait two times the refresh time to allow completion of a previous cycle with old settings (worst case)
  48. time.sleep(self.get_refresh_time() * 2)
  49. msb = self.bus.read_byte(self.sendor_address + (ADDR_H - ADDR_L))
  50. lsb = self.bus.read_byte(self.sendor_address)
  51. self.disable()
  52. return (msb << 8) | lsb
  53. def get_uva_light_intensity(self):
  54. """
  55. returns the UVA light intensity in Watt per square meter (W/(m*m))
  56. """
  57. raw_data = self.get_uva_light_intensity_raw()
  58. # normalize raw data (step count sampled in get_refresh_time()) into the
  59. # linearly scaled normalized data (step count sampled in 0.1s) for which
  60. # we know the UVA sensitivity
  61. normalized_data = raw_data * NORMALIZED_REFRESHTIME / self.get_refresh_time()
  62. # now we can calculate the absolute UVA power detected combining
  63. # normalized data with known UVA sensitivity for this data
  64. return normalized_data * NORMALIZED_UVA_SENSITIVITY # in W/(m*m)
  65. def get_command_byte(self):
  66. """
  67. assembles the command byte for the current state
  68. """
  69. cmd = (self.shutdown & 0x01) << 0 # SD
  70. cmd = cmd | (self.integration_time & 0x03) << 2 # IT
  71. cmd = ((cmd | 0x02) & 0x3F) # reserved bits
  72. return cmd
  73. def get_refresh_time(self):
  74. """
  75. returns time needed to perform a complete measurement using current settings (in s)
  76. """
  77. case_refresh_it = {
  78. INTEGRATIONTIME_1_2T: 0.5,
  79. INTEGRATIONTIME_1T: 1,
  80. INTEGRATIONTIME_2T: 2,
  81. INTEGRATIONTIME_4T: 4
  82. }
  83. return self.rset * RSET_TO_REFRESHTIME_SCALE * case_refresh_it[self.integration_time]
  84. @staticmethod
  85. def get_estimated_risk_level(intensity):
  86. """
  87. returns estimated risk level from comparing UVA light intensity value
  88. in W/(m*m) as parameter, thresholds calculated from application notes
  89. """
  90. if intensity < 24.888:
  91. return "low"
  92. if intensity < 49.800:
  93. return "moderate"
  94. if intensity < 66.400:
  95. return "high"
  96. if intensity < 91.288:
  97. return "very high"
  98. return "extreme"