PI-SPI-DIN-4AO Python Code Samples

4 Channel Analog Output 4-20mA Module

The PI-SPI-DIN-4AO is a 4 channel Analog Output 4-20 mA Module that communicates with the Raspberry Pi via the I2C bus.

The PI-SPI-DIN-4AO has just been upgraded to provide Jumper Selection for:

4-20 mA , 0 to 5VDC and 0 to 10 VDC Outputs.

PI-SPI-DIN-4AO Analog Output 4-20mA, 5VDC, 10VDC

Here is the Jumper Selection for the various outputs:

PI-SPI-DIN-4AO Analog Output 4-20mA, 5VDC, 10VDC

This module has a base address of 0x60 which 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:


Sample 1 - Write 4 Channels of 4-20 mA Output

In this code example:

1. We import the Widgetlords library
2. Initialize the library
3. Write 4 Analog Output Channels where 800 DA Counts = 4 mA and 4000 DA Counts = 20 mA

from time import sleep
from widgetlords.pi_spi_din import *

outputs = Mod4AO()

while True:
    outputs.write_single(0, 800)
    outputs.write_single(2, 800)
    outputs.write_single(1, 4000)
    outputs.write_single(3, 4000)

    outputs.write_single(0, 4000)
    outputs.write_single(2, 4000)
    outputs.write_single(1, 800)
    outputs.write_single(3, 800)

Multiple Analog Output Modules - Up to 32 Analog 4-20 mA Outputs

The PI-SPI-DIN-4AO uses a Digital to Analog Converter that has 8 programmable addresses: 0x60 thru 0x67. The libwidgetlords version 2.0 library sample code refers to the addresses as 0 thru 7.

Using the libwidgetlords version 2.0 (or higher) library, each PI-SPI-DIN-4AO module is easy to have it's address re-programmed.

Here is the block diagram of the PI-SPI-DIN-4AO Module: