SparkFun Qwiic GPIO Hookup Guide
Python Examples
With the Qwiic GPIO Python Package installed, we can start working with the examples included with it. In this section we'll go over each example and highlight pertinent bits of code. To run the examples, download or copy the code into a file then open/save the example file (if needed) and execute the code in your preffered Python IDE.
Qwiic GPIO Example 1
This example sets up all eight I/O pins as outputs and toggles them on and off each second. Note you need to define the mode of each pin as well as call setMode
to write the configuration to the Qwiic GPIO. Copy the code below and execute it in your chosen Python IDE.
from __future__ import print_function import qwiic_gpio import time import sys def runExample(): print("\nSparkFun Qwiic GPIO Example 1\n") myGPIO = qwiic_gpio.QwiicGPIO() if myGPIO.isConnected() == False: print("The Qwiic GPIO isn't connected to the system. Please check your connection", \ file=sys.stderr) return myGPIO.begin() myGPIO.mode_0 = myGPIO.GPIO_OUT myGPIO.mode_1 = myGPIO.GPIO_OUT myGPIO.mode_2 = myGPIO.GPIO_OUT myGPIO.mode_3 = myGPIO.GPIO_OUT myGPIO.mode_4 = myGPIO.GPIO_OUT myGPIO.mode_5 = myGPIO.GPIO_OUT myGPIO.mode_6 = myGPIO.GPIO_OUT myGPIO.mode_7 = myGPIO.GPIO_OUT myGPIO.setMode() while True: myGPIO.out_status_0 = myGPIO.GPIO_HI myGPIO.out_status_1 = myGPIO.GPIO_HI myGPIO.out_status_2 = myGPIO.GPIO_HI myGPIO.out_status_3 = myGPIO.GPIO_HI myGPIO.out_status_4 = myGPIO.GPIO_HI myGPIO.out_status_5 = myGPIO.GPIO_HI myGPIO.out_status_6 = myGPIO.GPIO_HI myGPIO.out_status_7 = myGPIO.GPIO_HI myGPIO.setGPIO() print("set hi") time.sleep(1) myGPIO.out_status_0 = myGPIO.GPIO_LO myGPIO.out_status_1 = myGPIO.GPIO_LO myGPIO.out_status_2 = myGPIO.GPIO_LO myGPIO.out_status_3 = myGPIO.GPIO_LO myGPIO.out_status_4 = myGPIO.GPIO_LO myGPIO.out_status_5 = myGPIO.GPIO_LO myGPIO.out_status_6 = myGPIO.GPIO_LO myGPIO.out_status_7 = myGPIO.GPIO_LO myGPIO.setGPIO() print("set lo") time.sleep(1) if __name__ == '__main__': try: runExample() except (KeyboardInterrupt, SystemExit) as exErr: print("\nEnding Example 1") sys.exit(0)
Qwiic GPIO Example 2
Example 2 shows how to read each I/O pin when they are configured as inputs. It first sets up all pins using the setMode
function as we did in Example 1. The code then monitors and prints out the status of each input every 0.25 seconds. You can copy the entire example below or open it from your downloaded copy of the Python package:
from __future__ import print_function import qwiic_gpio import time import sys def runExample(): print("\nSparkFun Qwiic GPIO Example 2\n") myGPIO = qwiic_gpio.QwiicGPIO() if myGPIO.isConnected() == False: print("The Qwiic GPIO isn't connected to the system. Please check your connection", file=sys.stderr) return myGPIO.begin() myGPIO.mode_0 = myGPIO.GPIO_IN myGPIO.mode_1 = myGPIO.GPIO_IN myGPIO.mode_2 = myGPIO.GPIO_IN myGPIO.mode_3 = myGPIO.GPIO_IN myGPIO.mode_4 = myGPIO.GPIO_IN myGPIO.mode_5 = myGPIO.GPIO_IN myGPIO.mode_6 = myGPIO.GPIO_IN myGPIO.mode_7 = myGPIO.GPIO_IN myGPIO.setMode() while True: myGPIO.getGPIO() #This function updates each in_status_x variable print("GPIO 0:", end=" ") print(myGPIO.in_status_0, end=" ") print("GPIO 1:", end=" ") print(myGPIO.in_status_1, end=" ") print("GPIO 2:", end=" ") print(myGPIO.in_status_2, end=" ") print("GPIO 3:", end=" ") print(myGPIO.in_status_3, end=" ") print("GPIO 4:", end=" ") print(myGPIO.in_status_4, end=" ") print("GPIO 5:", end=" ") print(myGPIO.in_status_5, end=" ") print("GPIO 6:", end=" ") print(myGPIO.in_status_6, end=" ") print("GPIO 7:", end=" ") print(myGPIO.in_status_7) time.sleep(.25) if __name__ == '__main__': try: runExample() except (KeyboardInterrupt, SystemExit) as exErr: print("\nEnding Example 1") sys.exit(0)
Qwiic GPIO Example 3
Example 3 demonstrates how to invert an I/O pin configured as an input. We first set all eight pins as inputs and then invert half of them using the setInversion(self)
function. As we covered in the Arduino Examples section, each I/O pin set as an input defaults to an active HIGH input so inverting it switches it to an active LOW input. Again, note that you need to define the inversion status of each pin you wish to invert and then write that data using the setInversion
function.
from __future__ import print_function import qwiic_gpio import time import sys def runExample(): print("\nSparkFun Qwiic GPIO Example 3\n") myGPIO = qwiic_gpio.QwiicGPIO() if myGPIO.isConnected() == False: print("The Qwiic GPIO isn't connected to the system. Please check your connection", file=sys.stderr) return myGPIO.begin() myGPIO.mode_0 = myGPIO.GPIO_IN myGPIO.mode_1 = myGPIO.GPIO_IN myGPIO.mode_2 = myGPIO.GPIO_IN myGPIO.mode_3 = myGPIO.GPIO_IN myGPIO.mode_4 = myGPIO.GPIO_IN myGPIO.mode_5 = myGPIO.GPIO_IN myGPIO.mode_6 = myGPIO.GPIO_IN myGPIO.mode_7 = myGPIO.GPIO_IN myGPIO.setMode() myGPIO.inversion_0 = myGPIO.INVERT myGPIO.inversion_1 = myGPIO.NO_INVERT myGPIO.inversion_2 = myGPIO.INVERT myGPIO.inversion_3 = myGPIO.NO_INVERT myGPIO.inversion_4 = myGPIO.INVERT myGPIO.inversion_5 = myGPIO.NO_INVERT myGPIO.inversion_6 = myGPIO.INVERT myGPIO.inversion_7 = myGPIO.NO_INVERT myGPIO.setInversion() while True: myGPIO.getGPIO() # This function updates each in_status_x variable print("GPIO 0:", end=" ") print(myGPIO.in_status_0, end=" ") print("GPIO 1:", end=" ") print(myGPIO.in_status_1, end=" ") print("GPIO 2:", end=" ") print(myGPIO.in_status_2, end=" ") print("GPIO 3:", end=" ") print(myGPIO.in_status_3, end=" ") print("GPIO 4:", end=" ") print(myGPIO.in_status_4, end=" ") print("GPIO 5:", end=" ") print(myGPIO.in_status_5, end=" ") print("GPIO 6:", end=" ") print(myGPIO.in_status_6, end=" ") print("GPIO 7:", end=" ") print(myGPIO.in_status_7) time.sleep(.25) if __name__ == '__main__': try: runExample() except (KeyboardInterrupt, SystemExit) as exErr: print("\nEnding Example 1") sys.exit(0)