1 # https://github.com/flrrth/pico-bh1750
5 from micropython
import const
6 from utime
import sleep_ms
10 """Class for the BH1750 digital Ambient Light Sensor
12 The datasheet can be found at https://components101.com/sites/default/files/component_datasheet/BH1750.pdf
15 MEASUREMENT_MODE_CONTINUOUSLY
= const(1)
16 MEASUREMENT_MODE_ONE_TIME
= const(2)
18 RESOLUTION_HIGH
= const(0)
19 RESOLUTION_HIGH_2
= const(1)
20 RESOLUTION_LOW
= const(2)
22 MEASUREMENT_TIME_DEFAULT
= const(69)
23 MEASUREMENT_TIME_MIN
= const(31)
24 MEASUREMENT_TIME_MAX
= const(254)
26 def __init__(self
, address
, i2c
):
27 self
._address
= address
29 self
._measurement
_mode
= BH1750
.MEASUREMENT_MODE_ONE_TIME
30 self
._resolution
= BH1750
.RESOLUTION_HIGH
31 self
._measurement
_time
= BH1750
.MEASUREMENT_TIME_DEFAULT
33 self
._write
_measurement
_time
()
34 self
._write
_measurement
_mode
()
36 def configure(self
, measurement_mode
: int, resolution
: int, measurement_time
: int):
37 """Configures the BH1750.
40 measurement_mode -- measure either continuously or once
41 resolution -- return measurements in either high, high2 or low resolution
42 measurement_time -- the duration of a single measurement
44 if measurement_time
not in range(BH1750
.MEASUREMENT_TIME_MIN
, BH1750
.MEASUREMENT_TIME_MAX
+ 1):
45 raise ValueError("measurement_time must be between {0} and {1}"
46 .format(BH1750
.MEASUREMENT_TIME_MIN
, BH1750
.MEASUREMENT_TIME_MAX
))
48 self
._measurement
_mode
= measurement_mode
49 self
._resolution
= resolution
50 self
._measurement
_time
= measurement_time
52 self
._write
_measurement
_time
()
53 self
._write
_measurement
_mode
()
55 def _write_measurement_time(self
):
58 high_bit
= 1 << 6 | self
._measurement
_time
>> 5
59 low_bit
= 3 << 5 |
(self
._measurement
_time
<< 3) >> 3
62 self
._i
2c
.writeto(self
._address
, buffer)
65 self
._i
2c
.writeto(self
._address
, buffer)
67 def _write_measurement_mode(self
):
70 buffer[0] = self
._measurement
_mode
<< 4 | self
._resolution
71 self
._i
2c
.writeto(self
._address
, buffer)
72 sleep_ms(24 if self
._measurement
_time
== BH1750
.RESOLUTION_LOW
else 180)
75 """Clear the illuminance data register."""
76 self
._i
2c
.writeto(self
._address
, bytearray(b
'\x07'))
79 """Powers on the BH1750."""
80 self
._i
2c
.writeto(self
._address
, bytearray(b
'\x01'))
83 """Powers off the BH1750."""
84 self
._i
2c
.writeto(self
._address
, bytearray(b
'\x00'))
87 def measurement(self
) -> float:
88 """Returns the latest measurement."""
89 if self
._measurement
_mode
== BH1750
.MEASUREMENT_MODE_ONE_TIME
:
90 self
._write
_measurement
_mode
()
93 self
._i
2c
.readfrom_into(self
._address
, buffer)
94 lux
= (buffer[0] << 8 |
buffer[1]) / (1.2 * (BH1750
.MEASUREMENT_TIME_DEFAULT
/ self
._measurement
_time
))
96 if self
._resolution
== BH1750
.RESOLUTION_HIGH_2
:
101 def measurements(self
) -> float:
102 """This is a generator function that continues to provide the latest measurement. Because the measurement time
103 is greatly affected by resolution and the configured measurement time, this function attemts to calculate the
104 appropriate sleep time between measurements.
108 for measurement in bh1750.measurements(): # bh1750 is an instance of this class
112 yield self
.measurement
114 if self
._measurement
_mode
== BH1750
.MEASUREMENT_MODE_CONTINUOUSLY
:
115 base_measurement_time
= 16 if self
._measurement
_time
== BH1750
.RESOLUTION_LOW
else 120
116 sleep_ms(math
.ceil(base_measurement_time
* self
._measurement
_time
/ BH1750
.MEASUREMENT_TIME_DEFAULT
))