英文:
I2c Setup Issue
问题
*** 如何在不使用 import BOARD 模块的情况下设置 I2C 传感器?是否有一种方法可以使用 RPi.GPIO 来设置它?
英文:
I am trying to setup an Adafruit i2c temperature sensor in Python with a RaspberryPi4. The only example code for this sensor (SCD-40) sets up the i2c by importing board module:
import board
import adafruit_scd4x
i2c = board.I2C() # uses board.SCL and board.SDA
scd4x = adafruit_scd4x.SCD4X(i2c)
*** This works and runs fine on its own. My problem is that I also have 3 stepper motors which I'm controlling using RPI.GPIO:
import RPi.GPIO as GPIO
*** When I try bringing my i2c temperature sensor into the stepper motor Python code, I get an error telling me I cannot use board with RPi.GPIO
ERROR: (GPIO.setmode(GPIO.BOARD) ValueError: A different mode has already been set!)
HOW CAN I SETUP AN I2C SENSOR WITHOUT USING THE import BOARD module? Is there a way to set it using RPi.GPIO
答案1
得分: 1
为什么需要 'RPi.GPIO'?您可以使用 'board' 与 'digitalio' 来访问 GPIO。
import board
import digitalio
import busio
pin17 = digitalio.DigitalInOut(board.D17)
pin17.direction = digitalio.Direction.OUTPUT
pin17.pull = digitalio.Pull.DOWN
value = pin17.value
您还可以使用 'busio' 进行 I2C 通信。
i2c = busio.I2C(board.SCL, board.SDA)
英文:
Why do you need 'RPi.GPIO'? You can use 'board' with 'digitalio' to access to GPIOs.
import board
import digitalio
import busio
pin17 = digitalio.DigitalInOut(board.D17)
pin17.direction = digitalio.Direction.OUTPUT
pin17.pull = digitalio.Pull.DOWN
value = pin17.value
You can also use I2C with 'busio'
i2c = busio.I2C(board.SCL, board.SDA)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论