Using GPIO Zero
The following sections show a few basic ways to interface with Dictel’s io1212 using the GPIO Zero library in Python.
Installation
As seen on GPIO Zero’s installation instructions, GPIO Zero is available through the apt repositories of Ubuntu, Debian, and Raspberry Pi OS.
Install it by running:
ubuntu@rpi:~$ sudo apt update
ubuntu@rpi:~$ sudo apt install python3-gpiozero
You can also install the package through Python’s pip
command. If you choose to install GPIO Zero inside of a virtual environment, take into account any relevant development notes.
ubuntu@rpi:~$ sudo pip3 install gpiozero
Example 1: Blink
This first example shows how to turn a single output on and off repeatedly using GPIO Zero’s LED
class. The output uses relay #0 (connected in pin 4, as shown in Pinout).
#!/usr/bin/python
from gpiozero import LED
from time import sleep
output = LED(4)
while True:
output.on()
sleep(1)
output.off()
sleep(1)
Example 2: Using LEDBoard
Expanding on the previous example, the LEDBoard
class can be used to group several outputs together. This example sets up an LED Board with 12 outputs and blinks the first one.
#!/usr/bin/python
from gpiozero import LEDBoard
from time import sleep
outputs = LEDBoard(4,5,6,7,8,9,10,11,12,13,14,15)
outputs.off() # turn off all outputs
while True:
outputs.on(0)
sleep(1)
outputs.off(0)
sleep(1)
Example 3: Naming your outputs
You can define module-level “constants” naming each of your outputs and later use them to index the LED Board array.
#!/usr/bin/python
from gpiozero import LEDBoard
from time import sleep
Output_1 = 0
Output_2 = 1
Output_3 = 2
Output_4 = 3
Output_5 = 4
Output_6 = 5
Output_7 = 6
Output_8 = 7
Output_9 = 8
Output_10 = 9
Output_11 = 10
Output_12 = 11
outputs = LEDBoard(4,5,6,7,8,9,10,11,12,13,14,15)
outputs.off() # turn off all LEDs
while True:
outputs.on(Output_1)
sleep(1)
outputs.off(Output_1)
sleep(1)