PI-SPI-8DI Python Code Samples

8 Channel Isolated Digital Input Module

The PI-SPI-8DI is an 8 channel Isolated Digital Input Module that communicates with the Raspberry Pi via the SPI bus.

This module has 2 chip selects:

Default Chip Select is GPIO17
Alternate Chip Select is GPIO22

The chip select operation during SPI communication is handled directly by the libwidgetlords library and kernel.

To run the sample Python code examples, please install the Widgetlords libwidgetlords libraries as explained in:

Getting Started with PI-SPI Libraries

NOTE: All of the sample code examples are for Python 3.

Please ensure you have the dtoverlay set correctly in the /boot/config.txt file:

dtoverlay=pi-spi

Sample 1 - Basic Read Digital Input

In this code example:

1. We import the Widgetlords library
2. Initialize the library
3. Read all the inputs as 1 byte
4. Print reading to the shell window

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8DI()

while True:
    print(inputs.read())
    sleep(0.5)

Sample 2 - Read Single Digital Input

This code example will read single digital input where 0 is D1 and 7 is D8

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8DI()

while True:
    print(inputs.read_single(0))
    sleep(0.5)

 

Sample 3 - Read Digital Inputs from Different Address

The PI-SPI-8DI uses the Microchip MCP23S08 GPIO expander, which has 4 different address selections. The default address is 0 where the jumpers labelled J3-A0 and J3-A1 are installed. Up to 4 modules or 32 isolated digital inputs can be read simultaneously with 1 chip select.

The default is address 0:

inputs = Mod8DI()

To change the addressing to address 1 J3-A0 removed and J3-A1 installed, the address is selected as shown:

inputs = Mod8DI(1)

This code example will read the digital inputs from Address 1

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8DI(1)

while True:
    print(inputs.read())
    sleep(0.5)

 

Sample 4 - Read Digital Inputs from Alternate Chip Select

Every Pi-SPI module has a dedicated GPIO pin for it's SPI Chip Select and an Alternate chip select to allow two modules of the same type to be used together.

This code example shows how to enable the alternate chip select. NOTE: this works for all following code examples as well!

Each module type has the Chip Select defaulted to it's unique GPIO Pin as follows:

PI-SPI-8AI = GPIO7 
PI-SPI-8KO = GPIO8
PI-SPI-8DI = GPIO17
PI-SPI-2AO = GPIO4

For each module, the alternate Chip Select is GPIO22

The module initialization line for the default Chip Select is

inputs = Mod8DI()

OR

inputs = Mod8DI(False)

To use the alternate chip select, we also have to specify the address as described. This example uses the address 0 with both jumper J3-A0 and J3-A1 installed:

inputs = Mod8DI(0,True)

Here is the complete sample code example using the alternate chip select:

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8DI(0,True)

while True:
    print(inputs.read())
    sleep(0.5)