The PI-AIO and PI-DIO modules are connected to the Raspberry Pi on the I2C bus on pins GPIO2 (SDA) and GPIO3 (SCL). Therefore, the I2C stack, which is already integrated in the Raspberry OS, can be used for control. Let's take a look at how to do this in the Python programming language.

Assume you already have the Raspberry OS installed and working. There is no need to install any additional packages, everything you need is already in the basic setup.

We recommend reading the datasheet for both modules to see how it is wired.

First of all we need to set the GPIO2 and GPIO3 pins to I2C mode directly at boot. In the file /boot/firmware/config.txt just add the line:

dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3

This will create a new I2C bus in the system, probably number 22 (i2c-22), because there are already buses 20 and 21 in the system.

For the PI-DIO module, the MCP23008 circuit is reset using the GPIO4 pin. This is needed if the system fails after a reset, the outputs will be disabled in the safe state. if this were not the case, the outputs could still be on after a reset, which can be dangerous. From the command line this can be achieved as follows:

> gpioset 0 4=1

Poté můžeme zkusit detekovat obvody na sběrnici i2c-22 příkazem:

> gpiodetect 22

Zobrazit by se měly všechny detekované obvody, u nás tedy 0x20 u PI-DIO a 0x60 a 0x68 u PI-AIO. Číslo se může lišit podle nastavení jumperů na modulu.

PI-DIO

Now we will create a simple Python program. First we need to create an access to the I2C bus:

i2cbus = SMBus(I2CBUS)

Where I2CBUS is bus number, in out case 22.

Then initialize the inputs and outputs of the GPIO expander:

i2cbus.write_byte_data(I2CADDR, MCP_IODIR, DIN1 | DIN2 | DIN3 | DIN4 | DOF1 | DOF2)
I2CADDR is the I2C address of MCP23008 and MCP_IODIR is GPIO register address.

The DIN and DOUT Fault inputs can be read using the command:

val = i2cbus.read_byte_data(I2CADDR, MCP_GPIO)

Control of DOUT outputs we can do with:

i2cbus.write_byte_data(I2CADDR, MCP_GPIO, val)

Variable val we set bits 5 resp. 7 for DOUT1 resp. DOUT2.

PI-AIO

The PI-AIO module is similar. First we connect to the I2C bus:

i2cbus = SMBus(I2CBUS)

For the MCP3424 ADC, start the conversion by writing to the configuration register:

i2cbus.write_byte(I2CADDR, channel | MCP3424_RATE_15SPS_16B | MCP3424_GAIN_1 | MCP3424_RDY)

And then we read the result:

read = i2c_msg.read(I2CADDR, 3)
i2cbus.i2c_rdwr(read)
val1 = int.from_bytes(read.buf[0])
val2 = int.from_bytes(read.buf[1])
val = (val1 << 8) + val2

It is similar for the MCP47CMB22 DA converter. The output is set by writing to the register:

val1 = (val >> 8) & 0xff
val2 = val & 0xff
write = i2c_msg.write(I2CADDR, [channel, val1, val2])
i2cbus.i2c_rdwr(write)

The complete program

Three programs for controlling dio, ain and aout can serve as an example. These can be used to control inputs and outputs from the command line. Examples of use for PI-DIO:

python dio.py din1        # Read input 1
python dio.py dout1 1   # Enable output 1
python dio.py dout1 0   # Disable output 1
python dio.py dof1        # Read output 1 fault

For analog inputs and outputs (the program converts 0-10 V inputs and 0-5 V outputs):

ain 1        # Read analog input 1
aout 1 2.5 # Set analog output 1 to 2.5V

You can download programs here.

 

Cookies

We use cookies to deliver and enhance the quality of our services.