How to expand inputs and outputs using MCP23S17

Manufacturers P/N: MCP23S17
Cost: $1.65 from Digikey.com
Description: 16-bit I/O expander w/ Serial Interface
Datasheet: MCP23S17

Sometimes using small microcontroller chips (to save money!) offers a limited number of inputs and outputs. Especially when using the Picaxe family of chips, the inputs and outputs are sometimes just not enough. There is an unfortunate event that happens with the Picaxe 08, 08M, 14M, 20M, 18, 18A, 28A chips: not enough GOSUBs are allowed. The Picaxe 18X, 28X family, and 40X family has the memory and GOSUBs available to handle this expansion. This tutorial uses sub procedures for the serial communications. The X1 and X2 parts have SHIFTOUT and SHIFTIN capabilities that saves a lot of memory. I have done some experimenting, and I have taken a poorly written program (143 bytes) to a better one (123 bytes). This information is provided, but the actual example program will demonstrate a little more and will be much larger.

The datasheet for this chip is a little unclear, but with a little experimentation I have found the most useful information. Most people will use this chip to just expand their inputs and outputs. The other features that come with this chip are there if you need them. The only other feature that is somewhat useful is the interrupt feature. This is nice to have, but my programs usually constantly read for inputs, so an interrupt is not so important to me. Others may have a design the requires the interrupt feature.

Short tutorial: Download the schematic below and then the program. Wire up the circuit as the schematic. Download program to microcontroller. The program itself has many comments to help you sort out a little about what is happening.

MCP23S17 demonstrative program
MCP23S17 wiring schematic for this experiment

Step by Step Tutorial

  1. First things first. Please gather the following items from your parts drawer: MCP23S17 IC, 6min to 13max - 1k resistors, 1min to 8max - LEDs, 4.7k resistor, 8 - 10k resistors, 2 – SPST switches, 9v battery and 7805 +5v voltage regulator or 3 – 1.5volt batteries, solderless breadboard, wire, and your favorite Picaxe chip and protoboard (Preferably one of the 3 types listed above).
  2. Place your MCP23S17 IC into your solderless breadboard. Connect pin 10 to ground and pin 9 to +5v.
  3. Connect a 10k resistor from pins 1, 3, 5-8 directly to ground. For simplicity these pins will all simulate a low state switch.
  4. Connect a 1k resistor from pins 2 and 4. Connect the other end of the 1k resistor to an empty row on your breadboard. Now connect a 10k resistor from the 1k resistors. Connect the other end of the 10k resistors to ground. Take your switches. (If you are using switches that need to be soldered then solder 2 wires to each of them and then continue. If you have switches that can be placed in the breadbord then this will be a little easier.) Connect one end of the switch to the junction of the 1k and 10k resistor. Repeat this for the other resistor. Connect the other end of the switches to +5 volts.
  5. Connect a 1k resistor to pins 15 – 17. Connect the other end of the resistors to ground. According to the datasheet, these are the hardware addressing pins. In the demonstrative program these have been disabled, but they must still be connected to ground. If you enable hardware addressing as the datasheet describes, you can connect the pins to ground or +5 volts in any combination. This is helpful if you connect more than one MCP23S17 chips together. (The hardware address in the example is 0100000. If you connected pin 15 to +5 volts the address would then be 0100001.)
  6. Connect a 4.7k resistor to pin 18 and connect the other end to +5v. This is a reset switch that is activated when it is in the low (ground) state. The chip will not work if you leave this floating (unconnected).
  7. Now to the outputs. Pins 19 – 28 are I/O, but in the example I have made them O(utputs). You really only need to connect an LED to one of these to see what is happening, but some of you might have plenty of spare LEDs and resistors, and you might light big light shows. If so, then connect all the LEDs, or whatever you might want to use, to these outputs. Connect a 1k resistor to any outputs that you would like to use. Place the other end of each resistor into an empty row in your solderless breadboard. Connect the long end, anode, of the LED to each 1k resistor, and then connect the other end to ground.
  8. Final connections! Connect pin 11 – 13 to output pins on your Picaxe chip. Connect pin 14 to an input pin on your Picaxe. I connected pin 11 to output 2 (latch or CS), pin 12 to output 0 (clock), and pin 13 to output 1 (data in to MCP23S17). Then I connected pin 14 to input 1 (data out from MCP23S17). This is reflected in the SYMBOL area of the demonstrative program. Any deviations from this should be changed in the program to your setup.
  9. Download the program to your Picaxe chip, and things should take off. The DEBUG window should appear. Press switches and the variable DATAIN1 should change. If you press both switches as connected per the schematic DATAIN should be 00001010. I connected things a little different from what standards suggest, but you can see how my debug turned out. DATAIN in all zeros because that is what is read from the output states. This is the reason that I made GPIOA all outputs and GPIOB all inputs to cut down on some initial confusion. Reading from output pins will give you the current state of that output.

My tiny breadboard was crowded.
Debug of my switches (1 high on b3) I did have nice labels for each variable, but for some reason it didn’t show up in this screenshot. b3 is the same as DATAIN1.


This entry was posted in Electronics, Product Reviews and tagged , , , by robbie. Bookmark the permalink.

About robbie

I am an electronics enthusiest and a ham radio operator (W1RCP). I like to play with electronics. It's fun and educational. I looked forward to working in the engineering field in the future. I have a BS in Electronics Engineering Technology from DeVry University. I also have an Associate's degree in Marketing Management from Moultrie Tech, and a diploma in Electronics from MTC.

Comments are closed.