Four MCP23017 (I2C GPIO Expander) on one ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
vtt
Posts: 6
Joined: Mon Nov 02, 2020 4:34 pm

Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by vtt » Wed Nov 04, 2020 5:10 pm

Hello everyone,
I'm a beginner in micropython, I would like to operate 4 MCP23017 via I2C on an ESP32.
Only found this example for one, form mcauser:

Code: Select all

from machine import Pin, I2C
import mcp23017
i2c = I2C(scl=Pin(22), sda=Pin(21))
mcp = mcp23017.MCP23017(i2c, 0x20)

# list interface
mcp[0].input()
mcp[1].input(pull=1)
mcp[1].value()
mcp[2].output(1)
mcp[3].output(0)

# method interface
mcp.pin(0, mode=1)
mcp.pin(1, mode=1, pull=True)
mcp.pin(1)
mcp.pin(2, mode=0, value=1)
mcp.pin(3, mode=0, value=0)

mcp.config(interrupt_polarity=0, interrupt_mirror=1)

# property interface 16-bit
mcp.mode = 0xfffe
mcp.gpio = 0x0001

# property interface 8-bit
mcp.porta.mode = 0xfe
mcp.portb.mode = 0xff
mcp.porta.gpio = 0x01
mcp.portb.gpio = 0x02
What about the I2C addresses, what do I have to do?

I appreciate help. :D

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by Roberthh » Wed Nov 04, 2020 5:18 pm

The MCP23017 has three address selection inputs allowing 8 possible addresses to be set. Using these pins you have to configure the four chips for different addresses. That is usually done hardwired.

vtt
Posts: 6
Joined: Mon Nov 02, 2020 4:34 pm

Re: Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by vtt » Wed Nov 04, 2020 5:43 pm

And then could I address her like that?

Code: Select all

i2c = I2C(scl=Pin(22), sda=Pin(21))
mcp = mcp23017.MCP23017(i2c, 0x20)
mcp1 = mcp23017.MCP23017(i2c, 0x21)
mcp2 = mcp23017.MCP23017(i2c, 0x22)
mcp3 = mcp23017.MCP23017(i2c, 0x23)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by Roberthh » Wed Nov 04, 2020 5:55 pm

Yes.

vtt
Posts: 6
Joined: Mon Nov 02, 2020 4:34 pm

Re: Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by vtt » Wed Nov 04, 2020 6:22 pm

Thank you very much!

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

Re: Four MCP23017 (I2C GPIO Expander) on one ESP32

Post by mcauser » Thu Nov 12, 2020 5:09 am

Roberthh is spot on.

That's a lot of IOs! What are you building?

You can only have 8x MCP23017 chips on the same I2C bus, unless you use something like the TCA9548A, which give you another 7x I2C busses - well, more like channels on the bus.
https://github.com/mcauser/micropython-tca9548a

8x I2C bus channels, with 8x MCP23017's gives you 1024 IOs.
Still not enough? You can chain 64 of those TCA9548As for up to 65,536 IOs!

Post Reply