Cannot interact with MCP23008

THAT WAS IT.
Thank you very much and sorry for making so much mess for a simple thing.

Another question, I’m trying to use a push button as an input in the GPs and then when I push it, turn on a relay.
I have not been able to do so by just using the button as a GPIO input, like it would be if I was only working with the ESP.

Is there any special consideration to do that ?

Hi,

Can you provide the code you have developed so far?

You will need to set the direction of the IO lines on the MCP23008 by writing 0xF0 to register 0x00. Then you need to set the inputs to pull ups by writing 0xF0 to register 0x06. This will set those 4 lines to inputs and pull them high internally. This allows you to connect your buttons between the those GPIO lines and GND in order to monitor the dry contact closure generated by the buttons. Finally you can monitor the status of those inputs by reading the byte from register 0x09. That will contain the current status of all GPIO lines.

If you have any questions on that please let us know.

Thank you,
Travis Elliott

This is the code I’m trying to use:

#include <Wire.h>
#define Addr 0x20
const int buttonPin = 35;
int buttonState = 0;
int data;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Wire.begin();
Wire.beginTransmission(Addr);
Serial.println(“Setup”);
Wire.write(0x00);
Wire.write(0x00);
Wire.endTransmission();
pinMode(buttonPin, INPUT);
delay(500);
}

void loop() {
buttonState = digitalRead(buttonPin);
data = 0x01;
Wire.beginTransmission(Addr);
Wire.write(0x09);
Wire.write(data);
Wire.endTransmission();
Serial.println(“Button Pressed”);
} else {
data = 0x00;
Wire.beginTransmission(Addr);
Wire.write(0x09);
Wire.write(data);
Wire.endTransmission();
delay(500);
}
}

How could I set the direction of the IO lines from the button to the relays?

It’s going to be something like this:

#include <Wire.h>
#define Addr 0x20
const int buttonPin = 35;
int buttonState = 0;
int data;

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Wire.begin();
  //Set IO direction
  Wire.beginTransmission(Addr);
  Serial.println(“Setup”);
  Wire.write(0x00);
  Wire.write(0xF0);
  Wire.endTransmission();
  //Set Input PUll ups
  Wire.beginTransmission(Addr);
  Serial.println(“Setup”);
  Wire.write(0x06);
  Wire.write(0xF0);
  Wire.endTransmission();
  delay(500);
}

void loop() {
  Wire.beginTransmission(Addr);
  Wire.write(0x09);
  Wire.endTransmission();
  Wire.requestFrom(Addr,1);
  uint8_t inputBankStatus = Wire.read();
  if(inputBankStatus & 16){
    //Input 1 closed
  }esle{
    //Input 1 opened
  }
  if(inputBankStatus & 32){
    //Input 2 closed
  }esle{
    //Input 2 opened
  }
  if(inputBankStatus & 64){
    //Input 3 closed
  }esle{
    //Input 3 opened
  }
  if(inputBankStatus & 128){
    //Input 4 closed
  }esle{
    //Input 4 opened
  }
}

Hello,
It works like this for the first input:

#include <Wire.h>
#define Addr 0x20
const int buttonPin = 35;
int buttonState = 0;
int data;

void setup() {

Serial.begin(9600);
Wire.begin();
//Set IO direction
Wire.beginTransmission(Addr);
// Serial.println(“Setup”);
Wire.write(0x00);
Wire.write(0xF0);
Wire.endTransmission();
//Set Input PUll ups
Wire.beginTransmission(Addr);
//Serial.println(“Setup”);
Wire.write(0x06);
Wire.write(0xF0);
Wire.endTransmission();
delay(500);
}

void loop() {
Wire.beginTransmission(Addr);
Wire.write(0x09);
Wire.endTransmission();
Wire.requestFrom(Addr,1);
uint8_t inputBankStatus = Wire.read();
if(inputBankStatus & 16){
data = 0x00;
Wire.beginTransmission(Addr);
Wire.write(0x09);
Wire.write(data);
Wire.endTransmission();

}else{
data = 0x01;
Wire.beginTransmission(Addr);
Wire.write(0x09);
Wire.write(data);
Wire.endTransmission();
Serial.print(“Turning Relay “);
Serial.println(” ON”);
}
}

I’m happy it works but I wonder, could it be done in a more simple way? what are your thoughts?

I’m extremely thankful.