I2C串列通訊實作Part2 – BH1750 微型數位光強度光照感測器
感測器簡介
|
Pin Number |
Pin Name |
Description |
|
1 |
VCC |
Power supply for the module can be 2.4V to 3.6V, typically 3.0V is used |
|
2 |
GND |
Ground of the module, connected to ground of the circuit |
|
3 |
SCL |
Serial Clock Line, used to provide clock pulse for I2C communication |
|
4 |
SDA |
Serial Data Address, used to transfer the data through I2C communication |
|
5 |
ADDR |
Device address pin, used to select the address when more than two modules are connected |
電路圖
程式列表
|
""" Micropython BH1750 ambient light sensor driver. """ import utime
class BH1750(): # Define some constants from the datasheet POWER_DOWN = 0x00 # No active state POWER_ON = 0x01 # Power on RESET = 0x07 # Reset data register value
# modes # Start measurement at 4lx resolution. Time typically 16ms. CONTINUOUS_LOW_RES_MODE = 0x13 # Start measurement at 1lx resolution. Time typically 120ms CONTINUOUS_HIGH_RES_MODE_1 = 0x10 # Start measurement at 0.5lx resolution. Time typically 120ms CONTINUOUS_HIGH_RES_MODE_2 = 0x11 # Start measurement at 1lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. ONE_TIME_HIGH_RES_MODE_1 = 0x20 # Start measurement at 0.5lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. ONE_TIME_HIGH_RES_MODE_2 = 0x21 # Start measurement at 1lx resolution. Time typically 120ms # Device is automatically set to Power Down after measurement. ONE_TIME_LOW_RES_MODE = 0x23
""" Implement BH1750 communication. """ # default addr=0x23 if addr pin floating or pulled to ground # addr=0x5c if addr pin pulled high def __init__(self, bus, addr=0x23): self.bus = bus self.addr = addr self.power_down() self.reset()
def _set_mode(self, mode): """Set sensor mode.""" self.mode = mode self.bus.write(1,self.addr, self.mode,1,1)
def power_down(self): """Turn sensor off.""" self._set_mode(self.POWER_DOWN)
def power_on(self): """Turn sensor on.""" self._set_mode(self.POWER_ON)
def reset(self): """Reset sensor, turn on first if required.""" self.power_on() #It has to be powered on before resetting self._set_mode(self.RESET)
def cont_low_res(self): self._set_mode(self.CONTINUOUS_LOW_RES_MODE)
def cont_high_res(self): self._set_mode(self.CONTINUOUS_HIGH_RES_MODE_1)
def cont_high_res2(self): self._set_mode(self.CONTINUOUS_HIGH_RES_MODE_2)
def oneshot_low_res(self): self._set_mode(self.ONE_TIME_LOW_RES_MODE)
def oneshot_high_res(self): self._set_mode(self.ONE_TIME_HIGH_RES_MODE_1)
def oneshot_high_res2(self): self._set_mode(self.ONE_TIME_HIGH_RES_MODE_2)
def luminance(self, mode): """Sample luminance (in lux), using specified sensor mode.""" data = bytearray(2) # continuous modes if mode & 0x10 and mode != self.mode: self._set_mode(mode) # one shot modes if mode & 0x20: self._set_mode(mode) # earlier measurements return previous reading utime.sleep_ms(24 if mode in (0x13, 0x23) else 180) self.bus.read(1,self.addr,data,2,1) # 讀取2個字節 factor = 2.0 if mode in (0x11, 0x21) else 1.0 #print("%x,%x"%(data[0],data[1])) #Debug return ((data[0]<<8 | data[1]) / (1.2 * factor)) |
|
""" ------------ 3V3 VCC GND GND P19 SCL P20 SDA ADDR """
from micropython import const from machine import KEY #獲取按鍵KEY庫 from machine import I2C, PIN import utime import BH1750
i2c_1 = None addrBH1750 = const(0x23) # BH1750 Device ID address for slave target level = 0
# Start Function if __name__=="__main__": # Declaration I2C i2c_1 = I2C(1,100000) #創建頻率為100kHz的I2C外設,選擇要使用的外設I2C 1 sensor = BH1750.BH1750(i2c_1) key_c = KEY(KEY.KEYC) #創建按鍵C print("Start light measurement.")
try: while True: level = sensor.luminance(sensor.ONE_TIME_HIGH_RES_MODE_1) print("Light Level :%d"%(level)) utime.sleep(1) if key_c.value() == 0: #如果按鍵C被按下 break
except: pass
i2c_1.deinit() print("Exit light measurement.")
|
執行結果
|
Start light measurement. Light Level :1050 lx Light Level :1040 lx Light Level :1051 lx Light Level :1035 lx Light Level :1042 lx Exit light measurement. |
沒有留言:
張貼留言