【使用笔记】树莓派基于Python操作IIC接口的红外测温传感器

mac2024-11-18  9

最近在使用红外测温传感器,IIC通信,在单片机上已能读取数据,但是想把数据读取到电脑上用python做一些处理,想到树莓派自带IIC接口,于是尝试直接将红外传感器接到树莓派的IIC管脚。

启用树莓派I2C功能: 参考瑞士军刀扩展板的教程“DS1307 RTC 时钟模块”,开启树莓派的IIC功能,教程链接: http://shumeipai.nxez.com/2016/05/30/saks-diy-tutorials-ds1307-rtc-clock-module.html 需要注意的是树莓派默认只能使用I2C1,I2C0是不能使用的。即要连接SDA.1和SCL.1 这一步的主要工作是打开树莓派的I2C接口功能,并下载安装i2c-tool和python-smbus模块: sudo apt-get install i2c-tools python-smbus 然后用命令查询是否检测到IC设备: sudo i2cdetect -y 1 pi@raspberrypi:~ $ sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: – -- – -- – -- – -- – -- – -- – 10: 10 – -- – -- – -- – -- – -- – -- – -- – 20: – -- – -- – -- – -- – -- – -- – -- – -- 30: – -- – -- – -- – -- – -- – -- – -- – -- 40: – -- – -- – -- – -- – -- – -- – -- – -- 50: – -- – -- – -- – -- – -- – -- – -- – -- 60: – -- – -- – -- – -- – -- – -- – -- – -- 70: – -- – -- – -- – --

也可以用如下命令读取I2C设备的信息: i2cdump -y 1 0x10 #0x10为I2C设备号 pi@raspberrypi:~ $ sudo i2cdump -y 1 0x10 No size specified (using byte-data access) 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef 00: ed 13 00 b1 17 00 00 cf f3 9f f5 b7 df 14 fd af ??.??..??? 10: ba ec c7 00 00 00 00 00 00 00 00 00 00 00 00 00 ???.. 20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … 40: ec 13 00 b0 17 00 00 cf f3 9f f5 b7 df 14 fd af ??.??..??? 50: ba ec c7 00 00 00 00 00 00 00 00 00 00 00 00 00 ???.. 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … 70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … 80: ec 13 00 b0 17 00 00 cf f3 9f f5 b7 df 14 fd af ??.??..??? 90: ba ec c7 00 00 00 00 00 00 00 00 00 00 00 00 00 ???.. a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … c0: ed 13 00 bf 17 00 00 cf f3 9f f5 b7 df 14 fd af ??.??..??? d0: ba ec c7 00 00 00 00 00 00 00 00 00 00 00 00 00 ???.. e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 … f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 …

用python操作I2C设备 python操作I2C的模块为smbus,库说明的链接如下: https://pypi.org/project/smbus2/0.1.2/ 常用的几个函数为: read_byte_data write_byte_data read_word_data write_word_data read_i2c_block_data write_i2c_block_data

具体读取I2C数据步骤: (1)建立总线: pi@raspberrypi:~ $ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type “help”, “copyright”, “credits” or “license” for more information.

import smbus i2c_bus = smbus.SMBus(1) slave_addr = 0x10

(2)读取字节数据(偏移地址为0x80):

i2c_bus.read_byte_data(slave_addr, 0x80) 15

(3)连续读取多个字节数据(起始偏移地址为0x80,读取8个字节):

i2c_bus.read_i2c_block_data(slave_addr, 0x80, 8) [17, 20, 0, 189, 23, 0, 0, 207]

最新回复(0)