使用i2c从树莓Pi读取ADS1115时出现问题

2024-06-25 23:46:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用adc转换器(ads1115)

但是当我尝试运行示例脚本时;simpletest.py

我得到:

正在读取ADS1x15值,请按Ctrl-C退出

|0 | 1 | 2 | 3|

回溯(最近一次呼叫最后一次): 文件“simpletest.py”,第43行,在 值[i]=adc.read\u adc(i,增益=增益) 文件“build/bdist.linux-armv7l/egg/Adafruit_ADS1x15/ADS1x15.py”,第192行,在read_adc中 文件“build/bdist.linux-armv7l/egg/Adafruit_ADS1x15/ADS1x15.py”,第133行,已读 文件“build/bdist.linux-armv7l/egg/Adafruit\GPIO/I2C.py”,第136行,在readList中 文件“build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py”,第216行,在read_i2c_block_数据中 TypeError:需要一个字符串

当我运行diagnostic命令:i2cdetect时,我得到

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: -- -- -- -- -- -- -- --        

显示错误消息的python脚本(simpletest.py)的完整代码:

# Simple demo of reading each analog input from the ADS1x15 and printing it to
# the screen.
# Author: Tony DiCola
# License: Public Domain
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15


# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

# Or create an ADS1015 ADC (12-bit) instance.
#adc = Adafruit_ADS1x15.ADS1015()

# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
#adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
print('-' * 37)
# Main loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*4
    for i in range(4):
        # Read the specified ADC channel using the previously set gain value.
        values[i] = adc.read_adc(i, gain=GAIN)
        # Note you can also pass in an optional data_rate parameter that controls
        # the ADC conversion time (in samples/second). Each chip has a different
        # set of allowed data rate values, see datasheet Table 9 config register
        # DR bit values.
        #values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
        # Each value will be a 12 or 16 bit signed integer value depending on the
        # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
    # Print the ADC values.
    print('| {0:>6`enter code here`} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5) 

请告知

凯文


Tags: 文件theinpybuildadafruitreadbit
3条回答

我想补充一点,inapt布线会导致I2C地址在操作过程中发生变化,从而使设备无法在原始地址访问

我编写了一个示例程序,在不使用 AdaFruit图书馆似乎帮助了一些人: http://smartypies.com/projects/ads1115-with-raspberrypi-and-python/ads1115runner

uffff长搜索。使用Python3没有问题,但是我的ROS是用Python2.7安装的,所以我需要解决这个问题。以下是我在一个中文论坛上所做的:

覆盆子皮:

  1. mkdir~/temp

  2. cd/usr/local/lib/python2.7/dist-packages

  3. cp-p Adafruit_PureIO-1.1.5-py2.7.egg~/温度

  4. cd~/temp

  5. 解压*.egg

  6. 阿达杜普里奥酒店

  7. 编辑文件smbus.py 第255行 更改:cmdstring[i]=val 通过 cmdstring[i]=str(val) 保存文件

  8. cd

  9. zip-r Adafruit_PureIO-1.1.5-py2.7.egg Adafruit_PureIO-1.1.5-py2.7.egg/usr/local/lib/python2.7/dist-packages

我在找到解决方案后写下了这些步骤

现在您可以运行示例和自己的代码了

pi@raspberrypi:~/Adafruit\u Python\u ADS1x15/examples$Python simpletest.py 正在读取ADS1x15值,请按Ctrl-C退出。。。 |0 | 1 | 2 | 3|

| 1157 | 845 | 292 | 292| | 1157 | 845 | 296 | 294 | |1157 | 845 | 293 | 296 |

胡安

我在python2.7中遇到了同样的问题,然后我使用pip3 install Adafruit-ADS1x15安装Adafruit ADS1115库,然后使用python3运行脚本,它就可以工作了

相关问题 更多 >