MCP2221A has 30ms of latency per write
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MCP2221A has 30ms of latency per write

by ericnutsch on Thu Aug 27, 2020 3:32 am

This is the device: https://www.adafruit.com/product/4471
This is the tutorial: https://learn.adafruit.com/circuitpytho ... all-checks
This is the chip docs: https://www.microchip.com/wwwproducts/en/MCP2221A
Photo of the breadboard is attached (note that the leds have internal resistors and are rated for 5v)

Talking to a MCP23017 over I2C https://www.microchip.com/wwwproducts/en/MCP23017

The simple code below just writes a high to all GPIOA outputs lighting some leds. Note that it is not a python delay or loop issue as we also hard coded it without a loop and had the same issue.
When the code is run on the raspberry PI adafruit-blinka library I2C, all 8 leds light simultaneously.
When the code is run on a windows 10 machine with mcp2221 adafruit-blinka library I2C, there is 0.03seconds of latency between each light. You can visual see them progressively light. The bigger issue is that the core is being held in a sleep during each write. This is really adds up and will occupy the whole core even if the latency is acceptable. We timed individuals and all eight with time.time. 0.235seconds for all eight.

I confirmed it was not an issue with the wiring or the chip by installing MCP2221 Command Line Interface (v1.0.2) and using an os.system in a loop to turn them on at off: and all light simultaneously. Also confirmed that the I2C bus speed did not matter. It worked perfectly at 400000 and at 48000.

So the verdict is that there is a latency issue in the adafruit-blinka library mcp2221 driver. Maybe something windows related??
The issue likely lives in mcp2221.py as there are multiple sleep instructions in there. If someone smarter could look at this driver I would appreciate it!

Worst case we will abandon the driver for the PyMCP2221A library, but hopefully we can get it working. https://github.com/nonNoise/PyMCP2221A/ ... PyMCP2221A

Any input is appreciated! Thanks!

Also: This user was having issues with the library latency on reading I2C, maybe related, maybe not.

Code: Select all | TOGGLE FULL SIZE
import board
import digitalio
import time
import busio
from adafruit_mcp230xx.mcp23017 import MCP23017

# SETUP INSTRUCTIONS: https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221?view=all
# Make sure "set BLINKA_MCP2221=1" environment variable has been set before running this script.

# MCP2221 Pins
led = digitalio.DigitalInOut(board.G0)
led.direction = digitalio.Direction.OUTPUT

# MCP230 Pins
i2c = busio.I2C(board.SCL, board.SDA)

""" Output Chip """
mcp1 = MCP23017(i2c, address=0x20)
# mcp2 = MCP23017(i2c, address=0x21)  # currently holds all the inputs

# Set up pins and outputs
outputs = list()
for i in range(10):
    pin = mcp1.get_pin(i)
    pin.direction = digitalio.Direction.OUTPUT

led.value = True

print("Starting output/led test")
for _ in range(30):
    for p in outputs:
        p.value = True
    for p in outputs:
        p.value = False
20200827_011656.jpg (527.17 KiB) Viewed 115 times
Posts: 2
Joined: Fri Nov 15, 2013 12:20 am

Re: MCP2221A has 30ms of latency per write

by siddacious on Fri Aug 28, 2020 3:01 pm

That's some pretty exhaustive testing; thanks for your efforts!

I'm not familiar enough with the code to help but I will bring this to the attention of someone who is

Thanks again for all the info; it will definitely help

Posts: 406
Joined: Fri Apr 21, 2017 3:09 pm

Please be positive and constructive with your questions and comments.