Pi-SPi Series Python Sample Code

Updated March 2018

The Pi-SPi Series of building blocks for the Raspberry Pi now have sample test code written in Python available for download.

Each module has test code written in Python and tested on the RPi 3, using the SPI output. Each Module has a dedicated Chip Select line, with an alternate Chip Select for one extra module.

The new Python libraries for the PI-SPI and Pi-SPI-DIN series of modules take care of the SPI chip selects and make writing Python code as easy as using "Read" and "Write" commands as shown below.

To get started, please visit:

Getting Started with PI-SPI Libraries

and follow the instructions for installation.

PI-SPI-8AI Python Code Samples

8 analog inputs, 8 x 4-20mA, MCP3208 AD Converter

Basic "Read" A/D counts program:

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8AI()

while True:
    print(inputs.read_single(0)) # Read Input 1 AD Counts
    sleep(0.5)

 

Pi-SPi-2AO Python Code Samples

2 analog 4-20mA Outputs, 2 x 0-10 VDC Output, MCP4922 Dual DA Convert

Basic "Write" D/A Counts program:

from time import sleep
from widgetlords.pi_spi import *

init()
outputs = Mod2AO()

while True:
    outputs.write_single(0, 745)   # Output 1 = 4 mA
    outputs.write_single(1, 3723)  # Output 2 = 20 mA
    sleep(2)
   

 

Pi-SPi-8DI Python Code Samples

8 x Isolated Digital input, MCP23S08 IO Expander.

Basic "Read" Digital Inputs program:

from time import sleep
from widgetlords.pi_spi import *

init()
inputs = Mod8DI()

while True:
    print(inputs.read())  # Read all Inputs as a byte
    sleep(0.5)

 

Pi-SPi-8KO Python Code Samples

2 x 10A SPDT Relay, 6 Signal Level Outputs, 74HC595 Serial Shift Register

Basic "Write" Relay Outputs program:


from time import sleep
from widgetlords.pi_spi import *

init()
relays = Mod8KO()

while True:
    relays.write(0x01)  # Relay 1 ON
    sleep(1)

 


Leave a comment

Please note, comments must be approved before they are published