MCP23017 not initializing in Setup

Status
Not open for further replies.

rfresh737

Well-known member
I'm trying to add an MCP23017 chip to my bread board which is using a Teensy 3.6.

My whole sketch is attached.

The problem I am having is that in Setup, I can't get through the whole MCP setup.

Below is the setup block and my debug code prints 1 in the serial window and then stops. I never see 2, 3, etc.

Thank you for any help.

Code:
#include <Adafruit_MCP23017.h>
#include <i2c_t3.h>
#include <SD.h>
#include "SPI.h"

const byte  mcp1_address = 0x20; // I2C Address of 1st MCP23017 Chip: 12 Softkeys
const byte  GPIOA = 0x12;            // Register Address of Port A
const byte  GPIOB = 0x13;            // Register Address of Port B
byte c = 0;
int buttonPressDelay = 200;
int mx = 0;
int my = 0;

void setup()
{                     
  Serial.begin(115200);
  while (!Serial)
  {
  }                
  
  Serial.println("Setup begin");
  int debug2 = 1;
  //Send settings to MCP device  
  Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, I2C_RATE_400);
  if (debug2 == 1) Serial.print("1"); // <------------------- stops here
  Wire.beginTransmission(mcp1_address);
  Wire.write(0x00); // IODIRA registers - Bank A
  Wire.write(0xFF); // set all of bank A pins to input type
  Wire.endTransmission();
  if (debug2 == 1) Serial.print("2");
  Wire.beginTransmission(mcp1_address);
  Wire.write(0x01); // IODIRB registers - Bank B
  Wire.write(0xFF); // set all of bank B pins to input type
  Wire.endTransmission();
  if (debug2 == 1) Serial.print("3");
  Wire.beginTransmission(mcp1_address);
  Wire.write(0x0C); //pull up resistors on bank A pins
  Wire.write(0xFF); // set all of bank A pins to input type
  Wire.endTransmission();  
  if (debug2 == 1) Serial.println("4");
  Wire.beginTransmission(mcp1_address);
  Wire.write(0x0D); // IODIRB register - Bank B
  Wire.write(0xFF); // set all of bank B pins to input type
  Wire.endTransmission();  
  Serial.println("Setup end");
}
 

Attachments

  • FS1000Main1.0.0.ino
    5.4 KB · Views: 80
Looking at the i2c_t3 library documentation, I find:

Previous library releases used
I2C_RATE_xxxx enums. This is still supported, but is now deprecated, and specifying the frequency directly (as a uint32_t value) is now the preferred method.

Thus, I'd try one of these from the documentation again:
Code:
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000); // Wire bus, SCL pin 19, SDA pin 18, ext pullup, 400kHz 
Wire.begin(I2C_MASTER, 0x00, 19, 18); // equivalent to above, will default to ext pullup at 400kHz

And I would check with the scanner example sketch if your MCP answers really on address 0x20.
 
lacking a given diagram of your setup, make sure your address lines are all tied to GND since your using 0x20 in your sketch, also make sure the MCP reset line is tied HIGH, ALSO make sure your running the MCP chip at 3.3V, NOT 5V. Teensy 3.6 is NOT 5V tolerant.. :)
 
the adafruit MCP23017 library uses Wire.h, that will conflict with your sketch using i2c_t3.h. The T3.6 is not 5v tolerant make sure you power the MCP23017 with 3v3. You also will need pullup resistsors (to 3.3v) on SDA and SCL pins.

i would try running one of the adafruit MCP23017 examples with the adafruit library.
 
Thanks everyone for the help.

Here is my response to each one.

>the adafruit MCP23017 library uses Wire.h, that will conflict with your sketch using i2c_t3.h.

I commented out the line:
Code:
//#include <i2c_t3.h>

I tried both of the suggestions below with no changes seen in the sketch behavior:

Code:
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, 400000); // Wire bus, SCL pin 19, SDA pin 18, ext pullup, 400kHz 
Wire.begin(I2C_MASTER, 0x00, 19, 18); // equivalent to above, will default to ext pullup at 400kHz

>lacking a given diagram of your setup, make sure your address lines are all tied to GND since your using 0x20 in your sketch.

Do you mean the GPA and GPB address lines? At the moment, I only have one wire hooked up to GPA0. One wire from the switch going into this pin (GPA0) on the MCP and the other switch wire going to ground. The rest of the GPA pins are not being used at this point. None of the GPAB pins are being used at this time either.

>also make sure the MCP reset line is tied HIGH

The MCP RESET pin (18) has 3.3V power going into it.

>ALSO make sure your running the MCP chip at 3.3V, NOT 5V. Teensy 3.6 is NOT 5V tolerant

I'm powering my bread board power rows using the 3.3V pin on the Teensy 3.6

>And I would check with the scanner example sketch if your MCP answers really on address 0x20.

A problem here. I ran the scanner sketch and got "No I2C devices found".

MCP pins 15, 16 and 17 are all tied into ground to equal address 0x20.
MCP pin 18 is receiving 3.3V power.
MCP pin 9 is receiving 3.3V power.
MCP pin 10 is wired to ground.
MCP pin 12 SCL is plugged into pin 19 on the 3.6.
MCP pin 13 SDA is plugged into pin 18 on the 3.6.

MCP pin 14 not used.
MCP pin 11 not used.
MCP pin 19 not used.
MCP pin 20 not used.
 
>i would try running one of the adafruit MCP23017 examples with the adafruit library.

I tried to run the button example (below) but when I press my button, I don't see the LED light up at all.

I've checked all the pin connections as commented at the top of this sketch.

Code:
//#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)

// Input #0 is on pin 21 so connect a button or switch from there to ground

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

  mcp.pinMode(0, INPUT);
  mcp.pullUp(0, HIGH);  // turn on a 100K pullup internally

  pinMode(13, OUTPUT);  // use the p13 LED as debugging
}



void loop() {
  // The LED will 'echo' the button
  digitalWrite(13, mcp.digitalRead(0));
}

I've had to comment out the wire.h since it's already in the Adafruit MCP i2c include.

Top part of the Adafruit MCP header file:
Code:
#ifndef _Adafruit_MCP23017_H_
#define _Adafruit_MCP23017_H_

// Don't forget the Wire library
#ifndef ARDUINO_AVR_GEMMA
                                    //TinyWireM is now part of
                                    //   Adafruit version of Wire Library, so this
                                    // will work with Adafruit ATtiny85's
                                    //But Arduino Gemma doesn't use that library
                                    // We do NOT want to include Wire if it's an arduino Gemma
  //#include <Wire.h>
  #include <i2c_t3.h>
#else
  #include <TinyWireM.h>
  #define Wire TinyWireM
#endif
 
If the scanner sketch does already not yet see the MCP, no need to look further into the code. I2C wiring and addressing issues have to be resolved, first.
 
I would imagine you NEED pull-up resistors. You need two resistors, one between SCL and 3.3v, and the other between SDA and 3.3v. For a simple i2c bus on a Teensy, you want 2.2K resistors (on a complex i2c bus, you may need to calculate what resistors you need or experiment with different values). You can go higher (4.7K is a typical value used for 5v systems), but if you use a higher value, it may prevent you from running i2c at higher bus speeds. Some i2c devices provide pull-up resistors, some don't. IIRC, the MCP23017 doesn't provide pull-up resistors.
 
Michael is right,
Code:
                             3.3V
                                |
                           2.2K ohm
                                |
MCP pin 12 SCL is plugged into pin 19 on the 3.6.

MCP pin 13 SDA is plugged into pin 18 on the 3.6.
                                |
                           2.2K ohm
                                |
                             3.3V
 
I added the resistors and that worked. The scanner sketch now ID's 0x20 MCP. My button press works as expected.

Thank you everyone for the help!
 
Status
Not open for further replies.
Back
Top