Search code examples
i2cesp32

ESP32 - Connection to MCP23009. Can't get the OUTPUT to HIGH even if the connections returns succesful ( 0 )


I was trying to get an ESP32 working with I2C and because I had it around I took a MCP23009. Schmeantic is in the Image Image

My Code is the following :

#include <Wire.h> // specify use of Wire.h library.

#define MCPAddress  0x20 //I2C Address

#define IO_DIR_REG 0x00           // The Output Register
#define GPIO_REG 0x09             // The GPIO Register
#define IOCON 0x05                // Settings
#define SEQOP_REQ 0b00100000      // Disable address increment


#define I2C_SDA 21
#define I2C_SCL 22

int error;

void setup() 
{
  Serial.begin(115200); 
  delay(1000);
  Serial.println("Starting Wire");
  Wire.begin(I2C_SDA, I2C_SCL);

  Wire.setClock(100000); //Frequenz

  Wire.beginTransmission(MCPAddress); // Check if connection succesfull
  error = Wire.endTransmission();
  if(error == 0){
    Serial.println("Success");
  }else{
    Serial.println("Failure: ");
    Serial.print(error);
  }
  
  //Serial.println("Disable Auto-Address increment!");
  //writeBlockData(IOCON,SEQOP_REQ); //Experimental, didn't make it work
  
  Serial.println("Setting Outputs!");
  writeBlockData(IO_DIR_REG,0x00);
  
  Serial.println("Writing LOW!");
  writeBlockData(GPIO_REG,0x00);

}
 
void loop() 
{  
  Serial.println("Writing HIGH!");
  writeBlockData(GPIO_REG,0b11111111);
  delay(3000);
  
  Serial.println("Writing LOW!");
  writeBlockData(GPIO_REG,0b00000000);
  delay(3000);
}

int writeBlockData(uint8_t cmd, uint8_t val)
{
  Wire.beginTransmission(MCPAddress);
  Wire.write(cmd);
  Wire.write(val);
  delay(10);
  return Wire.endTransmission();
}

It's fairly simple and the connections works as I get only 0 when I read Wire.endTransmission() but the LEDs never turn High. No matter what I do. Here is the Datasheet from the MCP http://ww1.microchip.com/downloads/en/DeviceDoc/20002121C.pdf If anybody sees my mistake I would really appreciate it ! I am fairly new to work with I2C so I dont really see it. Even working with a Arduino Library didnt work.

Thanks and greetings !


Solution

  • Well, I didn' read the whole datasheet. The MCP has open drain outputs so turning around the diodes and put the other end into Vdd fixed it.