cn 2 years ago
parent
commit
8cfb2541b9
6 changed files with 149 additions and 1 deletions
  1. 1
    0
      .gitignore
  2. 17
    1
      README.md
  3. 13
    0
      demo.py
  4. 11
    0
      setup.py
  5. 3
    0
      veml6070/__init__.py
  6. 104
    0
      veml6070/veml6070.py

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+*.pyc

+ 17
- 1
README.md View File

@@ -1,2 +1,18 @@
1
+
1 2
 # python-veml6070
2
-A python library for accessing the VEML6070 digital UV light sensor from Vishay
3
+
4
+A Python library for accessing the [VEML6070 digital UV light sensor](http://www.vishay.com/docs/84277/veml6070.pdf) from Vishay via `python-smbus` using the I2C interface.
5
+
6
+Default settings are suitable for Raspberry Pi 2 and 3 and was successfully tested using a [breakout](https://github.com/watterott/VEML6070-Breakout).
7
+
8
+I created this Python library in style of e.g. [python-tsl2591](https://github.com/maxlklaxl/python-tsl2591) (of the TSL2591 light sensor) since I found either [python code](https://github.com/ControlEverythingCommunity/VEML6070) broken for my hardware or [code targeted at Arduino](https://github.com/kriswiner/VEML6070).
9
+
10
+## Usage
11
+
12
+Consult the data sheet and see [demo.py](demo.py) for clues how to use this library.
13
+
14
+Not all functions of the chip are supported, especially not the interrupt handling since I had no use for this. Please send pull requests for improvements and bug fixes!
15
+
16
+## License
17
+
18
+Python files in this repository are released under the [MIT license](LICENSE).

+ 13
- 0
demo.py View File

@@ -0,0 +1,13 @@
1
+
2
+import veml6070
3
+
4
+if __name__ == '__main__':
5
+  veml = veml6070.Veml6070()
6
+  for i in [veml6070.INTEGRATIONTIME_1_2T,
7
+            veml6070.INTEGRATIONTIME_1T,
8
+            veml6070.INTEGRATIONTIME_2T,
9
+            veml6070.INTEGRATIONTIME_4T]:
10
+    veml.set_integration_time(i)
11
+    uv_raw = veml.get_uva_light_intensity_raw()
12
+    uv = veml.get_uva_light_intensity()
13
+    print "Integration Time setting %d: %f W/(m*m) from raw value %d" % (i, uv, uv_raw)

+ 11
- 0
setup.py View File

@@ -0,0 +1,11 @@
1
+from setuptools import setup
2
+
3
+setup(name='veml6070',
4
+      version='1.0',
5
+      url='http://github.com/cmur2/python-veml6070',
6
+      author='Christian Nicolai',
7
+      description=' A python library for accessing the VEML6070 digital UV light sensor from Vishay.',
8
+      packages=['veml6070'],
9
+      long_description=open('README.md').read(),
10
+      requires=['python (>= 2.7)', 'smbus (>= 0.4.1)'],
11
+      install_requires=['smbus-cffi'])

+ 3
- 0
veml6070/__init__.py View File

@@ -0,0 +1,3 @@
1
+
2
+from veml6070 import Veml6070
3
+from veml6070 import INTEGRATIONTIME_1_2T, INTEGRATIONTIME_1T, INTEGRATIONTIME_2T, INTEGRATIONTIME_4T, RSET_240K, RSET_270K, RSET_300K, RSET_600K

+ 104
- 0
veml6070/veml6070.py View File

@@ -0,0 +1,104 @@
1
+
2
+import smbus
3
+import time
4
+
5
+ADDR_L=0x38 # 7bit address of the VEML6070 (write, read)
6
+ADDR_H=0x39 # 7bit address of the VEML6070 (read)
7
+
8
+RSET_240K=240000
9
+RSET_270K=270000
10
+RSET_300K=300000
11
+RSET_600K=600000
12
+
13
+SHUTDOWN_DISABLE=0x00
14
+SHUTDOWN_ENABLE=0x01
15
+
16
+INTEGRATIONTIME_1_2T=0x00
17
+INTEGRATIONTIME_1T=0x01
18
+INTEGRATIONTIME_2T=0x02
19
+INTEGRATIONTIME_4T=0x03
20
+
21
+class Veml6070:
22
+
23
+    def __init__(self, i2c_bus=1, sensor_address=ADDR_L, rset=RSET_270K, integration_time=INTEGRATIONTIME_1T):
24
+        self.bus = smbus.SMBus(i2c_bus)
25
+        self.sendor_address = sensor_address
26
+        self.rset = rset
27
+        self.shutdown = SHUTDOWN_DISABLE # before set_integration_time()
28
+        self.set_integration_time(integration_time)
29
+        self.disable()
30
+
31
+    def set_integration_time(self, integration_time):
32
+        self.integration_time = integration_time
33
+        self.bus.write_byte(self.sendor_address, self.get_command_byte())
34
+        # constant offset determined experimentally to allow sensor to readjust
35
+        time.sleep(0.2)
36
+
37
+    def get_integration_time(self):
38
+        return self.integration_time
39
+
40
+    def enable(self):
41
+        self.shutdown = SHUTDOWN_DISABLE
42
+        self.bus.write_byte(self.sendor_address, self.get_command_byte())
43
+
44
+    def disable(self):
45
+        self.shutdown = SHUTDOWN_ENABLE
46
+        self.bus.write_byte(self.sendor_address, self.get_command_byte())
47
+
48
+    def get_uva_light_intensity_raw(self):
49
+        self.enable()
50
+        # wait two times the refresh time to allow completion of a previous cycle with old settings (worst case)
51
+        time.sleep(self.get_refresh_time()*2)
52
+        msb = self.bus.read_byte(self.sendor_address+(ADDR_H-ADDR_L))
53
+        lsb = self.bus.read_byte(self.sendor_address)
54
+        self.disable()
55
+        return (msb << 8) | lsb
56
+
57
+    def get_uva_light_intensity(self):
58
+        uv = self.get_uva_light_intensity_raw()
59
+        return uv * self.get_uva_light_sensitivity()
60
+
61
+    def get_command_byte(self):
62
+        """
63
+        assembles the command byte for the current state
64
+        """
65
+        cmd = (self.shutdown & 0x01) << 0 # SD
66
+        cmd = (self.integration_time & 0x03) << 2 # IT
67
+        cmd = ((cmd | 0x02) & 0x3F) # reserved bits
68
+        return cmd
69
+
70
+    def get_refresh_time(self):
71
+        """
72
+        returns time needed to perform a complete measurement using current settings (in s)
73
+        """
74
+        case_refresh_rset = {
75
+            RSET_240K: 0.1,
76
+            RSET_270K: 0.1125,
77
+            RSET_300K: 0.125,
78
+            RSET_600K: 0.25
79
+        }
80
+        case_refresh_it = {
81
+            INTEGRATIONTIME_1_2T: 0.5,
82
+            INTEGRATIONTIME_1T: 1,
83
+            INTEGRATIONTIME_2T: 2,
84
+            INTEGRATIONTIME_4T: 4
85
+        }
86
+        return case_refresh_rset[self.rset] * case_refresh_it[self.integration_time]
87
+
88
+    def get_uva_light_sensitivity(self):
89
+        """
90
+        returns UVA light sensitivity in W/(m*m)/step
91
+        """
92
+        case_sens_rset = {
93
+            RSET_240K: 0.05,
94
+            RSET_270K: 0.05625,
95
+            RSET_300K: 0.0625,
96
+            RSET_600K: 0.125
97
+        }
98
+        case_sens_it = {
99
+            INTEGRATIONTIME_1_2T: 0.5,
100
+            INTEGRATIONTIME_1T: 1,
101
+            INTEGRATIONTIME_2T: 2,
102
+            INTEGRATIONTIME_4T: 4
103
+        }
104
+        return case_sens_rset[self.rset] / case_sens_it[self.integration_time]