如何使用 python I2C TCA9548A 从多路复用器读取

2023-12-04

基于以下链接: https://github.com/kizniche/Mycodo/issues/43#issuecomment-156718451

我创建了以下内容:

多路复用器.py

#!/usr/bin/python

# Change channel of TCA9548A
# Example: sudo ./multiplexer_channel.py 0

import smbus
import time
import sys

I2C_address = 0x77
I2C_bus_number = 1
I2C_ch_0 = 0b00000001
I2C_ch_1 = 0b00000010
I2C_ch_2 = 0b00000100
I2C_ch_3 = 0b00001000
I2C_ch_4 = 0b00010000
I2C_ch_5 = 0b00100000
I2C_ch_6 = 0b01000000
I2C_ch_7 = 0b10000000

def I2C_setup(i2c_channel_setup):
    bus = smbus.SMBus(I2C_bus_number)
    bus.write_byte(I2C_address,i2c_channel_setup)
    time.sleep(0.1)
    print "TCA9548A I2C channel status:", bin(bus.read_byte(I2C_address))

I2C_setup(int(sys.argv[1]))

和 索引2.py

from tentacle_pi.BMP180 import BMP180
import time
bmp = BMP180(0x70,"/dev/i2c-1")


for x in range(0,1005):
        print "temperature: %s" % bmp.temperature()
        print "pressure: %s" % bmp.pressure()
        print "altitude: %s" % bmp.altitude()
        print
        time.sleep(2)

如果我使用参数 0 到 7(多路复用器端口)执行第一个文件,我总是会遇到连接超时。

请注意,我使用的是树莓派 PI 3,所有东西都已连接。

如果我执行第二个文件,我会得到读数,但它们总是具有误导性(固定读数),顺便说一句,我认为如果我有很多传感器(BMP180),第二个文件将不起作用

编辑1: 1. 当我们尝试 i2cdetect y 时,我们什么也没得到。 2. 我们找到了另一个与多路复用器一起使用的 Python 程序Ñ

# coding=utf-8

import argparse
import smbus
import time


class TCA9548A(object):
    def __init__(self, bus, address=0x70, ):
        self.i2c_address = address
        self.i2c_bus = bus
        self.bus = smbus.SMBus(self.i2c_bus)

    def setup(self, channel):
        try:
            self.bus.write_byte(self.i2c_address, channel)
            return 1, "Success"
        except Exception as msg:
            return 0, "Fail: {}".format(msg)

    def read(self):
        time.sleep(0.1)
        return self.bus.read_byte(self.i2c_address)


def menu():
    parser = argparse.ArgumentParser(description='Select I2C address and channel of TCA9548A I2C multiplexer')
    parser.add_argument('-a', '--address', metavar='ADDRESS', type=int,
                        help='I2C address of the multiplexer, only last two digits, (ex. enter "70" if 0x70)',
                        required=True)
    parser.add_argument('-b', '--bus', metavar='BUS', type=int,
                        help='I2C bus of the multiplexer',
                        required=True)
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-c', '--channel', metavar='CHANNEL', type=int,
                       help='Channel to be activated with the multiplexer')
    group.add_argument('-r', '--read', action='store_true',
                       help='Only read multiplexer and return channel number')

    args = parser.parse_args()

    i2c_address = int(str(args.address), 16)
    multiplexer = TCA9548A(args.bus, i2c_address)
    if args.channel:
        multiplexer.setup(args.channel)
    read_response = multiplexer.read()
    print("TCA9548A I2C channel status: {} (channel {})".format(bin(read_response), read_response))


if __name__ == "__main__":
    menu()

当我们尝试像这样执行它时:

sudo python index3py -a 70 -b 1

我们还得到了连接超时

我开始相信我们之间可能有一些错误的联系。

我们使用 adafruit 接线示例

EDIT2:带有图片

enter image description here

enter image description here

enter image description here

编辑3: 我重新启动了 PI,现在我可以看到 77 地址上的 i2c。

然而,我得到了误导性的结果,我尝试使用同一程序(index2.py)将所有 7 个通道从 70 更改为 77。并且我总是得到一个毫无意义的温度。

pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 0
TCA9548A I2C channel status: 0b11111000 (channel 248)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.599998474
pressure: 40287
altitude: -31.4230690002

temperature: -52.5
pressure: 40183
altitude: -31.5887317657

temperature: -52.5
pressure: 40281
altitude: -37.2313270569

temperature: -52.5
pressure: 40284
altitude: -34.078414917

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 1
TCA9548A I2C channel status: 0b1 (channel 1)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40166
altitude: -31.7548980713

temperature: -52.4000015259
pressure: 40163
altitude: -34.5762786865

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 2
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28375
altitude: 1204.44226074

temperature: -52.4000015259
pressure: 28303
altitude: 1222.52453613

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 3
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28304
altitude: 1205.19128418

temperature: -52.5
pressure: 28301
altitude: 1222.90002441

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 4
TCA9548A I2C channel status: 0b100 (channel 4)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40136
altitude: -46.9332351685

temperature: -52.5
pressure: 40116
altitude: -47.5958938599

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 5
TCA9548A I2C channel status: 0b101 (channel 5)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 40185
altitude: -46.9332351685

temperature: -52.5
pressure: 40136
altitude: -47.0986480713

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 6
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28365
altitude: 1216.15002441

temperature: -52.5
pressure: 28297
altitude: 1215.96289062

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index3.py -a 77 -b 1 -c 7
TCA9548A I2C channel status: 0b0 (channel 0)
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.p
python: can't open file 'index2.p': [Errno 2] No such file or directory
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ sudo python index2.py
temperature: -125.699996948
pressure: 28368
altitude: 1219.89953613

temperature: -52.5
pressure: 28283
altitude: 1219.71179199

temperature: -52.5
pressure: 28353
altitude: 1219.71179199

^CTraceback (most recent call last):
  File "index2.py", line 11, in <module>
    time.sleep(2)
KeyboardInterrupt
pi@pi1:~/Documents/NodeJSProjects/PythonTests $ ^C

我修改了你原来的例子,这对我有用:

My TCA9548A.py如下:

#!/usr/bin/python
# intended for import to change channel of TCA9548A
# import then call I2C_setup(multiplexer_addr,multiplexer_channel)

import smbus
import time
import sys

channel_array=[0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000]

def I2C_setup(multiplexer,i2c_channel_setup):
    bus = smbus.SMBus(1)
    bus.write_byte(multiplexer,channel_array[i2c_channel_setup])
    time.sleep(0.01)
    #uncomment to debug #print("TCA9548A I2C channel status:", bin(bus.read_byte(multiplexer)))

然后在同一目录中我有一个文件读取传感器.py它利用了你的例子:

#!/usr/bin/python

import TCA9548A

# set specific multiplexer to a specific channel
# TCA9548A.I2C_setup( multiplexer_addr , multiplexer_channel )
TCA9548A.I2C_setup(0x70,5)

接下来,根据需要导入库并从传感器获取数据。我有多个 BMP280,它们都有相同的 0x76 地址。因此,当我从 0x76 处的 BMP280 传感器读取数据时,多路复用器将仅选择该通道并对 I2C 线路开放。接下来我可能会去TCA9548A.I2C_设置(0x70,6)然后再次从 0x76 处的 BMP280 读取数据,但我将从该多路复用器通道 6 上的下一个 BMP280 接收数据。

我有另一个多路复用器,我将通过其自定义地址 0x71 来调用它。我将其 A0 引脚拉高至 VCC,以将该多路复用器的地址更改为 0x71。请注意,这里的代码没有利用多路复用器不选择通道的功能,因此请注意,任何多路复用器的最后选择的通道都将对 I2C 线路开放。我没有使用线写(0)方法如阿达水果的例子...因此,也许在使用多个多路复用器上的通道之前,要么在未使用的多路复用器上选择未使用的通道,要么使用 GPIO 将多路复用器 RST 引脚拉低以将其禁用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 python I2C TCA9548A 从多路复用器读取 的相关文章

随机推荐

  • 如何在不同时间向 facebook 请求不同的权限?

    Facebook 建议 在使用 Facebook 登录时 您应该首先向用户请求尽可能少的权限 特别是避免请求发布权限 直到用户需要通过您的网站发布某些内容 https developers facebook com docs faceboo
  • 用于在窗口上打印串行数据的Python代码。

    我对 python 和 pyserial 很陌生 我的电脑安装了带有 pyserial 的 python 2 7 4 我想在我的电脑上的单独窗口上打印串行接收的数据 首先必须打开窗口 然后在该窗口上打印串行数据 这里必须打开一次窗口 并且必
  • 什么会让 git 在 git pull --rebase 期间删除本地文件?

    我正在尝试重现我尝试回答所导致的问题这个问题 In short github 用户尝试这样做git pull rebase并且该用户的本地文件已被删除 我尝试在 github 上重新创建这个场景 但在我的例子中没有删除任何内容 那么如何重现
  • C: printf 一个浮点值

    我想打印一个浮点值 该值在逗号后有 2 个整数和 6 个小数位 如果我只是使用printf f myFloat 我得到了一个被截断的值 我不知道这是否总是发生在 C 语言中 或者只是因为我使用 C 语言作为微控制器 确切地说是 CCS 但在
  • 如何在 Android 中调出锁屏?

    我想在单击按钮时调出设备的锁定屏幕 我搜索了很多并且知道可以通过DevicePolicyManager但不知道该怎么做 我还找到了很多例子 但它们都没有打开锁屏 没有错误 例如this or this 如何使用锁定设备DevicePolic
  • 如何在Python中计算One Class SVM的AUC?

    我在 python 中绘制 OneClassSVM 的 AUC 图时遇到困难 我使用 sklearn 生成混淆矩阵 例如 tp fp fn tn with fn tn 0 from sklearn metrics import roc cu
  • Grunt imagemin正在运行但不缩小

    Image min 运行正常 但我得到 0 个缩小的图像 为什么 在我的终端上运行代码后 grunt imagemin Running imagemin dist imagemin task Minified 0 images saved
  • 如何在 Laravel 5.0 中使用外全连接?

    这是我的控制器 public function lihatpesanansemua ajax if Request ajax hasil DB table pesanan gt join pemesan pemesan id pesanan
  • 解析字段名不一致的JSON字符串

    我在反序列化以下 JSON 结构时遇到问题 每个节点包含一个 ID 和带有值的多语言代码 语言属性的数量并不一致 但我需要这些值作为具有语言字段和值字段的对象列表 id w 312457 eng deep fat frying ger Fr
  • 需要帮助在单个单元格中获取多个值,并在 Excel 中满足条件

    我需要帮助来获取单个单元格中的值并满足条件 我想要单个单元格中的值低于 95 的违约者 e g 如果有动态数组公式 FILTER 和 TEXTJOIN TEXTJOIN CHAR 10 TRUE FILTER A2 A7 E2 E7 lt
  • 如何在 iOS 应用程序中以编程方式创建 PDF 文件?

    如何根据用户操作生成 PDF 文件 See 用石英画画 了解如何创建 PDF 图形上下文 一些注意事项 iPhoneOS注意 如果您想在 iPhone 应用程序中创建 PDF 图形上下文 请确保您还阅读了 在 iPhone OS 中绘制到图
  • 在 C# 中使用 AES 加密

    Locked 这个问题及其答案是locked因为这个问题是题外话 但却具有历史意义 目前不接受新的答案或互动 我似乎找不到使用 AES 128 位加密的清晰示例 有人有一些示例代码吗 如果您只想使用内置加密提供程序 RijndaelMana
  • 注销 WordPress 并重定向到不同的 URL

    我的网站上有一个用于注销 Wordpress 的注销选项 注销后 我想将用户重定向到不同的 URL 我在functions php中使用它 add action wp logout auto redirect external after
  • 使用 PySide 和 QTextEdit 半透明突出显示

    我创建了一个 QTextEdit 对象 下面的代码向当前选定的文本添加随机颜色的突出显示 我需要高光是半透明的 这样我就可以看到高光彼此分层 使用 setAlpha 似乎没有做任何事情 如何设置突出显示的 Alpha 或以其他方式获得半透明
  • 在 Active Directory 中查找用户的管理员记录

    我尝试使用 Active Directory 查找用户经理的 SamAccountName 和电子邮件 我通过搜索在 AD 中找到登录用户 其中 sAMAccountName Domain Account 然后 我检索管理器属性 如下所示
  • 如何隐藏 iPadOS 13 以来出现的 iPad Safari Web App 全屏模式上的新网址栏?

    现在 当通过 Safari 上的 添加到主屏幕 安装 WebApp 时 iPadOS 13 会显示白色 灰色条 即使添加了 apple touch fullscreen 元标记也是如此 该栏包含一个用于调整字体大小和请求桌面站点的菜单 但影
  • 如何使用 GSON 解析 JSON 响应(不同对象类型)

    问题 解析来自 Foursquare Venues API 的以下响应 meta code 200 notifications type notificationTray item unreadCount 0 response venues
  • 无法从您的 Ionic 应用程序生成 Android App Bundle(没有 Android Studio)

    这大部分是重复的Android 构建失败但目前还没有答案 而且我的情况略有不同 所以再次提出 就我而言 我能够正确构建 apk 并且我想在命令成功后创建一个应用程序包 ionic cordova build android prod rel
  • Azure Functions-发布 local.settings.json

    如何发布我的 Azure Functions local settings json IsEncrypted false Values AzureWebJobsStorage UseDevelopmentStorage true FUNCT
  • 如何使用 python I2C TCA9548A 从多路复用器读取

    基于以下链接 https github com kizniche Mycodo issues 43 issuecomment 156718451 我创建了以下内容 多路复用器 py usr bin python Change channel