Arduino port expander: If you run out of I/O pins you can either
choose a different processor with more pins, or you can choose an I/O
expander that uses a few of your pins, in exchange for a lot more!
To minimise the number of sacrificial pins a serial interface is employed with at least a clock and data line. There are two interfaces that are typically used:
Of the two, I2C is the most sophisticated, allowing master and
subordinate chips that communicate in both directions over the data bus (it requires 2 pins),
whereas SPI is a more primitive interface that requires more pins (3 or
4) but is far faster.
The interesting thing is that chips that have been around for decades
can make use of the SPI interface (because it defines a primitive clock
and data scheme). So nearly any serial chip is usable with this
interface.
The MCP23017 is
the all-singing-all-dancing Arduino port expander with 16 I/O pins and it gives you
virtually identical pin capability to the existing pins of a
microcontroller. They can be individually set to input or output and you
can enable an internal pullup (also individually).
By simply setting A0 to A2 you can have up to 8 of these devices on a
single I2C bus giving you a total I/O pin count of 128 pins!
Additionally there is an interrupt capability for maximum flexibility.
Note: One potential problem is that the chip has a lot of pins
meaning it is physically big and the DIP package has 28 pins. A better
option may be the PCF8574 that only has 8 outputs and 16 pins.
The
PCF8574 has 8 I/O bits and comes in a 16 pin DIP package (as well as
other packages). Again like the the MCP23017 the pins are input or
output capable. However they do not have internal pullup capability.
You must add your own external pullups where necessary on input pins.
The chip also has three address inputs so 8 devices can be placed on one I2C bus giving a total of 64 I/O pins on one bus.
Additionally the PCF8574 has an interrupt capability.
Note: This is actually a far less complex device (and probably less
capable but easier to use) as there are no internal registers! You
simply write/read to/from output registers after writing an I2C
address (with R/W bit).
The simplicity of the PCF8574 may have drawbacks compared to the
MCP23017 specifically when using interrupts. Interrupts fire on any
rising or falling edge of port inputs - there are no internal registers so you can't mask out an unwanted interrupt firing as you can with the MCP23017.
Note: Because there are no internal registers, and you therefore do
not have a pin direction register, the I/O pin is defined as
quasi-bidirectional! The datasheet claims the pin can be either an input or an
output at the same time!
Interestingly you can buy a version of the MCP23017 that specifically uses the SPI interface labelled MCP23S17, with identical pinout to the I2C version (all except the serial interface pin labels).
The maximum speed of the MCP23017 is 1.7MHz whereas for the SPI version, it
is 10MHz! You get all the complex functionality of the I2C version but
faster operation. However its more difficult to add multiple devices on
the same SPI bus (not impossible though).
Note: The MCP23S17
is identical to the MCP23017 except for the serial interface. They use
the same datasheet so the link above leads to the MCP23017 as they
operate in the same way.
A popular serial to parallel chip is the 74HC595
which has 8 outputs. It requires a serial clock, serial data input, and a latch clock and
possibly a chip enable i.e. an SPI interface.
This is a very cheap
chip that gives digital outputs (no pullups or interrupts) but you can
daisy chain lots of them together to get a lot of output pins all over a
board.
An example of a parallel to serial chip is the 74HC165 this has 8 digital inputs and uses a serial interface to allow retrieval of the data bits. Again the interface is compatible with the SPI interface.
The external input pins are D0~D7 while PLn loads data from those pins into the shift register.
To shift out the data CEn is held,low while CP (clock is pulsed).
You only need to worry about
DS and Q7 (or Q7n) if you are daisy chaining lots of 74HC165s together;
these allow the shift out bit (Q7) to become the data input to the
next 74HC165 forming a serial register chain.
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.