-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsi1132.py
More file actions
178 lines (160 loc) · 7.08 KB
/
Copy pathsi1132.py
File metadata and controls
178 lines (160 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# si1132
#This code is modified from https://github.com/ControlEverythingCommunity/SI1132 to work with micropython on
#an ESP8266 microcontroller using emulated SMBus from https://github.com/gkluoe/micropython-smbus.
#It is untested but should work on a variety of other platforms, provided a valid SMBus object is passed to it.
#
#Example:
#>>>from machine import Pin
#>>>from usmbus import SMBus
#>>>from si1132 import SI1132
#>>>bus = SMBus(scl=Pin(5, Pin.IN),sda=Pin(4, Pin.IN))
#>>>sensor = SI1132(smbus=bus)
#>>>sensor.read()
import time
from machine import Pin
class SI1132:
def __init__(
self,
smbus=None):
self.bus = smbus
def read(self):
# SI1132 address, 0x60(96)
# Enable UVindex measurement coefficients
self.bus.write_byte_data(0x60, 0x13, 0x29)
self.bus.write_byte_data(0x60, 0x14, 0x89)
self.bus.write_byte_data(0x60, 0x15, 0x02)
self.bus.write_byte_data(0x60, 0x16, 0x00)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0xF0(15) Enable UV, Visible, IR
self.bus.write_byte_data(0x60, 0x17, 0xF0)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x01 | 0xA0(161) Select CHLIST register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x01 | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select INT Output Enable register, 0x03(03)
# 0x01(01) INT pin driven low
self.bus.write_byte_data(0x60, 0x03, 0x01)
# SI1132 address, 0x60(96)
# Select ALS Interrupt Enable register, 0x04(04)
# 0x01(01) Assert INT pin whenever VIS or UV measurements are ready
self.bus.write_byte_data(0x60, 0x04, 0x01)
# SI1132 address, 0x60(96)
# Select HW_KEY register, 0x07(07)
# 0x17(23) Default value
self.bus.write_byte_data(0x60, 0x07, 0x17)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x00(0) Small IR photodiode
self.bus.write_byte_data(0x60, 0x17, 0x00)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x0E | 0xA0(174) Select ALS_IR_ADCMUX register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x0E | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x00(0) Set ADC Clock divided / 1
self.bus.write_byte_data(0x60, 0x17, 0x00)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x1E | 0xA0(190) Select ALS_IR_ADC_GAIN register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x1E | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x70(112) Set 511 ADC Clock
self.bus.write_byte_data(0x60, 0x17, 0x70)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x1D | 0xA0(189) Select ALS_IR_ADC_COUNTER register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x1D | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x00(0) Set ADC Clock divided / 1
self.bus.write_byte_data(0x60, 0x17, 0x00)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x11 | 0xA0(177) Select ALS_VIS_ADC_GAIN register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x11 | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x20(32) High Signal Range
self.bus.write_byte_data(0x60, 0x17, 0x20)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x1F | 0xA0(191) Select ALS_IR_ADC_MISC register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x1F | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x70(112) Set 511 ADC Clock
self.bus.write_byte_data(0x60, 0x17, 0x70)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x10 | 0xA0(176) Select ALS_VIS_ADC_COUNTER register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x10 | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select PARAM_WR register, 0x17(23)
# 0x20(32) High Signal Range
self.bus.write_byte_data(0x60, 0x17, 0x20)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x12 | 0xA0(178) Select ALS_VIS_ADC_MISC register in RAM
self.bus.write_byte_data(0x60, 0x18, (0x12 | 0xA0))
time.sleep(0.01)
# SI1132 address, 0x60(96)
# Read data back from 0x2E(46), 1 byte
response = self.bus.read_byte_data(0x60, 0x2E)
# SI1132 address, 0x60(96)
# Select COMMAND register, 0x18(24)
# 0x0E(14) Start ALS conversion
self.bus.write_byte_data(0x60, 0x18, 0x0E)
time.sleep(0.5)
# SI1132 address, 0x60(96)
# Read data back from 0x22(34), 4 bytes
# visible lsb, visible msb, ir lsb, ir msb
data = self.bus.read_i2c_block_data(0x60, 0x22, 4)
# Convert the data
visible = data[1] * 256 + data[0]
ir = data[3] * 256 + data[2]
# SI1132 address, 0x60(96)
# Read data back from 0x2C(44), 2 bytes
# uv lsb, uv msb
data = self.bus.read_i2c_block_data(0x60, 0x2C, 2)
# Convert the data
uv = data[1] * 256 + data[0]
# Output data to screen
#print("Visible Light of Source : %d lux" %visible)
#print("IR Of Source : %d lux" %ir)
#print("UV Of the Source : %d lux" %uv)
print('si1132 uv, ir, visible = %d, %d, %d lux' % (uv, ir, visible))
return uv, ir, visible