MCP23017 16-bit IO Expander

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

MCP23017 16-bit IO Expander

Post by mcauser » Tue Dec 24, 2019 11:39 pm

I created a library for the MCP23017 16-bit IO expander.
https://github.com/mcauser/micropython-mcp23017

Image

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MCP23017 16-bit IO Expander

Post by mcauser » Tue Dec 24, 2019 11:42 pm

Designed to work on my TinyPICO ESP32.
Might need some trimming to fit on an ESP8266.

It provides 3 interfaces for setting the registers.
Could be split up into separate versions and further optimised to fit on smaller boards.

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: MCP23017 16-bit IO Expander

Post by dentex » Tue Apr 07, 2020 5:15 pm

Hello!
I realized that this library doesn't work with my cheap ESP8266 (Lolin, nodeMCU whatever) board.

Do you have any hint for that "trim"?

Thanks.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MCP23017 16-bit IO Expander

Post by mcauser » Tue Apr 07, 2020 9:28 pm

Hi @dentex,

While creating the driver, I came up with 3 interfaces and I was going pick the most useful and maybe retire the others, or make alternate versions, so you can pick the one that works best for your needs.

1. The list interface, where each item represents a pin and has methods for reading input/setting output.
2. The method interface, where you have a single method for configuring the pin, and have to provide the pin number.
3. The property interface, where you can configure entire 8 or 16bit registers.

List was supposed to be for simplicity for people coming from Arduino. Configures one pin at a time.

Method combines all of the list methods as optional arguments and you need to give it a pin number. Lets you configure more features.

Property was so you could configure entire registers in one call. Where Method lets you configure all registers of a pin, Property lets you configure all pins of a register. I think it's cleaner, but you need to understand how the registers work.

Which interface / pattern would best suit your needs?

The mcp23008 is pretty much half of this mcp23017. As in, it only has 1 port. So I could make this a bit more generic and cover both ICs.

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: MCP23017 16-bit IO Expander

Post by dentex » Wed Apr 08, 2020 6:55 am

Thanks for the explanation!
Would have been great to arrive at the point to choose what method to use.
Unfortunately I can't define the mcp object to start with:

Code: Select all

mcp = mcp23017.MCP23017(i2c, 0x20) 
The above always fails with a:

Code: Select all

AttributeError: 'module' object has no attribute 'MPC23017'
I'm using pins D1 and D2, being GPIO 5 and 4 for SCL and SDA, but I tried also other pins.
Today I'll have a look at this again. I'll double check all the wiring, to be sure I'm not missing anything.

Thanks.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MCP23017 16-bit IO Expander

Post by mcauser » Wed Apr 08, 2020 7:19 am

I have an ESP8266 handy. I'll try to make it fit.
Those pins are fine for I2C, however, you'll need some pull ups.
Consider using D3 and D4 as they already have 10k's (to set the correct boot mode). Side effect is, D4 is also used by the LED so it will flicker on data. Maybe use it for the I2C clock.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: MCP23017 16-bit IO Expander

Post by mcauser » Thu Apr 09, 2020 4:34 am

It fits on my ESP8266 (4MB Wemos D1 Mini) running MicroPython v1.12-337, on both FAT and Littlefs v2

The mcp23017.py file takes up 16k.
If I convert all those spaces to tabs, it takes up 13k.
Cross compile it and upload as mcp23017.mpy and it takes up 4k.

Code: Select all

from machine import Pin, I2C
import mcp23017
i2c = I2C(scl=Pin(4), sda=Pin(0)) # On the D1 mini, D4=GPIO2, D3=GPIO0
mcp = mcp23017.MCP23017(i2c, 0x20)

Post Reply