Skip to main content

GPIO expander

What this example shows​

Set all 16 pins of the PCAL6416A GPIO expander as outputs and blink an LED on P1-5. The header pins P0-0 to P0-7 are pins 0 to 7 in code, and P1-0 to P1-7 are pins 8 to 15. Wire an LED with a 300 Ω resistor between P1-5 and GND on the GPIO EXPANDER header before you run it. This value comes from this example; the Arduino GPIO expander sketch asks for a 330 Ω resistor on P1-7 instead.

Example code​

"""Use the GPIO expander's pins to blink an LED."""
# This example shows you how to use the GPIO expander's pins
# See below which pins are available

# Include needed libraries
import time
from pcal6416a import *
from inkplate7spectra import Inkplate

# Create Inkplate object
display = Inkplate()

# Initialize the display, needs to be called only once
display.begin()

# pin = display.gpio_expander_pin(pin,mode)
# Supported modes: mode_input, mode_input_pullup, mode_input_pulldown, mode_output
# Supported pins on Inkplate 7SPECTRA are listed below

expander_p0_0 = display.gpio_expander_pin(0, mode_output)
expander_p0_1 = display.gpio_expander_pin(1, mode_output)
expander_p0_2 = display.gpio_expander_pin(2, mode_output)
expander_p0_3 = display.gpio_expander_pin(3, mode_output)
expander_p0_4 = display.gpio_expander_pin(4, mode_output)
expander_p0_5 = display.gpio_expander_pin(5, mode_output)
expander_p0_6 = display.gpio_expander_pin(6, mode_output)
expander_p0_7 = display.gpio_expander_pin(7, mode_output)

expander_p1_0 = display.gpio_expander_pin(8, mode_output)
expander_p1_1 = display.gpio_expander_pin(9, mode_output)
expander_p1_2 = display.gpio_expander_pin(10, mode_output)
expander_p1_3 = display.gpio_expander_pin(11, mode_output)
expander_p1_4 = display.gpio_expander_pin(12, mode_output)
expander_p1_5 = display.gpio_expander_pin(13, mode_output)
expander_p1_6 = display.gpio_expander_pin(14, mode_output)
expander_p1_7 = display.gpio_expander_pin(15, mode_output)

# Take the previously declared pin 1_5 and blink it
# To see the blinking, attatch a 300Ohm resistor and LED between that pin and GND
while 1:
expander_p1_5.digital_write(1)
time.sleep(0.5)
expander_p1_5.digital_write(0)
time.sleep(0.5)
# Infinite loop, this goes on forever

Functions used​

Inkplate()returns Inkplate

Creates the display object from the inkplate7spectra module.

ReturnsThe display object.

display.begin()returns bool

Sets up the board, including the expander at I2C address 0x20. It sets all 16 expander pins to outputs driven low, for low power.

ReturnsTrue.

display.gpio_expander_pin(pin, mode)returns GpioPin

Sets an expander pin's mode and returns an object for it. Modes from the pcal6416a module: mode_input, mode_output, mode_input_pullup, mode_input_pulldown.

ReturnsThe pin object.

expander_p1_5.digital_write(value)returns None

Drives the pin high (1) or low (0). digital_read() reads it.

ReturnsNothing.

What to expect​

The LED on P1-5 blinks: half a second on, half a second off. The screen does not change, and nothing is printed in the shell.

The modules use P1-1 for the battery measurement and P1-2 for the microSD card's supply, and this example sets them as outputs too. Leave those two pins free; see Jumpers.

🔗Open the complete example

examples/inkplate7spectra/gpio_expander.py

→