🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

Surprised by how slow i2c commands for MCP23017 are.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Surprised by how slow i2c commands for MCP23017 are.

by SeattleDavid on Mon Nov 16, 2020 11:42 pm

I was surprised by how slow i2c was on an Adafruit Metro ESP32-S2 when talking to an MCP23017 I/O chip. I am only getting about 30 I/O (writes) per second.

Perhaps this is considered normal (I am a newbie) but I would have expected it to be far faster...with i2c running at 100 or 400 kHz, and commands being just a few bytes, it seems like I should have been able to turn 16 relays on in less than half a second.

Here is the test code that I used...it would do 100 loops in 6 seconds (each loop does an ON and an OFF, so this is 200 i2c relay commands in 6 seconds, hence the 30-per-second determination.)

This means that controlling i2c devices is pretty limiting in terms of how many or how fast.

This this normal/typical/reasonable?


Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
from digitalio import Direction
#import digitalio
import time
from adafruit_mcp230xx.mcp23017 import MCP23017
i2c = busio.I2C(board.SCL, board.SDA)
mcp0x27 = MCP23017(i2c,address=0x27)
index=0
loopCount=0
relay=0
relayBuzzPin=7
relayBuzz=mcp0x27.get_pin(relayBuzzPin)
relayBuzz.direction = Direction.OUTPUT
relayBuzz.value = True # high logic level
time.sleep(1)
relayBuzz.value = False # low logic level
time.sleep(1)
relayBuzz.value = True # high logic level
time.sleep(1)
relayBuzz.value = False # low logic level
while True:
    relayBuzz.value = True # high logic level
    relayBuzz.value = False # low logic level
    loopCount += 1
    if (loopCount % 100) == 0:
        print("Loop #",loopCount)
print("Done")

SeattleDavid
 
Posts: 40
Joined: Wed Sep 18, 2019 2:26 am

Re: Surprised by how slow i2c commands for MCP23017 are.

by tannewt on Tue Nov 17, 2020 6:22 pm

It's hard to say what is typical. I'd suggest comparing it against the performance of a SAMD51 doing the same thing. We're seeing some weirdness with I2C on ESP32-S2 now and that might be related.

Also note that in 6.x we default to 100khz. So, you'll need to set it to 400khz if you want it faster.

tannewt
 
Posts: 1922
Joined: Thu Oct 06, 2016 8:48 pm

Re: Surprised by how slow i2c commands for MCP23017 are.

by SeattleDavid on Tue Nov 17, 2020 6:38 pm

So little data is being sent—a few bytes—that I can’t imagine it makes any difference if you transmit at 100,000 bits or 400,000 bits per second.

It still seems quite odd that you can only send 30 i2c commands/packets per second. It means you cant do very much I/O in a second.

I benchmarked the Metro ESP32-S2 and it is executing something crazy like 100,000 lines per second...shockingly fast.

But only being able to do 30 I/O per second is concerning.

SeattleDavid
 
Posts: 40
Joined: Wed Sep 18, 2019 2:26 am

Re: Surprised by how slow i2c commands for MCP23017 are.

by SeattleDavid on Sun Nov 22, 2020 6:04 pm

30 i2c commands—maximum—per second seems to be so slow as to make it nearly useless.

It would take a minimum of 1/2 second to turn on all outputs of a single MCP23017.

The problem is clearly not i2c Buss speed, so what the heck is making i2c I/O so hideously slow?

SeattleDavid
 
Posts: 40
Joined: Wed Sep 18, 2019 2:26 am

Please be positive and constructive with your questions and comments.