My program is a test system. It should first set SCL=1, SDA=0. Read some values from other GPIOs. Then set SCL=0, SDA=1. Read some values from other GPIOs. This is in order to verify the integrity of the test system. THEN it should switch to I2C mode and try to do I2C communication.

How do I switch between GPIO-mode and I2C-mode in the same program ?. I am writing the test program in PYTHON, but I think the problem would be the same in C.

Can anyone advise ?

BR Erik

  • Welcome. You should clarify whether you want to use the I2C pins as regular GPIOs, or whether you are talking about GPIOs not part of the I2C bus. If the former I think you are stuck since this is set up via /boot/config.txt although you might try pulling the kernel module and see what happens that way.
    – goldilocks
    May 19, 2019 at 15:20

1 Answer 1


I know you can do this with pigpio. I am not sure about the other Python modules.


pi = pigpio.pi()
if not pi.connected:

pi.write(SCL, 1) # automatically sets mode to WRITE
pi.write(SDA, 0) # automatically sets mode to WRITE

# read/write other GPIO

pi.write(SCL, 0)
pi.write(SDA, 1)

# read/write other GPIO

pi.set_mode(SDA, pigpio.ALT0) # set into I2C mode
pi.set_mode(SCL, pigpio.ALT0) # set into I2C mode

# Do an I2C transfer


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.