I2C设置问题

huangapple go评论63阅读模式
英文:

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)

Read this link and this link.

huangapple
  • 本文由 发表于 2023年2月18日 10:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定