Introduction: NODEMCU LUA ESP8266 With MCP23017 16 Bit Port Expander IC
The MCP23017 is a relatively easy IC to make a board for as it has Port A and B on either side of the chip and the pins are in sequential order.
Similarly the I2C address bus is all together as well.
There are 2 pins on this IC which are unused as it is comparable to the MCP23S17 which uses an SPI interface where these pins are used.
A datasheet for this IC is available from Microchip.
Step 1: Circuit Diagram
The circuit diagram shows how to connect the IC to an ESP8266
NOTE: The IC runs between 2.7 and 5.5 Volts.
Pin Connections on my board
- Pin 9 (VDD) to 3v3
- Pin 18 (RESET) to 3v3
- Pin 17 (A2) to GND
- Pin 16 (A1) to GND
- Pin 15 (A0) to GND
- Pin 14 (NC) to GND (Not necessary)
- Pin 13 (SDA) to ESP GPIO0
- Pin 12 (SCL) to ESP GPIO2
- Pin 11 (NC) to GND (Not necessary)
- Pin 10 (VSS) to GND
Step 2: Making the Main Board
The board is straight forward to make as explained above, everything is of a simple layout.
My main board took little time to make and is shown above.
You can of course build this circuit on a breadboard.
Step 3: Additional Boards
I have made up some extra 8 bit boards which can easily be plugged into different project boards.
The first board is connected to a 7 segment LED display and is connected pin 1 to segment a, pin 2 to b etc. There is a small resistor (about 55 ohm) to protect the LEDs.
The second one is an 8 switch bank all wired together and can be connected to 3.3V or ground. I have not included any pull resistors as the MCP23017 has them inbuilt.
The third one is from an Ebay kit, it consists of 8 LEDs and a resistor array with a connection to Gnd. I also have the same board but installed the LEDs the opposite way around so that it connects to 3.3V or 5V instead of Gnd. On Ebay they are known as an 8 Channel Flowing Water Light LED DIY Kit, 99p from China.
Step 4: Programming
I have written the programs to work with an ESP01 as this has only 2 I/O pins. It can of course be used with any of the ESP8266 boards. The SDA & SCL pins can be allocated to any pin between 1 and 12.
If using an older version of LUA (e.g. NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4) the I2C is already installed. Otherwise you need to ensure that the I2C module is included in your build.
I have included 3 simple Lua programs to show how the IC can be used.
7Segment.lua drives a LED display and sequences between the numbers 1 to 0.
KittCar.lua drives the 8 LED board to simulate the famous car from the 80s.
Reader.lua reads from Port B.
Step 5: Which Pin?
An extra program I came up with while tinkering.
It uses port B as an input and port A as an output. The photo shows DIP switches, but you can connect one of the port B pins to Gnd and the LED display will show which pin is connected.
NOTE: It only works with 1 pin at a time!
Step 6: Conclusion
There are of course other I/O expanders available. Some are 8 bit, 16 bit and even 24 bit! All work in a similar way to the MCP23017, but this IC is very cheap for its capabilities and can be sourced for around 10p each from China.
I have not used all the features of this IC as there are interrupts also available the can be put to use. Reading the datasheet tells all about the different registers and ways the IC can be used.
It is possible to have 8 of these devices on the same I2C bus giving 128 I/O ports all controlled by 2 lines. Think of the possibilities there!
We have a be nice policy.
Please be positive and constructive.