MCP23017 problem

Hi,
I’m trying to run this simple sketch on an Arduino Pro Mini 3.3V:

#include <Wire.h>
#include "Adafruit_MCP23017.h"

// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

Adafruit_MCP23017 mcp;
  
void setup() {  
  mcp.begin();      // use default address 0

  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT);
}


void loop() {

    mcp.digitalWrite(0, HIGH);
    delay(2000);
    mcp.digitalWrite(0, LOW);
    delay(1000);
    mcp.digitalWrite(1, HIGH);
    delay(2000);
    mcp.digitalWrite(1, LOW);
    delay(1000);

}

I’ve connected 2 leds at the output 0 and 1 of the MCP23017.
When the Arduino is powered by the USB-FTDI, there are no problems, when I disconnect the FTDI and power the Arduino with 9V to the RAW pin, it works for some minutes (sometimes 1 minute, sometimes 5 minutes) and then it hangs (one of the leds remain on or none is on, so the sketch hangs randomly).
What could cause this problem?

My guess is I2C problem. Maybe weak pull-ups causing problems at lower (higher/less stable?) supply voltage from regulator?

I’ve connected 2 leds at the output 0 and 1 of the MCP23017.

Do you have current limiting resistors in series with the LEDs?

A 9V block battery won’t power an Arduino for more than a few tens of minutes.

Is the regulator getting hot? How much current are you pulling?

What voltage is on 5v pin when it's misbehaving? What voltage on RAW pin?

The Arduino Wire library can hang when an I2C bus lockup occurs. If that's the problem, you could try an alternative I2C library that allows to you set a timeout.

That library also allows you to control the internal pull-ups, which can be useful.

Is the MCP23017 also using 3.3V power?

Since you have the 3.3V version of the Pro Mini and there are no problems when powered by 5V (USB-FTDI), then it appears to be a power issue. Specifically, your circuit is probably demanding more current at 3.3V than the regulator can continuously provide. Note that more current is available from VCC (3.3V) when RAW is at 5V than when RAW is at 9V. The max current you can get from the is 150mA, but this only occurs only when regulator's input is at it's minimum allowable voltage.

Try testing with a regulated 5V adapter or USB power cable and see if the problem goes away. Might also need to use higher value current limiting resistors for your LEDs.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Sorry for the delay.
Here is the circuit:
Screenshot from 2017-05-04 16-34-54.png
I’ve tried powering the Arduino with 9V and 5V on the RAW pin with a power supply (NO BATTERY) and the voltage regulator was never hot, then with 3.3V (still with a power supply) to the VCC but with the same result.
The circuit drawn about 8mA.
The FTDI provide 3.3V, not 5V.
I’m using a 100 ohm resistor for the leds (I’m using just one because they’re never on at the same time).
I haven’t tried to change the I2C library, will do and I’ll let you know.
Thanks!

There's no decoupling capacitor for the MCP23017 ... recommend 0.1µF. Also, I would suggest adding a 4.7µF to 100µF across the breadboard power bus rails.

And what about I2C pull-ups? Rule #1 with I2C: try stronger pull-ups. You have none.

Smajdalf:
And what about I2C pull-ups? Rule #1 with I2C: try stronger pull-ups. You have none.

dlloyd:
There's no decoupling capacitor for the MCP23017 ... recommend 0.1µF.

Ops, I didn't know it was needed, I don't know much about electronics, I've just followed the Adafruit example sketch:

Adafruit:
// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!

// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Output #0 is on pin 21 so connect an LED or whatever from that to ground

What resistor should I use? For both clock and data?

dlloyd:
Also, I would suggest adding a 4.7µF to 100µF across the breadboard power bus rails.

What for? Sorry for the ignorance!

Try one resistor with value from 3k3 to 10k between 3V3 and SDA pin. Second resistor between 3V3 and SCL pin. Longer wires and faster frequency and more noise needs stronger (lower value) resistors.
I2C devices are able pull the line down. If noone pulls down the resistors pull it up. Weak resistors = signal going up slowly = possible problems.

Capacitors are for powering chips when large surges of power are needed - power source is too slow to provide enough power when power demand quickly increases. It is hard to say how large capacitor is needed. More caps can make things better, rarely worse.

Smajdalf:
Try one resistor with value from 3k3 to 10k between 3V3 and SDA pin. Second resistor between 3V3 and SCL pin. Longer wires and faster frequency and more noise needs stronger (lower value) resistors.
I2C devices are able pull the line down. If noone pulls down the resistors pull it up. Weak resistors = signal going up slowly = possible problems.

That did it, thanks!!!

Smajdalf:
Capacitors are for powering chips when large surges of power are needed - power source is too slow to provide enough power when power demand quickly increases. It is hard to say how large capacitor is needed. More caps can make things better, rarely worse.

Knowing that I will have to connect a 9V latching solenoid valve (through a L9110 Motor Drive) instead of the led, cna you suggest a capacitor?

That's not really relevant. The cap was suggested to smooth the 5V supply. The motor driver will need a little current from the 5V supply, but mostly it will draw from the 9V supply. A big cap, e.g. 470uF or 1000uF, on that supply would be a good idea too, because there will be very sudden demands for extra current when the valve is activated. Put that cap close to the motor driver.

PaulRB:
That's not really relevant. The cap was suggested to smooth the 5V supply. The motor driver will need a little current from the 5V supply, but mostly it will draw from the 9V supply. A big cap on that supply would be a good idea too.

But I have just 1 9V power supply connected to the motor driver and to the RAW pin of Arduino

andrpex:
But I have just 1 9V power supply connected to the motor driver and to the RAW pin of Arduino

Yes, we we know, you said that. But you have two supply systems. The 9V and the 5V (even though one feeds the other). Smoothing caps on both supplies is a good idea.

Oh, you mean the 3.3V supplied by the Arduino Pro Mini?

Yes, sorry, 3.3V rather than 5V. There are no particularly current-hungry components in the 3.3V circuit (as far as you have told us) so a smaller cap like 47uF will be fine. But on the 9V circuit, that latching solenoid valve is going to pull a lot of current, so some big smoothing caps are required.

Perfect, thanks!

Smajdalf:
Try one resistor with value from 3k3 to 10k

10k is already quite high for a pull-up resistor, that's not a "strong" pull-up anymore. I would go for something between 1k8 and 2k7...