Expanding the GPIO on the Raspberry Pi

Although the single board computer the Raspberry Pi or RBPi has nearly 26 GPIO pins in its earlier models and 40 in its latest, there are times when the project demands more of them. In such cases, a GPIO expander is the only solution, and the MCP23S08, a device that Microchip Technology makes provides an easy way to expand the IO pins of a micro-controller using only a 2-wire serial interface.

The MCP23S08 works with the I2C protocol as a slave device, providing 8-bit, general purpose, bi-directional IO expansion for the I2C bus. It supports a 7-bit slave addressing, with the control byte acting as the read/write bit. Of the slave address, the MCP23S08 fixes the four most significant bits to 0100. This leaves the remaining three bits to be defined by the user as the address bits. Therefore, one can connect up to eight MCP23S08 devices on a common I2C bus at any one time.

It is possible to configure individual bits of the 8-bit GPIO port as either input or output. At the same time, it is also possible to enable the internal pull-up resistor on the port pins to interrupt-on-change. A set of configuration and control registers control these operations. Each resistor has its own address and its power-on reset value, as listed on the datasheet of the MCP23S08.

The first register is the IO direction register, and controls the direction of the data IO. On setting a bit in this register, its corresponding pin assumes an input direction, and if the bit is clear, its corresponding pin works like an output.

With the input polarity register, the user can configure the polarity of the corresponding GPIO port bits. When he/she sets a bit on this register, the corresponding GPIO register bit stores the inverted value present on that pin.

Interrupt-on-change control register controls this feature for each pin. When the user sets a bit on this register, the corresponding pin becomes capable of interrupt-on-change. Of course, for enabling this feature, the user must also configure the INTCON and DEFVAL registers as well.

Interrupt control or INTCON register controls the manner in which the associated pin value compares for the interrupt-on-change feature. If the user sets the bit, MCP23S08 compares the corresponding IO pin against the associated bit in the DEFVAL register. If clear, the MCP23008 compares the corresponding IO pin to its previous value.

The default comparison value or DEFVAL register is for configuring the default comparison value. If the user enables a bit in this register, and the associated pin has an opposite value, it will cause an interrupt.

The PI-SPI-DIN series of IO modules has the RBPi reading 2 modules of eight isolated digital inputs for a total of sixteen inputs and controlling four modules of four relay outputs for a total of 16 relays, all using the IO expander chip MCP23S08.

As the RBPi can select only two chip-select lines, each of the PI-SPI-DIN modules has a jumper selection of five chip selects. The two address lines of the MCP23S08 need to be enabled in the setup routine while determining the port pins as inputs or outputs.