akut
June 20, 2025, 11:58am
1
I'm trying to connect a MCP23017 to an Arduino via I2C but it doesen't work. There is the example code I'm trying to run.
// Blinks an LED attached to a MCP23XXX pin.
#include <Adafruit_MCP23X17.h>
#define LED_PIN 0 // MCP23XXX pin LED is attached to
Adafruit_MCP23X17 mcp;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("MCP23xxx Blink Test!");
// uncomment appropriate mcp.begin
if (!mcp.begin_I2C()) {
Serial.println("Error.");
while (1);
}
// configure pin for output
mcp.pinMode(LED_PIN, OUTPUT);
Serial.println("Looping...");
}
void loop() {
mcp.digitalWrite(LED_PIN, HIGH);
delay(500);
mcp.digitalWrite(LED_PIN, LOW);
delay(500);
}
And here is my setup. I'm trying to connect a Arduino Micro.
And I also tested with Arduino Uno and a second MCP23017 (both are: https://www.waveshare.com/mcp23017-io-expansion-board.htm )
In both cases the PWR on the MCP light up, so they have the needed voltage. And in both cases I get
MCP23xxx Blink Test!
Error.
in the serial monitor.
Can some one spot a mistake in the code or the set up? I took the code directly from the example folder of the "Adafruit_MCP23017_Arduino_Library"
PaulRB
June 20, 2025, 12:11pm
2
With i2c problems, the first thing to try is the i2c scanner code from the examples menu of the IDE. If that detects nothing, there is no point trying any other code.
akut
June 20, 2025, 12:17pm
3
14:15:57.828 -> Scanning...
14:15:57.828 -> I2C device found at address 0x27 !
14:15:57.828 -> done
14:15:57.828 ->
That is what I get when running the scanner.
@akut
And where did you use that address 0x27 or 39 decimal in your sketch?
try this change: if (!mcp.begin_I2C(0x27)) {
akut
June 20, 2025, 12:24pm
6
I found my mistake. I thought that the adress would be 0x20, which looked like the standard in all example codes that I could find. But since I have NOT shorten A0,A1,A2 to GND they are all set to HIGH, making the adress 0x27.
if (!mcp.begin_I2C(0x27)) {
This works now. Thanks for the hint
Then you need specify 0x27 as mcp.begin_I2C(0x27), because the default I2C address is 0x20.
#define MCP23XXX_INT_ERR 255 //!< Interrupt error
/**************************************************************************/
/*!
@brief Base class for all MCP23XXX variants.
*/
/**************************************************************************/
class Adafruit_MCP23XXX {
public:
// init
bool begin_I2C(uint8_t i2c_addr = MCP23XXX_ADDR, TwoWire *wire = &Wire);
bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI,
uint8_t _hw_addr = 0x00);
bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
int8_t mosi_pin, uint8_t _hw_addr = 0x00);
// main Arduino API methods
void pinMode(uint8_t pin, uint8_t mode);
uint8_t digitalRead(uint8_t pin);
void digitalWrite(uint8_t pin, uint8_t value);