I2c does not work with Arduino UNO R4 WiFi

I want to connect a keypad to my Arduino UNO R4 WiFi via an MCP23017. But the Arduino does not find the MCP23017 when I connect it to i2c. When I did it with my Arduino UNO R3 it worked perfectly. I have already tried pull-up resistors and Wire1 in the code. This is the code:

#include <Wire.h>
#include <Adafruit_MCP23X17.h>

Adafruit_MCP23X17 mcp;

// 4x4 Keypad layout
char keymap[4][4] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

// Rows and columns of the keypad
const int rows[4] = {0, 1, 2, 3};  // GPA0 - GPA3
const int cols[4] = {4, 5, 6, 7};  // GPA4 - GPA7

void setup() {
  Serial.begin(9600);

  if (!mcp.begin_I2C()) {  // Initialize MCP23017 via I²C
    Serial.println("Error: MCP23017 not found!");
    while (1); // Stop program if no chip is detected
  }

  // Set rows as OUTPUT and HIGH
  for (int i = 0; i < 4; i++) {
    mcp.pinMode(rows[i], OUTPUT);
    mcp.digitalWrite(rows[i], HIGH);
  }

  // Set columns as INPUT_PULLUP
  for (int i = 0; i < 4; i++) {
    mcp.pinMode(cols[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int r = 0; r < 4; r++) {
    // Set current row to LOW
    mcp.digitalWrite(rows[r], LOW);

    for (int c = 0; c < 4; c++) {
      if (!mcp.digitalRead(cols[c])) {  // Key pressed (LOW)
        Serial.print("Key pressed: ");
        Serial.println(keymap[r][c]);
        delay(200); // Debounce delay
      }
    }

    // Set row back to HIGH
    mcp.digitalWrite(rows[r], HIGH);
  }
}

Post an annotated schematic showing exactly how you have wired your project. Be sure to show all connections, power, ground, power sources and any other hardware.


The next step is to use the I2C scanner and see if it finds it and at what port. If that does not solve it post an annotated schematic showing exactly how you have wired it. Be sure to show all power ground and power sources.

The I2C scanner does not recognize any device.
Output:
Scanning...
No I2C devices found

  • I2C needs pull-up resistors.

  • Add de-coupling to the chip on the solderless breadboard.

  • Always show us a good schematic of your proposed circuit.

I managed to make it work. I used the wrong pull-up resistors because I didn't realize I needed 5.1k resistors. Thanks anyway

1 Like

informative!

  • That's a very interesting comment
    :thinking: