首页 > 解决方案 > 无法使用 PCF8591 读取树莓派中的有效电压输入

问题描述

我尝试使用 I2C 通信通过 Raspberry-pi 中的 PCF8591 读取来自机器的电压输入,但打印一些其他值。

另外,您能否建议我,如果电压下降,我想获得机器输入电压,它应该中断一个功能,我应该使用哪种方法?

PCF8591->树莓派之间的连接

  1. SDL -> Rpi 上的 SDL
  2. SCL -> Rpi 上的 SCL
  3. Rpi 上的 VCC -> 3.3v 甚至尝试了 5.0v
  4. GND -> Rpi 上的 GND

连接机器到PCF8591

  1. 5v -> PCF8591 上的 AIN1

  2. 接地 -> 接地 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

标签: pythonraspberry-pi

解决方案


要获得正确的值,您必须读取该值两次。第一次读取告诉芯片进行新的测量并同时返回寄存器中的当前值(这不是正确的值)。在第二次读取时,您会得到正确的值:

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)

推荐阅读