Main Menu

search

You are here

rPi Programming - Python

[last updated: 2024-10-09]
rPi home page
-----

  • serial communication on rPi
  • RS485 on rPi
    -------------------------------------------------

  • gpio programming:
    • Setup/initialization:
      • import RPi.GPIO as gpio # "gpio" is your choice of name
      • gpio.setmode(gpio.BOARD) # this allows using board pin numbers to designate i/o
      • gpio.setmode(gpio.BCM) # this allows using gpio numbers to designate i/o
      • gpio.setup(4, gpio.OUT, initial=gpio.LOW) # "4" can be a variable
      • gpio.setup(6, gpio.IN, pull_up_down=gpio.PUD_UP)
    • IRQ (callback) programming:
      • First you must define your callback/interrupt function, eg.:
          def PBpressed(channel): # "channel" is the name of the var that is returned by the function when it executes ... ???
                print("do something")
      • Then, in the code, After you've defined the function,
          gpio.add_event_detect(varName, gpio.FALLING, callback=PBpressed)

      • However:
        When I first did this on my rPi 3B+, it "failed to set event detect"
        Researching online (best guess) suggested there is a bug in this latest version of the rPIOS code ("bookworm"),
        and the workaround (that worked for me) is:
          $ sudo apt remove python3-rpi.gpio
          $ sudo apt update
          $ sudo apt install python3-rpi-lgpio

    -------------------------------------------------