ESP32, MCP23017 expander is unstable

Hi.

I'm trying, to expand outputs with MCP23017, but it's in no way stable, so i must be doing something wrong.
Here is my test code

#include <Wire.h>
#include <Adafruit_MCP23X17.h>

#define MCP_LED1 0
#define MCP_LED2 1
#define MCP_LED3 2
#define MCP_LED4 3
#define MCP_LED5 4
#define MCP_LED6 5
#define MCP_LED7 6
#define MCP_LED8 7
#define MCP_LED9 8
#define MCP_LED10 9
#define MCP_LED11 10
#define MCP_LED12 11
#define MCP_LED13 12
#define MCP_LED14 13
#define MCP_LED15 14
#define MCP_LED16 15

#define MCP_INT_ODR    false 

Adafruit_MCP23X17 mcp;

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

  mcp.pinMode(MCP_LED1, OUTPUT); 
  mcp.pinMode(MCP_LED2, OUTPUT); 
  mcp.pinMode(MCP_LED3, OUTPUT); 
  mcp.pinMode(MCP_LED4, OUTPUT); 
  mcp.pinMode(MCP_LED5, OUTPUT); 
  mcp.pinMode(MCP_LED6, OUTPUT); 
  mcp.pinMode(MCP_LED7, OUTPUT); 
  mcp.pinMode(MCP_LED8, OUTPUT); 
  mcp.pinMode(MCP_LED9, OUTPUT); 
  mcp.pinMode(MCP_LED10, OUTPUT); 
  mcp.pinMode(MCP_LED11, OUTPUT); 
  mcp.pinMode(MCP_LED12, OUTPUT); 
  mcp.pinMode(MCP_LED13, OUTPUT); 
  mcp.pinMode(MCP_LED14, OUTPUT); 
  mcp.pinMode(MCP_LED15, OUTPUT); 
  mcp.pinMode(MCP_LED16, OUTPUT); 
  
      // LED output
  mcp.digitalWrite(MCP_LED1,LOW);
  mcp.digitalWrite(MCP_LED2,LOW);
  mcp.digitalWrite(MCP_LED3,LOW);
  mcp.digitalWrite(MCP_LED4,LOW);
  mcp.digitalWrite(MCP_LED5,LOW);
  mcp.digitalWrite(MCP_LED6,LOW);
  mcp.digitalWrite(MCP_LED7,LOW);
  mcp.digitalWrite(MCP_LED8,LOW);
  mcp.digitalWrite(MCP_LED9,LOW);
  mcp.digitalWrite(MCP_LED10,LOW);
  mcp.digitalWrite(MCP_LED11,LOW);
  mcp.digitalWrite(MCP_LED12,LOW);
  mcp.digitalWrite(MCP_LED13,LOW);
  mcp.digitalWrite(MCP_LED14,LOW);
  mcp.digitalWrite(MCP_LED15,LOW);
  mcp.digitalWrite(MCP_LED16,LOW);
}

void loop(){
  mcp.digitalWrite(MCP_LED1, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED2, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED3, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED4, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED5, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED6, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED7, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED8, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED9, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED10, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED11, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED12, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED13, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED14, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED15, HIGH);
  delay(300);
  mcp.digitalWrite(MCP_LED16, HIGH);

  delay(300);

  mcp.digitalWrite(MCP_LED1, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED2, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED3, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED4, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED5, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED6, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED7, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED8, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED9, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED10, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED11, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED12, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED13, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED14, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED15, LOW);
  delay(300);
  mcp.digitalWrite(MCP_LED16, LOW);
  delay(300);

}


What do you mean by "no way stable"?
what did you see?
what did you expect?
how is the wiring done?
how is the power supply done?

A quick look at the code says, nothing wrong with the code but please learn to use arrays.

2 Likes

How do you define stable?

Possibly, you are exceeding max current by driving 16 LEDs.

2 Likes

Outputs are not opening and closing, sometimes all led's are on, other times one or two is working, and the rest are on or off. Most times all led's turns on after boot.

the led's are not turned on at the same time time.

Are you using a bare MCP23017 or a breakout board?
What is the distance between the ESP32 and the MCP23017?

If you are indeed using LEDs, do you have current limiting resistors? Be aware that the MCP23017 can only sink 8mA and source 3mA per output; also be aware that the total current through Vdd should not exceed 125mA and the total current through GND (Vss) should not exceed 150mA.

Have you connected a 0.1uF ceramic bypass cap to the chip?

do you have the 4.7k pullup resistors on SDA and SCL?

After confirming this:

... My go-to troubleshooting technique for issues like this is to see what the hardware is actually doing. You can get a very capable Logic Analyzer (with USB interface) from Amazon for well under 50USD. And, there's powerful, free software available for download. The software has decoders for dozens of protocols (including I2C) enabling you to see what's actually happening on the I2C interface. I find this technique invaluable.

Yes, i have 2 4.7K resitors

Yepp, i'm using a breakout board, and the distance is 3-4 inches

You can get four of these

but one will do.

You can pay more for the same thing elsewhere, but it won't be until you spend quite a bit more on something else that you end up with anything much better.

a7

Sorry guys.

If i change my 470ohm to 4.7kohm, it works :blush:
That's what happens, when a old guy thinks he don't need glasses.
Thanks for your time.

Finn

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.