PI-SPI-DIN-8AI Python Code Samples

8 Channel Analog Input 4-20 mA Module

The PI-SPI-DIN-8AI is an 8 channel Analog 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 AD Counts

In this code example:

1. We import the Widgetlords library
2. Initialize the library
3. Read a single Channel ( Channel 0 is Input A1 on the PI-SPI-DIN-8AI Module) using Chip Select CE0
4. Print reading to the shell window

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8AI(ChipEnable.CE0)

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

Sample 2 - Read AD Counts 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 CE1 as the Chip Select

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8AI(ChipEnable.CE1)

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

Sample 3 - Multiple Channel A/D Count Readings

This reads multiple channels 0 thru 7 (A1 thru A8) on the module and prints them to the shell window.

from time import sleep
from widgetlords.pi_spi_din import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    A2 = inputs.read_single(1)
    A3 = inputs.read_single(2)
    A4 = inputs.read_single(3)
    A5 = inputs.read_single(4)
    A6 = inputs.read_single(5)
    A7 = inputs.read_single(6)
    A8 = inputs.read_single(7)

    print(A1)
    print(A2)
    print(A3)
    print(A4)
    print(A5)
    print(A6)
    print(A7)
    print(A8)
    print("")    
    sleep(0.5)

Sample 4 - Format A/D Reading to mA or VDC

Instead of reading just A/D counts, it may be more meaningful to display the actual reading in mA, Voltage or Temperature. By adding one more library feature, the format function is available for the user:

counts_to_value(reading_ad_counts, zero_ad_counts, span_ad_counts, zero_value, span_value )

where:

reading_ad_counts = actual A/D counts from the channel
zero_ad_counts = A/D counts to represent the Zero Value
span_ad_counts = A/D counts to represent the Span Value
zero_value = Zero Value ie: 0 mA or 4 mA
span_value = Span Value ie: 20 mA

Here is the sample code to read channel 0 (A1) in mA:
(Standard Python number formatting to limit decimal places to 2 places)

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    print("Input 1 = %4d AD Counts, %0.2f mA" % (A1, counts_to_value(A1, 745, 3723, 4, 20 )))
    print("")
    sleep(0.5)

Here is the sample code to read channel 0 (A1) in VDC:
(Standard Python number formatting to limit decimal places to 2 places)
NOTE: Range is from 0 VDC to 6.6 VDC

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    print("Input 1 = %4d AD Counts, %0.2f VDC" % (A1, counts_to_value(A1, 0, 4095, 0, 6.6 )))
    print("")
    sleep(0.5)

 

Sample 5 - Format A/D Reading to Temperature Using Steinhartt-Hart Equation

The PI-SPI-8AI-TEMP module has 8 channels of Thermistor inputs for 8 channels of temperature measurement. Each channel fo the PI-SPI-8AI can be configured as either 4-20 mA, 0 to 6.6 VDC or Thermistor Input.

Here is the format to convert A/D counts to Degree C using the Steinhart-Hart Equation:

Temp = steinhart_hart(r_temp, beta, max_ad_counts, reading_ad_counts)

where:

Temp = the formatted reading as Deg C
r_temp = the Thermistor Ohms at Room Temperature (25 Deg.C)
beta = the Beat value of the Thermistor being used
max_ad_counts = the maximum ad counts ie: 4095 for a 12 bit A/D converter
reading_ad_counts = the actual A/D counts read.

In our products, we use a Cantherm thermistor with a 10K Ohm at 25 Deg C, 3380 Beta and our load resistor is 10K Ohms (fixed value)

Here is the sample code to convert channel 0 (A1) to a Temperature reading:

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    Temp = steinhart_hart(10000, 3380, 4095, A1)
    print(Temp)
    print("")
    sleep(0.5)

 

Sample 6 - PI-SPI-DIN-8AI-20MA Sample Code (8 Channels 4-20 mA)

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    A2 = inputs.read_single(1)
    A3 = inputs.read_single(2)
    A4 = inputs.read_single(3)
    A5 = inputs.read_single(4)
    A6 = inputs.read_single(5)
    A7 = inputs.read_single(6)
    A8 = inputs.read_single(7)

    print("Input 1 = %4d AD Counts, %0.2f mA" % (A1, counts_to_value(A1, 745, 3723, 4, 20 )))
    print("Input 2 = %4d AD Counts, %0.2f mA" % (A2, counts_to_value(A2, 745, 3723, 4, 20 )))
    print("Input 3 = %4d AD Counts, %0.2f mA" % (A3, counts_to_value(A3, 745, 3723, 4, 20 )))
    print("Input 4 = %4d AD Counts, %0.2f mA" % (A4, counts_to_value(A4, 745, 3723, 4, 20 )))
    print("Input 5 = %4d AD Counts, %0.2f mA" % (A5, counts_to_value(A5, 745, 3723, 4, 20 )))
    print("Input 6 = %4d AD Counts, %0.2f mA" % (A6, counts_to_value(A6, 745, 3723, 4, 20 )))
    print("Input 7 = %4d AD Counts, %0.2f mA" % (A7, counts_to_value(A7, 745, 3723, 4, 20 )))
    print("Input 8 = %4d AD Counts, %0.2f mA" % (A8, counts_to_value(A8, 745, 3723, 4, 20 )))

    print("")
    sleep(0.5)
 
 

Sample 7 - PI-SPI-8AI-5VDC Sample Code (8 Channels 0 to 6.6 VDC Input)

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()

inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    A2 = inputs.read_single(1)
    A3 = inputs.read_single(2)
    A4 = inputs.read_single(3)
    A5 = inputs.read_single(4)
    A6 = inputs.read_single(5)
    A7 = inputs.read_single(6)
    A8 = inputs.read_single(7)

    print("Input 1 = %4d AD Counts, %0.2f VDC" % (A1, counts_to_value(A1, 0, 4095, 0, 6.6 )))
    print("Input 2 = %4d AD Counts, %0.2f VDC" % (A2, counts_to_value(A2, 0, 4095, 0, 6.6 )))
    print("Input 3 = %4d AD Counts, %0.2f VDC" % (A3, counts_to_value(A3, 0, 4095, 0, 6.6 )))
    print("Input 4 = %4d AD Counts, %0.2f VDC" % (A4, counts_to_value(A4, 0, 4095, 0, 6.6 )))   
    print("Input 5 = %4d AD Counts, %0.2f VDC" % (A5, counts_to_value(A5, 0, 4095, 0, 6.6 )))
    print("Input 6 = %4d AD Counts, %0.2f VDC" % (A6, counts_to_value(A6, 0, 4095, 0, 6.6 )))
    print("Input 7 = %4d AD Counts, %0.2f VDC" % (A7, counts_to_value(A7, 0, 4095, 0, 6.6 )))
    print("Input 8 = %4d AD Counts, %0.2f VDC" % (A8, counts_to_value(A8, 0, 4095, 0, 6.6 )))

    print("")    
    sleep(0.5)

Sample 8 - PI-SPI-DIN-8AI-10VDC Sample Code (8 Channels 0 to 10 VDC Input)

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()

inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    A2 = inputs.read_single(1)
    A3 = inputs.read_single(2)
    A4 = inputs.read_single(3)
    A5 = inputs.read_single(4)
    A6 = inputs.read_single(5)
    A7 = inputs.read_single(6)
    A8 = inputs.read_single(7)

    print("Input 1 = %4d AD Counts, %0.2f VDC" % (A1, counts_to_value(A1, 0, 3685, 0, 10 )))
    print("Input 2 = %4d AD Counts, %0.2f VDC" % (A2, counts_to_value(A2, 0, 3685, 0, 10 )))
    print("Input 3 = %4d AD Counts, %0.2f VDC" % (A3, counts_to_value(A3, 0, 3685, 0, 10 )))
    print("Input 4 = %4d AD Counts, %0.2f VDC" % (A4, counts_to_value(A4, 0, 3685, 0, 10 )))   
    print("Input 5 = %4d AD Counts, %0.2f VDC" % (A5, counts_to_value(A5, 0, 3685, 0, 10 )))
    print("Input 6 = %4d AD Counts, %0.2f VDC" % (A6, counts_to_value(A6, 0, 3685, 0, 10 )))
    print("Input 7 = %4d AD Counts, %0.2f VDC" % (A7, counts_to_value(A7, 0, 3685, 0, 10 )))
    print("Input 8 = %4d AD Counts, %0.2f VDC" % (A8, counts_to_value(A8, 0, 3685, 0, 10 )))

    print("")    
    sleep(0.5)

 

Sample 9 - PI-SPI-8AI-NTC10K Sample Code (8 Channels Thermistor Input)

from time import sleep
from widgetlords.pi_spi_din import *
from widgetlords import *

init()
inputs = Mod8AI(ChipEnable.CE1)

while True:
    A1 = inputs.read_single(0)
    A2 = inputs.read_single(1)
    A3 = inputs.read_single(2)
    A4 = inputs.read_single(3)
    A5 = inputs.read_single(4)
    A6 = inputs.read_single(5)
    A7 = inputs.read_single(6)
    A8 = inputs.read_single(7)

    Temp1 = steinhart_hart(10000, 3380, 4095, A1)
    Temp2 = steinhart_hart(10000, 3380, 4095, A2)
    Temp3 = steinhart_hart(10000, 3380, 4095, A3)
    Temp4 = steinhart_hart(10000, 3380, 4095, A4)
    Temp5 = steinhart_hart(10000, 3380, 4095, A5)
    Temp6 = steinhart_hart(10000, 3380, 4095, A6)
    Temp7 = steinhart_hart(10000, 3380, 4095, A7)
    Temp8 = steinhart_hart(10000, 3380, 4095, A8)

    if(A1>4050):# Test to see if Sensor is present
        print("Sensor Not present")
    else:
        print("Temp1 = %0.1f Deg C" % Temp1)

    sleep(0.5)