I have a 0.96" I2C screen, and an MCP23017 multiplexer IC on the same I2C bus.
Both devices are visible on the bus, as I have checked with an I2C sniffer.
The screen will not initialise and the code hangs when the line 'Wire.requestFrom(0x24,1);' is called to retrieve data from the MCP23017.
I am at work, so don't have the code to hand... but I have been messing around with this for a few evening now and not fixed it yet. From memory and Google...
Wire.begin(); //Set this Arduino as master I2c device Wire.beginTransmission(0x24); //Setup MCP23016 controller for inputs 1-16 Wire.write(0x06); //Set to pin direction setting mode Wire.write(0xFF); //Set controller IODIR0 to all input Wire.write(0xFF); //Set controller IODIR1 to all input Wire.endTransmission();
This sets up the MCP23017 to read all 16 input pins. I know this is correct, as i have used this before in several other projects (in fact, I lifted it directly from a working routine).
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); //Initialize the display with the I2C addr 0x3c
I set up the screen (using the Adafruit_SSD1306.h library).
Next would come the code to display my screen. This all works fine if I never call the line 'Wire.requestFrom(0x24,1);'.
Later in the code, I request the data from the MCP23017
Wire.beginTransmission(0x24); // Select MCP23017 device Wire.write(0x00); // Select bank GP0 Wire.endTransmission(); delay(50); Wire.requestFrom(0x24,1); // Request the state of the GP0 inputs B0to8_state = Wire.read();
This is the routine I always use to read the MCP23017.... but in this case, it's clashing with the screen comms.
If I comment out the line 'Wire.requestFrom(0x24,1);', then the issue goes away (but obviously I don't get my byte from the MCP23017).
With this line in place anywhere in the code, the routine will not run at all.
I have tried all manner of Wire.ends, I2C read loops to clear the bus, Wire.requestFrom(0x24,1,false); etc to try and clear the problem. No luck.