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);
}
}