您尚未登录。

楼主 #1 2020-07-01 10:19:53

小智跑腿
会员
注册时间: 2020-04-26
已发帖子: 78
积分: 78

小智 X3 测试 BH1750 光亮度传感器

本站下载: bh1750fvi_e_186247.pdf

离线

楼主 #2 2020-07-01 10:23:42

小智跑腿
会员
注册时间: 2020-04-26
已发帖子: 78
积分: 78

Re: 小智 X3 测试 BH1750 光亮度传感器

直接用shell读出亮度传感器数据:

i2cset -y 0 0x23 0x20;
sleep 1;
i2cget -y 0 0x23 0x00 w

离线

楼主 #3 2020-07-01 10:57:19

小智跑腿
会员
注册时间: 2020-04-26
已发帖子: 78
积分: 78

Re: 小智 X3 测试 BH1750 光亮度传感器

2020-07-01_105415.png

#!/usr/bin/python
import smbus
import time
 
# Constants taken from the datasheet
 
DEVICE     = 0x23 # Default device I2C address
 
POWER_DOWN = 0x00 # No active state
POWER_ON   = 0x01 # Power on
RESET      = 0x07 # Reset data register value
 
# Start measurement at 4 lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
 
# Start measurement at 1 lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
 
# Start measurement at 0.5 lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
 
# Start measurement at 1 lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
 
# Start measurement at 0.5 lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
 
# Start measurement at 1 lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23
 
bus = smbus.SMBus(0) # Rev 1 Pi uses 0
#bus = smbus.SMBus(1)  # Rev 2 Pi uses 1
 
def convertToNumber(data):
  # Simple function to convert 2 bytes of data
  # into a decimal number
  return ((data[1] + (256 * data[0])) / 1.2)
 
def readLight(addr=DEVICE):
  data = bus.read_i2c_block_data(addr, ONE_TIME_HIGH_RES_MODE_2)
  return convertToNumber(data)
 
def main():
 
  while True:
    print("Light Level : " + str(readLight()) + " lx")
    time.sleep(1)
 
if __name__=="__main__":
   main()

参考地址: http://raspberrypi.link-tech.de/doku.php?id=bh1750

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn