Cant get Adafruit_MCP23017.h to trigger LED's

Hey Everyone,
I hope someone can point me in the correct direction here, I am at a loss. I am trying to get my MCP23017 working. Before I start adding it to my design I just want to be sure it works. So far I can’t get it working. That goal is to have 4 LED outputs, 4 Relay outputs & 2 button inputs going through the MCP23017. For now I am just trying to get a loop created so the LED’s chase each other one second apart. I have reviewed my code over and over and looked up whatever samples I can find, I must be doing something wrong.
I appreciate your help! Also I am new to coding and IOT, this is also my first post here so if I do something wrong please educate me so I can learn.

#include <Adafruit_MCP23017.h>


Adafruit_MCP23017 mcp;

int led0 = 28;
int led1 = 27;
int led2 = 26;
int led3 = 25;

void setup() {
Serial.begin(9600);
mcp.begin();
mcp.pinMode(led0, OUTPUT);
mcp.pinMode(led1, OUTPUT);
mcp.pinMode(led2, OUTPUT);
mcp.pinMode(led3, OUTPUT);
pinMode(7, OUTPUT);
  mcp.digitalWrite(led0, HIGH); 
  mcp.digitalWrite(led1, HIGH);
  mcp.digitalWrite(led2, HIGH);
  mcp.digitalWrite(led3, HIGH); 

}

void loop() {
 
  mcp.digitalWrite(led0, LOW); 
  mcp.digitalWrite(led1, HIGH);
  mcp.digitalWrite(led2, HIGH);
  mcp.digitalWrite(led3, HIGH);
  digitalWrite(7, LOW);
delay(1000);
  mcp.digitalWrite(led0, HIGH); 
  mcp.digitalWrite(led1, LOW);
  mcp.digitalWrite(led2, HIGH);
  mcp.digitalWrite(led3, HIGH);
    digitalWrite(7, HIGH);
delay(1000);
  mcp.digitalWrite(led0, HIGH); 
  mcp.digitalWrite(led1, HIGH);
  mcp.digitalWrite(led2, LOW);
  mcp.digitalWrite(led3, HIGH);
    digitalWrite(7, LOW);
delay(1000);
  mcp.digitalWrite(led0, HIGH); 
  mcp.digitalWrite(led1, HIGH);
  mcp.digitalWrite(led2, HIGH);
  mcp.digitalWrite(led3, LOW);
    digitalWrite(7, HIGH);
delay(1000);

}

Here is my diagram of my end result, got a few more sensors Ill add directly to the photon later. I’m just trying to confirm my basic code works with the MCP23017 that I have, and so far it’s not.

Thanks a bunch!

Can you also provide a symptom description in what way your code doesn’t work?
Are you sure you want pins 25…28 when the chip only has 16 GPIOs? I guess you rather want 04…08 (Port A GPA4…GPA8)
Have you checked whether your MCP23017 can be found on the I2C bus at the default address?
To check for that you may run this I2C scanner

1 Like

@ScruffR,
Thanks for the assist.

Q: Can you also provide a symptom description in what way your code doesn’t work?
A: Can’t control the pins on the MCP23017, tried with both LED and Relays and can’t get the pins to switch to Output HIGH or LOW.

Q: Are you sure you want pins 25…28 when the chip only has 16 GPIOs? I guess you rather want 04…08 (Port A GPA4…GPA8)
A: Maybe I am not understanding the chip numbering. I thought I had to use the physical pin number.
I used this diagram:
mcp23017_Chip
I did also try to use 1-4 for my relay but that also didn’t work.

Q: Have you checked whether your MCP23017 can be found on the I2C bus at the default address?
A: Nope I didn’t even know this was possible. I ran the code you provided the link to and i get “No I2C device found”. So I guess that is my first issue, Why isn’t it seeing the chip…

I tried plugging my I2C LCD screen into D0 & D1 just to see if I can get an address and the scan tool found that device address of 0x27. Think maybe I have a bad chip?

Sorry for the Q&A formatting, I don’t know how to quote you in this thing.

@RecklessR, here are a few questions:

  1. Which DeviceOS are you specifying?
  2. Are you using the Web IDE or Particle Workbench for your code?
  3. Are you using a Web IDE library for the MCP23017. Have you looked at the examples for that library?
  4. For the line pinMode(7, OUTPUT); did you mean pin D7'? If so, I highly recommend using the full Particle pin name for any Photon pins, in this case D7`.

If you are using the WebIDE Adafruit_MCP23017 library, then PortA GPA0…GPA7 (pins 21 to 28) have pin values of 0…7. For PortB GPB0…GPB7 (pins 1 to 8) have pin values of 8…15.

Also, pins this library are defined as uint8_t whereas you define your pins are int values. The compile most likely threw some warnings on this. Though it should cause problems, it’s always a good idea to match parameter types.

What value are your pull-up resistors. To me they look wrong. For 4k7 they should be yellow-violet-red, your third ring does not look red to mee on that fritzing.
And as I suggested and @peekay123 also confirmed you are not using the physical pin numbers but the GPIO numbers 0…7 for port A and 8…15 for port B.

@ScruffR, if correct about the pull-ups. If that third band is grey then you’ve got 4.7Giga Ohms!!

1 Like

4.7 Giga, right?

2 Likes

Oh man you guys are awesome! I went back to check my resistors and I pulled 2 4R7 resistor instead of a 4K7 resistor. Ill have to order some 4k7, but I remembered seeing something about using a 10K so I tried that and bam got an address of 0x20.

I also now have one of the LED’s working off of the MCP23017, looks like I just need to set the correct pins and I should be good.
I’ll play around with this for a bit and see if I can get all 4 LED’s blinking.

1 Like

Huge thanks to @ScruffR & @peekay123. My original code is now working via Web IDE with the 10K resistor, that ill replace with a 4k7 one shortly.

4 Likes

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