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 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)

Example 4: Using inputs with Button

Similarly, you can use the Button class to treat an input like a button. Remember to set the pull_up parameter to None.

#!/usr/bin/python

from gpiozero import LEDBoard, Button
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)

Input_1 = Button(16,pull_up=None,bounce_time=0.5)
Input_2 = Button(17,pull_up=None,bounce_time=0.5)
Input_3 = Button(18,pull_up=None,bounce_time=0.5)
Input_4 = Button(19,pull_up=None,bounce_time=0.5)
Input_5 = Button(20,pull_up=None,bounce_time=0.5)
Input_6 = Button(21,pull_up=None,bounce_time=0.5)
Input_7 = Button(22,pull_up=None,bounce_time=0.5)
Input_8 = Button(23,pull_up=None,bounce_time=0.5)
Input_9 = Button(24,pull_up=None,bounce_time=0.5)
Input_10 = Button(25,pull_up=None,bounce_time=0.5)
Input_11 = Button(26,pull_up=None,bounce_time=0.5)
Input_12 = Button(27,pull_up=None,bounce_time=0.5)

outputs.off()      # turn off all LEDs

print(Input_1.is_pressed)
print(Input_1.value)

Input_1.wait_for_press()

print(Input_1.is_pressed)
print(Input_1.value)

while True:
    outputs.on(Output_1)
    sleep(1)
    outputs.off(Output_1)
    sleep(1)

Example 5: Using inputs with ButtonBoard

Like in the case of LEDBoard, you can use the ButtonBoard class to group all inputs, and then optionally assign names to each of them.

#!/usr/bin/python

from gpiozero import LEDBoard, ButtonBoard
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

Input_1 = 0
Input_2 = 1
Input_3 = 2
Input_4 = 3
Input_5 = 4
Input_6 = 5
Input_7 = 6
Input_8 = 7
Input_9 = 8
Input_10 = 9
Input_11 = 10
Input_12 = 11

outputs = LEDBoard(4,5,6,7,8,9,10,11,12,13,14,15)
inputs = ButtonBoard(16,17,18,19,20,21,22,23,24,25,26,27,pull_up=False)

outputs.off()      # turn off all LEDs

while not (inputs.is_pressed) :
    sleep(1)

while True:
    outputs.on(Output_1)
    sleep(1)
    outputs.off(Output_1)
    sleep(1)