MK0
MK0

Reputation: 1

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.

Then I ran another Python script that uses one of the I2C pins as a GPIO using the GPIOZERO library.

  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.

I thought the close() would have reverted the pin function back to default I2C function but it did not. I also confirmed using

 i2cdetect -y 1

and confirmed I2C is really not working.

Without rebooting and how can I reprogram the pin back to I2C function programmatically using python?

Upvotes: 0

Views: 1042

Answers (1)

MK0
MK0

Reputation: 1

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. 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'])

Upvotes: 0

Related Questions