Mcp23017 floating button problem

Hello,
I wrote this sketch from the tutorial from tronixstuff : Adding an MCP23017 I/O Extender to Arduino or ESP8266 https://www.instructables.com/Adding-an-MCP23017-IO-Extender-to-Arduino-or-ESP82/

The sketch is below.
When I use the button with a pull-down or a pull-up resistor, all is working well.
Without pull-down or pull-up resistor, obviously, I have a floating problem.
Since my welds are already done (I was planning to use the Adafruit_MCP23017.h library), I would like to insert a pull-up like "mcp.pullUp (buttonPlay1Pin, HIGH);" or a pull-down.
My question : Is there a way to put all my buttons in pull-up (or pull-down) by default?
Thanks

#include <Wire.h> // Wire.h
byte input = 0;

void setup()
{
  Serial.begin(9600);
  Wire.begin(); // wake up I2C bus
  Wire.beginTransmission(0x20);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // set entire PORT A as output
  Wire.endTransmission();
}

void loop()
{
  // read the inputs of PORT B
  Wire.beginTransmission(0x20);
  Wire.write(0x13);
  Wire.endTransmission();
  Wire.requestFrom(0x20, 1);
  input = Wire.read();

  Serial.print("input, DEC : "); Serial.println(input, DEC); // debug

  // if PUSH BUTTON GPB6 (2) »» write LED GPA6 (64) ON
  if (input == 2) {          // GPB6 (2) Button

    // write that input to PORT A
    Wire.beginTransmission(0x20);
    Wire.write(0x12);
    Wire.write(64);    // LED ON (GPA6)
    Wire.endTransmission();
    delay(100); // for debounce
    Serial.println("LED ON"); // debug

    delay(1000);

    Wire.beginTransmission(0x20);
    Wire.write(0x12);
    Wire.write(00000000);    // ALL LEDS OFF
    Wire.endTransmission();
    delay(100); // for debounce
    Serial.println("ALL LEDS OFF"); // debug
  }
}

The pullup resistor registers (GPPUA and GPPUB) are at 0x0c and 0x0d. Write 0xff to each register to enable the internal pullups on all pins in each port. Or put a 1 at each bit position where you want the internal pullup enabled or 0 where the pullup is disabled.

See page 22 of the data sheet.

There are no internal pull down resistors.

Thank you @groundFungus. It works as I expected.

  //Port B
  Wire.beginTransmission(0x20);
  Wire.write(0x0d); // pullup resistor register GPPUB
  Wire.write(0xff); // enable the internal pullups on all pins in each port
  Wire.endTransmission();

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