簡體   English   中英

覆盆子零 i2c BME280 不工作(adafruit lib)

[英]Raspberry Zero i2c BME280 not working (adafruit lib)

我有以下代碼不起作用並給我一條錯誤消息。 我不太確定,但在錯誤消息中顯示 i2c 地址 77。但 i2c-detect 顯示 76。數字 48 是同一總線上的 ADC,它工作正常。

有誰知道可能導致問題的原因? 我認為默認的 i2c 地址可能是 77。我的設備之一是 76。是否可以在 adafruit lib 中以某種方式更改它?

代碼:

import board
import busio
import adafruit_bme280

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

# OR create library object using our Bus SPI port
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# bme_cs = digitalio.DigitalInOut(board.D10)
# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)

# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25

while True:
    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    time.sleep(2)

錯誤:

$ python3 i2ctest.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 184, in __probe_for_device
    self.i2c.writeto(self.device_address, b"")
  File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 101, in writeto
    return self._i2c.writeto(address, buffer, stop=stop)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 46, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_PureIO/smbus.py", line 308, in write_bytes
    self._device.write(buf)
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 190, in __probe_for_device
    self.i2c.readfrom_into(self.device_address, result)
  File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 89, in readfrom_into
    return self._i2c.readfrom_into(address, buffer, stop=stop)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 53, in readfrom_into
    readin = self._i2c_bus.read_bytes(address, end - start)
  File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_PureIO/smbus.py", line 179, in read_bytes
    return self._device.read(number)
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "i2ctest.py", line 9, in <module>
    bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bme280.py", line 482, in __init__
    self._i2c = i2c_device.I2CDevice(i2c, address)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 69, in __init__
    self.__probe_for_device()
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 192, in __probe_for_device
    raise ValueError("No I2C device at address: %x" % self.device_address)
ValueError: No I2C device at address: 77

I2C 檢測:

i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --

添加您的特定 I2C 地址,如文檔中所述:

class adafruit_bme280.Adafruit_BME280_I2C(i2c, address=<sphinx.ext.autodoc.importer._MockObject object>)

它應該看起來像:

bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)

我自己發現的。

問題確實是地址。 lib 中的默認地址是 0x77。 我的傳感器板有 0x76。

但如果你仔細檢查,你可以看到三個焊盤。 左邊和中間是由一條路線連接起來的。 如果你切斷這條路線(用針或其他東西)並連接中間和右邊,地址更改為 0x77 並且庫工作正常:)

這就是我的傳感器的樣子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM