Using I2C,SPI,uart in python

hello is there a way to control gpio using python script?
some sort of module?

here is a python script that watches a GPIO pin to control a headphone jack. This script uses pinctrl instead of gpio

import os
import time

def init_gpio():
    os.popen("pinctrl set 11 op")
    os.popen("pinctrl set 10 ip pn")

def check_3_5():
    tmp = os.popen("pinctrl 10").readline().strip("\n")
    return tmp


def enable_speaker_gpio():
    os.popen("pinctrl set 11 op dh")

def disable_speaker_gpio():
    os.popen("pinctrl set 11 op dl")

init_gpio()

while True:
    tmp =  check_3_5()
    if tmp == "10: ip    pn | lo // GPIO10 = input":
        enable_speaker_gpio()
    elif tmp == "10: ip    pn | hi // GPIO10 = input":
        disable_speaker_gpio()
    
    time.sleep(1)

Thank you so much.

1 Like

You can also try python library periphery.

much better for me :smile:

just trying to work with I2C example so i need to know where is located I2C controller?

from periphery import I2C

# Open i2c-0 controller
i2c = I2C("/dev/i2c0")

# Read byte at address 0x100 of EEPROM at 0x50
msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)]
i2c.transfer(0x50, msgs)
print("0x100: 0x{:02x}".format(msgs[1].data[0]))

i2c.close()

can you help?
just for the info im trying to hook up the ROCK with I2C lcd display.

You can refer to this example.
By the way, you need to make sure that you have enabled i2c-0 first if you use i2c-0.