MCP23008 I2C issue

Hi,

I'm running a very simple sketch to test an adafruit MCP23008 IC, using the adafruit library on an ESP32 Dev-Kit. The code for the sketch is;

#include <Adafruit_MCP23X08.h>

Adafruit_MCP23X08 mcp;

int ledArray[] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup() {
  delay(1000);
  Serial.begin(9600);
  mcp.begin_I2C();
  int i = 0;
  for (i; i < 8; i++) {
    mcp.pinMode(i, OUTPUT);
    mcp.digitalWrite(i, LOW);
  }
}

void loop() {
  int i = 0;
  for (i; i < 8; i++) {
    mcp.digitalWrite(ledArray[i], HIGH);
    Serial.print("Active Pin: ");
    Serial.println(i);
    delay(1000);
    mcp.digitalWrite(ledArray[i], LOW);
    Serial.print("Deactiving Pin: ");
    Serial.println(i);
  }
}

I don't know how to draw a circuit diagram online but my pinout for the MCP23008

1. SCL line to ESP32 pin D22       <I2C Clock line>
2. SDA line to ESP32 pin D21     <I2C Data line>
3. Add2 -> Ground of ESP32
4. Add1 -> Ground of ESP32
5. Add0 -> Ground of ESP32  <should give MCP23008 address of 20 which is adafruit default>
6. Reset -> +5V from ESP32 VIN
7. NC (Float) therefore disconnected
8. INT (Float) therefore disconnected
9. VSS -> Ground on ESP32
10. GPIO 0pin -> LED -> 1k Resistor -> Ground of ESP32.
11. GPIO 1pin -> LED -> 1k Resistor -> Ground of ESP32.
12. GPIO 2pin -> LED -> 1k Resistor -> Ground of ESP32.
13. GPIO 3pin -> LED -> 1k Resistor -> Ground of ESP32.
14. GPIO 4pin -> LED -> 1k Resistor -> Ground of ESP32.
15. GPIO 5pin -> LED -> 1k Resistor -> Ground of ESP32.
16. GPIO 6pin -> LED -> 1k Resistor -> Ground of ESP32.
17. GPIO 7pin -> LED -> 1k Resistor -> Ground of ESP32.
18. VSS (+5V)  ->   ESP32 VIN

The ESP32-DevKit is on a bread board, with dupont cables running to another breadboard with the MCP23008 IC. All points have been tested for continuity from ESP32 pins to MCP23008 pins (to rule out cable issues) and +5 voltage has been confirmed too on pins requiring them.

When I use and arduino I2C scanner I can confirm that the I2C portion is working;

16:22:56.084 -> Scanning...
16:22:56.130 -> I2C device found at address 0x20 !
16:22:56.130 -> done

When I run my sketch I would expect the LEDs to sequentially turn on and off again, and for the most part they do. However, quite often and seemingly random (not in sequence to the loop) ALL or MOST of the LED's turn on simultaneously, and only turn off when the sequence forces them to toggle on / off again.

N.B. This issue seemingly reduces and the symptoms get better when I run from a mains powered USB charger and is worse when plugged into my laptop USB socket to power the ESP32-DevKit.
N.B.2. I have swapped out three different MCP23008 ICs to rule out a single point of failure of the IC.
N.B.3. I purchased a multipack of these ESP32-DevKits (ESP-WROOM32) from amazon, could it be that there is a fault with the 'cheap' ESP boards that i have picked up?

This is part of a wider project for me and I'm trying to work out how best to use 3x MCP23008 to add significantly more (24 more) GPIO than a single ESP32-DevKit has by default.

I additionally tried running two MCP23008 chips (one with ADD1+2 -> +5V for an address of 26 (mcp2.begin(6) but i couldn't get this to respond in the sketch, despite both being visible on the I2C scanner at address 20+26). I'm unsure if this is the same ???noise??? issue that i'm seeing with just one IC as described again or an unrelated issue.

Thanks for taking the time to read and I appreciate any comments especially if there is a better way to either wire it up, sketch test it. Currently I'm unable to source MCP23017 ICs which would add more pins easier ... the great chip shortage!

edited to format sketch

Thanks again

Josh

That's the only thing that looks odd to me.

Can you get 5 volts on there directly from 5 volts? Vin, as you might suspect, is where voltage goes in.

The device may work on fumes until you start drawing LED amounts of current.

a7

1 Like

Aha!

Evidentially I thought I could as I measured with a volt meter and get a full +5v. I assumed that was just taking +5V from the USB bus and that I could use that as a +5v feed.

I was looking at the pinout diagram and as the datasheet for the MCP23008 said +5V and that was the only +5V on the pinout diagram.

I've just moved

18. VSS -> ESP32 3.3V pin

And it seems to be working nicely. Now my brain wants to know why that works but the +5v line didn't?

I'm intentionally running higher resistors (1k) to limit current draw across small leds (20ma draw)... but maybe I was just choking that 5v line too hard?

Now time to see if i can run a second MCP23008, shame i just packed all my stuff away for the night!

Sorry, i was so surprise that that worked I forgot to thank you!

Thanks so much for your help! Really appreciate it!

Photo of pencil/paper drawing would do :wink:

Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Read How to make a schematic to post to get started with CAD schematic drawing.

It looks like pin 18 is Vdd (+V) not Vss (GND, like pin 9)!

Assuming you are asking about Vin, it is on the microprocessor board for where you give power to the microprocessor, it has no ability to drive anything as a power supply.

What you measured was misleading as there may have been voltage there, someone can explain it better, but no voltage with any ability to power LEDs no matter how dim.

Or your chip was getting phantom power from the I2C bus, which again works until you make a significant demand for current.

In any case, doing it right works, so.

+1 for hand-drawn schematics - there's a time and place for using a computer, and climbing that learning curve, but being able to just draw one will never be something you regret learning.

Schematics, like code, are a language. Like any language, reading will inform improvement on writing, so start looking at a few simple schematics to see what the deal is.

Browsing these fora is wonderfully educational, you can see code, schematics and so forth and see other ppl's work, either to emulate or eschew.

Eschew, haha.

a7

Thanks everyone.

Yes I did make a typo, Pin 9 should read VSS -> Ground.
edited to add that the error was actually
Pin 18 should read VDD -> 3.3V (solution was to move from the 5v to the 3.3v!)

I will have a look up online to see how to draw the diagrams!

The voltage does make sense now you put it like that, it might show 5v but I'm guessing as soon as a load was put on it would have dropped significantly enough to see the issues I was having.

I have subsequently got a 2nd MCP23008 functional this evening.

Thanks again for all your help.

#include <Adafruit_MCP23X08.h>

Adafruit_MCP23X08 mcpa;
Adafruit_MCP23X08 mcpb;

int ledArray[] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup() {
  delay(1000);
  Serial.begin(9600);
  mcpa.begin_I2C(0x20);
  mcpb.begin_I2C(0x26);
  int i = 0;
  for (i; i < 8; i++) {
    mcpa.pinMode(i, OUTPUT);
    mcpb.pinMode(i, OUTPUT);
    mcpa.digitalWrite(i, LOW);
    mcpb.digitalWrite(i, LOW);
  }
}

void loop() {
  int i = 0;
  for (i; i < 8; i++) {
    mcpa.digitalWrite(ledArray[i], HIGH);
    mcpb.digitalWrite(ledArray[i], HIGH);
    Serial.print("Active Pin: ");
    Serial.println(i);
    delay(500);
    mcpa.digitalWrite(ledArray[i], LOW);
    mcpb.digitalWrite(ledArray[i], LOW);
    Serial.print("Deactiving Pin: ");
    Serial.println(i);
  }
}

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