Navigation Menu+

MCP23S17 – Hook Up Guide

Introduction

The MCP23S17 is a SPI Based Port Expander which can add 16 more digital I/O pins to your Arduino or Raspberry Pi. Thanks to the wide operating voltage of 1.8 – 5.5V,  you can add 5V I/O pins to the Raspberry Pi or 3.3V based Arduino thereby avoiding a level shifter and simplifying connections.

Hardware Required:

MCP23S17 Port Expander

MCP23S17 Port Expander

Breadboard

Breadboard

Male - Female Jumper Wires

Jumper Wires

Board OverviewMCP23S17 Port Expander

The board consists of 2 Port Banks (A and B), a 3-way switch, power and SPI headers

3-Way switch is used to set the address of the Port Expander, the switch positioned towards ON sets the address as ‘0’. The image above sets the address as ‘7’

INT B Interrupt output for PortB

B.0 – B.7 Bidirectional I/O

INT A Interrupt output for PortA

A.0 – A.7 Bidirectional I/O

VCC Power supply pin for the MCP23S17, connect to a voltage source between 1.8V -5V

GND connect to ground of power supply

SO, SI, SCK & CS SPI Pins

RESET connect to ground to reset the port expander

Note: The silkscreen on Port A is reversed, meaning INT A actually is A.7 and A.7 actually is INT A

 

Enabling SPI on the Raspberry Pi

By default the SPI peripheral is not enabled, we cannot continue unless this is done first. Have a look at the tutorial by SparkFun on how to do this.

 

Interfacing the MCP23S17 Port Expander with the Raspberry Pi

Connection Diagram for the MCP23S17 to the Raspberry Pi 3

Interfacing the MCP23S17 to the Raspberry Pi

 

Connections from Raspberry Pi to MCP23S17 Port Expander

MCP23S17 PinRaspberry Pi GPIO Pin
VCC5V/3.3V
GNDGND
SOSPI_MISO / GPIO9 / Pin 21
SISPI_MOSI / GPIO10 / Pin 19
SCKSPI_CLK / GPIO11 / Pin 23
CSSPI_CE0_N / GPIO8 / Pin 24

Installing the Python Library

In order to get started the python library needs to be installed first. Run the following command in terminal to install the library

sudo pip install RPiMCP23S17

 

Sample Code

The below code will turn ON and OFF all the pins at an interval of 1 second.

from RPiMCP23S17.MCP23S17 import MCP23S17
import time

mcp = MCP23S17(bus=0x00, ce=0x00, deviceID=0x00)
mcp.open()

for x in range(0, 16):
    mcp.setDirection(x, mcp.DIR_OUTPUT)

print "Starting blinky on all pins (CTRL+C to quit)"
while (True):
    for x in range(0, 16):
        mcp.digitalWrite(x, MCP23S17.LEVEL_HIGH)
    time.sleep(1)
    for x in range(0, 16):
        mcp.digitalWrite(x, MCP23S17.LEVEL_LOW)
    time.sleep(1)

Further Reading

RPiMCP23S17 Github Repo

1 Comment

  1. Hi,
    I have a problem with Python 3.5
    Installed:

    root@Stretch:/# sudo pip install RPiMCP23S17
    Collecting RPiMCP23S17
    Downloading https://files.pythonhosted.org/packages/3d/ec/98155de7254f2f0ce7dc6cf2fa73620fa5492d28dcc7a1f84fca2409b75e/RPiMCP23S17-0.2.4.tar.gz
    Building wheels for collected packages: RPiMCP23S17
    Running setup.py bdist_wheel for RPiMCP23S17 … done
    Stored in directory: /root/.cache/pip/wheels/fe/db/b6/4364b206cecf872ec9ed83d52af350dde5368ae0c94b8f3970
    Successfully built RPiMCP23S17
    Installing collected packages: RPiMCP23S17
    Successfully installed RPiMCP23S17-0.2.4

    copied Python source code and run it , to test MCP23S17 but gave me the error:

    ============= RESTART: /home/pi/Documents/Test SPI MCP23S17.py ==============
    Traceback (most recent call last):
    File “/home/pi/Documents/Test SPI MCP23S17.py”, line 2, in
    from RPiMCP23S17 import MCP23S17
    ImportError: No module named ‘RPiMCP23S17’

    How can i solve this?

    Regards,
    Ronald.

Submit a Comment

Your email address will not be published. Required fields are marked *