The MCP23008 is an 8-Bit I/O Expander with Serial Interface and operates between 1.8 and 5.5 volts, so is ideal for the ESP8266, Arduino, Raspberry Pi, PIC Chips and more.
If you have seen my other Instructable on the MCP23017, you might be wondering why I am also doing one for the MCP23008, as it is really a smaller version of the same thing.
The reasons are that its registers are different both in name and number and I would like to show how to use a ready made library module. Not everyone is adept when using NodeMCU lua, so I would like to show this aspect of programming as well as how to use the MCP23008.
The library and programs are available at github.com.
The datasheet for the MCP23008 is available from Microchip.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Wiring the MCP23008
The MCP23008 IC is of a simple layout and building a board is realtively easy for it. You can also set up the circuit on a bread board.
Pin Connections on my board
- Pin 18 (VDD) to 3v3
- Pin 9 (VSS) to GND
- Pin 7 (NC) to GND (Not necessary)
- Pin 1 (SCL) to ESP GPIO2
- Pin 2 (SDA) to ESP GPIO0
- Pin 5 (A0) to GND
- Pin 4 (A1) to GND
- Pin 3 (A2) to GND
- Pin 6 (RESET) to 3v3
Connect pins to be read to Gnd on Port A (pins 10 - 17)
Note: here all the address pins are connected to Gnd to use the MCP23008 at address 0x20 on the I2C address bus.
If you were using address 0x21 then A0 would be connected to 3.3V, with A1 & A2 connected to Gnd.
Similarly if using address 0x22 then A1 would have to be connected to 3.3V with A0 and A2 connected to Gnd.
Step 2: Constructing a Board
I used a 25 mm x 64 mm (9 rows x 25 holes) Vero strip board to construct my board. It is a bit tight, but as most of the pins you need are on either side of the IC, it is adequate for what it has to do.
I have used 8 pins and 8 header sockets for port A so that I can plug in my various modules as well as other wiring for different projects. I have added extra Gnd and 3.3V pins as I find there is always a shortage of these when connecting modules together.
Step 3: Using a Library Module
Library modules normally contain a selection of sub-routines, functions and variables that can be accessed by another program. The program itself does not run, but its functions can be accessed by the calling program. It means that you can have your sub-routines within the library and call them up whenever you need them, making a small calling program. It becomes more valuable if you have many different programs using the same sub-routines, you don't need to include the sub-routines in every program.
Note: the mcp23008.lua program needs to be loaded into the ESP8266 memory like the other programs.
I have included the github.com programs here as well as a simple program (test.lua) to show that the library is working.
There are at least 2 ways to include the library in your program.
require ("mcp23008") mcp23008.begin(0x0,gpio2,gpio0,i2c.SLOW)
mcp = require ("mcp23008") mcp.begin(0x0,gpio2,gpio0,i2c.SLOW)
Both of the above do the same thing, but the second method allows you to use your own variable instead of the program name.
Step 4: Kitt Car Program
I have included the KittCar23008.lua program and KittLib.lua which uses the library to show the differences in how to write programs this way. Both programs do the same thing.
The 8 LED plug in is available from Ebay as a kit and is known as an 8 Channel Flowing Water Light LED DIY Kit, 99p from China. You have to solder it yourself.
Note: If you have a problem trying to run the KittLib.lua program, try connecting the MCP23008 RESET pin to Gnd momentarily. I know this will give a direct short circuit (as it is connected to 3.3V) and you have to reset everything else. It also works by connecting to Gnd through a 10 ohm resistor, probably a better way to do it.
I also found that KittLib.lua would run with no problems after running test.lua (Don't ask me why?)
I have tried various ways to resolve the KittLib.lua program problem, but so far cannot find any logical reason as to why it crashes. Dose anyone have any ideas?
I haven't written all of these programs myself, so I am not able to say why there is a problem, although after looking at the coding, there does not seem to be anything obviously wrong.
Step 5: 7 Segment LED
As with the KittCar.lua program above, I have included a standalone and a library dependent program to drive a 7 segment LED display.
Again, both programs do the same thing, but demonstrate how to use a library module either written by yourself or someone else.
Step 6: Conclusion
I have tried to demonstrate how to use code modules within the Lua environment, and apply it to a specific IC at the same time.
Even though I have had a problem doing this, I think that there is enough to show how these modules work as well as demonstrating a real life application.