MCP23017 exhibiting different behaviour with UNO and Zero

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

Hi there,

I've recently gotten into the mcp23017 microchip for port expansion. In my initial test, I had a circuit with 5 buttons and 4 leds working consistently on an Arduino Uno. I ported it over to an Arduino Zero and also an Arduino MKRZero, but neither of them worked. I ended up trimming down the circuit quite a bit to just barebones chip detection / setup, and then adding one single LED. Still, it wouldn't work. With one single LED, it was very sometime-ish (mcp would sometimes configure, sometimes not), and as soon as I added anything more, it would behave very erratically.

For reference, I'm using the Adafruit_MCP23X17 library, and regular mcp23017 chips. The following code works without fail on the Uno:

Code: Select all

#include <Adafruit_MCP23X17.h>
#include <Stepper.h>

// initialise mcp
Adafruit_MCP23X17 mcp;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Welcome to the candy machine");

  // init mcp
  if (!mcp.begin_I2C(0x20)) {
    Serial.println("Error initialising mcp");
    while (1);
  }

  // assign leds as outputs
  mcp.pinMode(8, OUTPUT);
}

void loop() {
  mcp.digitalWrite(8, HIGH);
}
I've also attached the circuit schematic for the single LED layout (see attached).
circuit_bb.png
circuit_bb.png (316.05 KiB) Viewed 115 times
- On the Uno, I'm powering from computer.
- On the Zero, I'm using the programming port for uploading code to the Arduino, and the barrel jack to supply 12V to the device.

Would love any input or feedback on this one, as I can't find much out there on the interwebs!

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by adafruit_support_bill »

If the Zero is not connected via USB to your computer, you need to delete this section of code, since the Zero is a native USB processor:

Code: Select all

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  

User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

I actually leave the usb connected to observe the Serial logs. It passes this step, but then intermittently fails in the mcp.begin conditional. So the Serial will log "Error initialising mcp".

If I have more components connected to the mcp pins, it fails even more regularly... pretty much every time. And the odd time it does connect to mcp, the running of the rest of it all is very erratic. Unlike on the Uno, where it ran all as expected.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by adafruit_support_bill »

Your diagram shows you powering it with 5v from the UNO. Since the Zero uses 3.3v logic levels, you should be powering the MCP23017 from 3.3v also.

User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

Oh that's interesting, I'll give it a shot. Just curious then, what's the 5v pin for on the Zero? I've used a multimeter across the jumpers GND and 5v coming from the Zero, and it reported 5v, so I assumed I could use it as is...

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by adafruit_support_bill »

Many shields and other devices run on 5v, so they maintained the 5v pin for backwards compatibility with other Arduino devices. However, the processor itself runs on 3.3v, so anything connected to the GPIO pins should be 3.3v as well - including the SDA and SCL pins of the i2c bus used to communicate with the MCP23017.

User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

Ok awesome, that's great to know! I'll give it a spin again and let you know how it goes :) Thanks for the help so far.

User avatar
michaelmeissner
 
Posts: 1821
Joined: Wed Aug 29, 2012 12:40 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by michaelmeissner »

Note, I'm a software guy, not an EE, but over the years, I've run into problems with I2C buses.

One other thing that you may need is two pull-up resistors, one connecting the SDA line to your power line (3.3v), and the other connecting the SCL line to the power line (3.3v).

With 5 volt AVR you typically do not need pull-up resistors, since the I2C library enables the hardware pull-up resistors.

With many 3.3v ARM processors, you do need pull-up resistors because while the hardware has built-in pull-up resistors, they aren't strong enough to use.

The newer i2c devices from Adafruit (and other suppliers) have pull-up resistors on the I2C device, so you wouldn't need to add more resistors if you have one of these newer devices on the I2C bus. You only need one pair of pull-up resistors for the entire I2C bus.

You can have multiple devices that each have pull-up resistors. In general this will work, but with the more pull-up resistors on the I2C bus, it can possibly slow things down (depending on the characteristics of the bus and devices).

The MCP23017 is an older I2C device, it may not have the pull-up resistors on the board.

I imagine that the reason pull-up resistors aren't included on the microprocessor board is the two lines that implement SDA and SCL can often be used for different purposes, and a pull-up resistor may impact those other uses.

A pull-up resistor is a resistor that one end is wired in parallel to the data connection and the other end is wired to your processor's main power bus (3.3v). For systems that only have 3.3v devices, you would typically use a 2.2K resistor. For systems with 5v devices, you would typically use a 4.7K resistor. IIRC, you can probably use up to a 10K resistor, but generally you want to use the smaller resistor if it will work.

On the ARM Teensy processors that I usually use, if I don't have pull-up resistors, I can't use the normal i2cscanner example when there are no i2c devices on the bus, but I would be able to use it if one of the devices has the pull-up resistors. As I recall, several microprocessors from Adafruit mention not having pull-up resistors on the I2C bus. Some of the QT PY boards that have 2 I2C buses, will not include pull-ups for the I2C bus through normal pins, but they will use pull-ups for the QWIIC connector (since that is generally only used for I2C).

<edit>
One other note about mixing I2C voltages. If your microprocessor is not tolerant of 5 volt inputs, and you hook up the I2C bus using ground, SDA, SCL, and 5 volts (VIN/USB/etc.), you might fry your microprocessor (or at least the I2C pins). There are ways to have an I2C bridge that has 3.3 volts on one side of the I2C bus, and 5 volts on the other side (BTDT). These days, I try to eliminate using any I2C device that requires 5 volts (the MCP23017 can run at either 3.3v or 5v). Generally, only devices I bought back when I was beginning with the Arduino UNO were 5 volt only.

User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

Oh wow that is a very interesting insight Michael! Thanks for providing all those details. I'm also now coming into electrical/hardware after many years in software, so every day is a learning :) I'll try to digest all that and do some more reading, and take note of my findings.

BTW these are the chips I bought in case it helps with any further insights:

https://www.amazon.ca/MCP23017-SP-EXPAN ... 277&sr=8-2

User avatar
michaelmeissner
 
Posts: 1821
Joined: Wed Aug 29, 2012 12:40 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by michaelmeissner »

You are welcome.

BTW, here is an explanation from the Adafruit learning system: BTW, it was a coincidence that I was continuing to soldering up a prototype board today so that I can mount the various Teensys, and test things like the electronic animated eyes project with the various Teensies and the various displays I have (basically 2 displays that display right/left eyes in a creepy fashion).

For all of the Teensies, it brings out the main SPI device using the pins I typically use, a neopixel port, an analog input area I can put a potentiometer, and finally a set of pins to bring out the main I2C bus in a 4 pin setup for easy device plugging. If I have a Teensy 4.1, the prototype board will bring out the second SPI bus, and have 2 breakouts for the UART devices.

Between my previous reply and now, I just soldered in the 2.2K pull-up resistors I need for SDA/SCL, along with the pull-up resistors for the CS and D/C pins on the two SPI breakouts.

User avatar
nicksalloum
 
Posts: 6
Joined: Wed Sep 28, 2022 12:59 pm

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by nicksalloum »

Happy to report that the pull up resistor requirements with the 3.3v logic level voltage on the zero (which is SAMD51) was the missing piece of the puzzle. The series of readings here (https://learn.adafruit.com/working-with ... -resistors) really helped out! Thanks all for the help.

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

Re: MCP23017 exhibiting different behaviour with UNO and Zero

Post by adafruit_support_bill »

Good to hear that is working for you. Thanks for the update.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”