ADCPi如何运行而不是根?

2024-10-02 00:38:55 发布

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

我有一个PiModelA,运行最新版本的Raspbian。插入它的是一个adcpi(https://www.abelectronics.co.uk/products/3/Raspberry-Pi/17/ADC-Pi-V2---Raspberry-Pi-Analogue-to-Digital-converter)和各种模拟传感器。在

运行演示代码(如下) 如果我使用'./adc_演示.py“效果很好 如果我用'sudo python3 adc_py演示“I get the error”导入错误:没有名为quick2wire.i2c的模块。 如何才能使用后一个语句运行它?我有另一个脚本,它通过pi上的GPIO管脚运行一个电机,它需要以root用户的身份运行,我正在尝试将这两个脚本合并在一起。在

模数转换器_演示.py在

#!/usr/bin/env python3
# read abelectronics ADC Pi board inputs
# uses quick2wire from http://quick2wire.com/
# See http://elinux.org/RPi_ADC_I2C_Python for full setup instructions

import quick2wire.i2c as i2c

import re
import time

adc_address1 = 0x68
adc_address2 = 0x69

adc_channel1 = 0x98
adc_channel2 = 0xB8
adc_channel3 = 0xD8
adc_channel4 = 0xF8


for line in open('/proc/cpuinfo').readlines():
    m = re.match('(.*?)\s*:\s*(.*)', line)
    if m:
        (name, value) = (m.group(1), m.group(2))
        if name == "Revision":
           if value [-4:] in ('0002', '0003'):
                i2c_bus = 0
            else:
                i2c_bus = 1
            break

with i2c.I2CMaster(i2c_bus) as bus:

    def getadcreading(address, channel):
        bus.transaction(i2c.writing_bytes(address, channel))
        time.sleep(0.05)
        h, l, r = bus.transaction(i2c.reading(address,3))[0]
        time.sleep(0.05)
        h, l, r = bus.transaction(i2c.reading(address,3))[0]

        t = (h << 8) | l
        v = t * 0.000154
        if v < 5.5:
            return v
        else: # must be a floating input
            return 0.00

while True:

    print("1: %f" % getadcreading(adc_address1, adc_channel1))
    print("2: %f" % getadcreading(adc_address1, adc_channel2))
    print("3: %f" % getadcreading(adc_address1, adc_channel3))
    print("4: %f" % getadcreading(adc_address1, adc_channel4))

    print("5: %f" % getadcreading(adc_address2, adc_channel1))
    print("6: %f" % getadcreading(adc_address2, adc_channel2))
    print("7: %f" % getadcreading(adc_address2, adc_channel3))
    print("8: %f" % getadcreading(adc_address2, adc_channel4))

    time.sleep(1)

Tags: pyimportiftimeaddresspii2cprint

热门问题