简体   繁体   English

树莓派:I2C 引脚上的 GPIOZERO

[英]Raspberry PI : GPIOZERO on I2C pins

This is on the Raspberry PI.这是在树莓派上。

I was able to get the I2C-1 to work properly on the PI using Python.我能够使用 Python 让 I2C-1 在 PI 上正常工作。

Then I ran another Python script that uses one of the I2C pins as a GPIO using the GPIOZERO library.然后我运行另一个 Python 脚本,该脚本使用 GPIOZERO 库将其中一个 I2C 引脚用作 GPIO。

  from gpiozero import LED

  i2cpin = LED("BOARD3")
  i2cpin.on()
  time.sleep(1)
  i2cpin.off()
  i2cpin.close()

However, when I go back to the first program, the I2C stopped working.但是,当我 go 回到第一个程序时,I2C 停止工作。

I thought the close() would have reverted the pin function back to default I2C function but it did not.我认为 close() 会将引脚 function 恢复为默认 I2C function 但它没有。 I also confirmed using我也确认使用

 i2cdetect -y 1

and confirmed I2C is really not working.并确认 I2C 确实不起作用。

Without rebooting and how can I reprogram the pin back to I2C function programmatically using python?无需重新启动,如何使用 python 以编程方式将引脚重新编程回 I2C function?

Ok.... for those who face the same issue, this is my workaround好的....对于那些面临同样问题的人,这是我的解决方法

In the program I need to use I2C, I will always reload the I2C module and it will take care of the pin configuration.在我需要使用 I2C 的程序中,我总是会重新加载 I2C 模块,它会负责引脚配置。 Not pretty but it works for me.不漂亮,但它对我有用。

import subprocess

def main():

  subprocess.call(['sudo','rmmod', 'i2c_dev'])
  subprocess.call(['sudo','rmmod', 'i2c_bcm2835'])
  subprocess.call(['sudo','modprobe', 'i2c_dev'])
  subprocess.call(['sudo','modprobe', 'i2c_bcm2835'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM