如何使用i2c工具通过I2C接口获取BH1750(光传感器)的数据

问题描述 投票:0回答:1

我正在尝试在Ubuntu-16.04的i2c接口上使用BH170FVI sensor。我的板子是UpSquared,而不是Raspberry。

为了能够使用i2c界面,我安装了i2c-tools。在没有遇到问题之前,我还使用此工具控制了BMI160 sensor

[当我尝试使用以下命令通过终端上的传感器进行检测时:

i2cdetect -r -y 5

此命令如下所示正确检测到我的传感器:

enter image description here

似乎我的设备地址为0x23

此后,当我尝试通过命令读取所有寄存器映射数据时:

i2cdump -y -f 5 0x23

结果是:

enter image description here

问题是所有地图地址均为零。其中至少有一个应该读取光量。我以为传感器电源可能会关闭,但是我尝试使用命令i2cset进行开机,但仍然没有任何变化。

注意:我的传感器没有问题,因为我在Arduino和下面的代码中都尝试过:

#!/usr/bin/python
import smbus
import time

# Define some constants 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
ONE_TIME_HIGH_RES_MODE = 0x20

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)
  return convertToNumber(data)

def main():

  while True:
    print "Light Level : " + str(readLight()) + " lux"
    time.sleep(0.5)

if __name__=="__main__":
   main()

我的问题是为什么我不能通过i2ctools来控制传感器。

linux x86 i2c light-sensor
1个回答
0
投票

我认为@ 0andriy是正确的。您需要一个驱动程序,一个接口使其连接的库,您的情况下的python绑定以及一些配置。在用户空间中,您可以使用mraa / upm,但在Ubuntu中则没有,因此您需要构建它。

建议的答案是在内核中使用驱动程序,创建一个小的acpi表,该表告诉内核要在总线5的地址23上找到哪个设备。然后,您可以使用python-libiio绑定来访问该设备。可以加载到内核中的acpi表示例(提示:通过configfs)在此处https://github.com/westeri/meta-acpi/tree/master/recipes-bsp/acpi-tables/samples

© www.soinside.com 2019 - 2024. All rights reserved.