-
Notifications
You must be signed in to change notification settings - Fork 0
/
bh1750.py
110 lines (97 loc) · 4.66 KB
/
bh1750.py
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
#!/usr/bin/python
import smbus
import time
class BH1750:
"""Python driver for [BH1750 Digital 16-bit ambient light sensor](http://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf)
Methods:
- set_mode : Set the mode defined in initialisation
- get_light : Read the latest light measurement in lux; most useful in
continuous modes
- get_light_mode : Set the mode and read the light measurement in lux; most
useful in one time modes
Requires:
- The smbus library
"""
# Define device parameters
I2C_ADDRESS_L = 0x23 # Device I2C address for ADDR low
I2C_ADDRESS_H = 0x5C # Device I2C address for ADDR high
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Waiting for measurment command
RESET = 0x07 # Reset data register value
#Modes
CONTINUOUSLY_H_RES_MODE = 0x10 # Start measurement at 1 lx resolution.
# Measurement time is typically 120ms
CONTINUOUSLY_H_RES_MODE_2 = 0x11 # Start measurement at 0.5 lx resolution.
# Measurement time is typically 120ms
CONTINUOUSLY_L_RES_MODE = 0x13 # Start measurement at 4 lx resolution.
# Measurement time is typically 16ms.
ONE_TIME_H_RES_MODE = 0x20 # Start measurement at 1 lx resolution.
# Measurement time is typically 120ms
# Device is automatically set to Power Down
# after measurement.
ONE_TIME_H_RES_MODE_2 = 0x21 # Start measurement at 0.5 lx resolution.
# Measurement time is typically 120ms
# Device is automatically set to Power Down
# after measurement.
ONE_TIME_L_RES_MODE = 0x23 # Start measurement at 4 lx resolution.
# Measurement time is typically 16ms
# Device is automatically set to Power Down
# after measurement.
def __init__(self, addr = 0, bus = 1, mode = 3):
""" Define connection
Parameters:
- addr: 0 = address pin is low; 1 = address pin is high.
- bus: The SMBus used. Rev 1 Pi uses 0; Rev 2 Pi uses 1.
- mode: 0 to 5 = the mode as defined in the order above
"""
self.bus = bus
self.i2cbus = smbus.SMBus(self.bus)
if (addr == 0):
self.addr = self.I2C_ADDRESS_L
else:
self.addr = self.I2C_ADDRESS_H
if (mode == 0):
self.mode = self.CONTINUOUSLY_H_RES_MODE
self.wait = 0.18 # Wait 180ms for result
self.resolution_div = 1
elif (mode == 1):
self.mode = self.CONTINUOUSLY_H_RES_MODE_2
self.wait = 0.18 # Wait 180ms for result
self.resolution_div = 2
elif (mode == 2):
self.mode = self.CONTINUOUSLY_L_RES_MODE
self.wait = 0.024 # Wait 24ms for result
self.resolution_div = 1
elif (mode == 3):
self.mode = self.ONE_TIME_H_RES_MODE
self.wait = 0.18 # Wait 180ms for result
self.resolution_div = 1
elif (mode == 4):
self.mode = self.ONE_TIME_H_RES_MODE_2
self.wait = 0.18 # Wait 180ms for result
self.resolution_div = 2
else:
self.mode = self.ONE_TIME_L_RES_MODE
self.wait = 0.024 # Wait 24ms for result
self.resolution_div = 1
def get_light(self):
# Read light level from I2C interface
# Must only be called after an appropriate wait for the next measurement
# to be ready. Calling this directly in one time modes will result in
# every measurement being out of date
self.data = self.i2cbus.read_i2c_block_data(self.addr,
self.mode)
# Should not need to specify
# command, but smbus lacks a
# multi-byte read only command
lux=(self.data[1] + 256*self.data[0]) / 1.2 / self.resolution_div
return lux
def set_mode(self):
# Set mode on I2C interface
self.i2cbus.write_byte(self.addr, self.mode)
return
def get_light_mode(self):
# Set mode and read light level from I2C interface
self.set_mode()
time.sleep(self.wait) # Wait for result
return self.get_light()