PI-SPI-DIN-8DI Python Code Samples

8 Channel Isolated Digital Input Module

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

This module has 5 chip selects: CE0 thru CE4

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=vpe-2701c

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 using Chip Select CE2
4. Print reading to the shell window

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8DI(ChipEnable.CE2)

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_din import *

init()
inputs = Mod8DI(ChipEnable.CE2)

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(ChipEnable.CE2)

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

inputs = Mod8DI(ChipEnable.CE2,1)

This code example will read the digital inputs from Address 1

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8DI(ChipEnable.CE2,1)

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

 

Sample 4 - Read Digital Inputs from Alternate Chip Select

Every Pi-SPI-DIN module has 5 Chip Selects allowing up to 5 different modules 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 Selects defaulted to the following GPIO Pins:

CE4 = GPIO18 
CE3 = GPIO23
CE2 = GPIO24
CE0 = GPIO8
CE1 = GPIO7 

Here is the complete sample code example using CE3 as the Chip Select

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8DI(ChipEnable.CE3)

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