python - 无法使用 PCF8591 读取树莓派中的有效电压输入
问题描述
我尝试使用 I2C 通信通过 Raspberry-pi 中的 PCF8591 读取来自机器的电压输入,但打印一些其他值。
另外,您能否建议我,如果电压下降,我想获得机器输入电压,它应该中断一个功能,我应该使用哪种方法?
PCF8591->树莓派之间的连接
- SDL -> Rpi 上的 SDL
- SCL -> Rpi 上的 SCL
- Rpi 上的 VCC -> 3.3v 甚至尝试了 5.0v
- GND -> Rpi 上的 GND
连接机器到PCF8591
5v -> PCF8591 上的 AIN1
接地 -> 接地 Rpi
import smbus import time address = 0x48 A0 = 0x40 A1 = 0x41 A2 = 0x42 A3 = 0x43 bus = smbus.SMBus(1) while True: bus.read_byte_data(address,A1) value = bus.read_byte_data(address, A1) print("AOUT: %1.03f" %(value*3.3/255)) time.sleep(0.2)
显示这样的输出 AOUT: 2.756
解决方案
要获得正确的值,您必须读取该值两次。第一次读取告诉芯片进行新的测量并同时返回寄存器中的当前值(这不是正确的值)。在第二次读取时,您会得到正确的值:
import smbus
import time
address = 0x48
A0 = 0x40
A1 = 0x41
A2 = 0x42
A3 = 0x43
bus = smbus.SMBus(1)
while True:
bus.read_byte_data(address, A1) # do the measurement but ignore the value
value = bus.read_byte_data(address, A1) # get the correct value
#print(value)
print("AOUT: %1.03f" %(value*3.3/255))
time.sleep(0.2)
推荐阅读
- python - 如何将 Node.js 转换为 python
- r - 用 R 中另一个数据集的信息填充一个变量
- google-apps-script - 用于根据单元格值添加行的 Google 表格脚本
- android - 选项卡控制器和默认选项卡控制器之间的区别
- extjs - MODERN - 对话框未按 ESC 键关闭
- android - 在同一个 google play dev 帐户上为多个应用程序使用相同的代码
- python - Sympy,分段求和,简化
- javascript - 尝试使用 useEffect 挂钩来实现此功能
- android - 如何在 Android 9 或 10 中启用 Wifi Tethering
- javascript - 在加载到服务器期间将 .mp3 文件转换为 юogg 的最佳方法是什么?