I am playing around with a project and i2c is just driving me nuts. I get occasional freezes of the whole ESP32 what is a show stopper.
As a rookie I want to learn and try to understand what is going wrong - so I bought a cheap scope and logic analyzer.
For pinning down the problems I started by setting up a simple test circuit with just ONE i2c device: a MCP23017 port expander.
Using the standard Adafruit library with the most simple code possible (see code at the end of the post). The code IS WORKING and the pin is turned on as expected. Now I had a look what is going on under the hood.
What I expected:
The slaves adress is 0x20 (as in the documentation as well as in the library as a constant #define MCP23017_ADDRESS 0x20).
Without going too far and pretend to understand what the library is doing exactly, I expected at least some traffic adressing 0x20 after the start condition. Lets say: "Start" -> "Address" (0100 0000) -> Read/Write -> Ack. Then whatever the library is reading/writing to that slave.
What I got instead:
Thats whats on the bus actually - no 0x20 but adressing to 0x40??
Code: Select all
0.000007666666667,I2C,Setup Write to [@ (0x40)] + ACK
0.000100166666667,I2C,'0' (0x00) + ACK
0.000192666666667,I2C,'255' (0xFF) + ACK
0.000377083333333,I2C,Setup Write to [@ (0x40)] + ACK
0.000469583333333,I2C,'1' (0x01) + ACK
0.000562000000000,I2C,'255' (0xFF) + ACK
0.000733166666667,I2C,Setup Write to [@ (0x40)] + ACK
0.000825583333333,I2C,'0' (0x00) + ACK
0.000994500000000,I2C,Setup Read to [A (0x41)] + ACK
0.001086916666667,I2C,'255' (0xFF) + NAK
0.001254083333333,I2C,Setup Write to [@ (0x40)] + ACK
0.001346583333333,I2C,'0' (0x00) + ACK
0.001439000000000,I2C,'254' (0xFE) + ACK
5.001864583333333,I2C,Setup Write to [@ (0x40)] + ACK
5.001957083333333,I2C,'20' (0x14) + ACK
5.002120500000000,I2C,Setup Read to [A (0x41)] + ACK
5.002213000000000,I2C,'1' (0x01) + NAK
5.002378166666666,I2C,Setup Write to [@ (0x40)] + ACK
5.002470666666667,I2C,'18' (0x12) + ACK
5.002563083333333,I2C,'1' (0x01) + ACK
Does this makes any sense? Any hint is highly apprectiated!
Code: Select all
#include <Wire.h>
#include "Adafruit_MCP23017.h"
// Basic pin reading and pullup test for the MCP23017 I/O expander
// public domain!
// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to ground (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)
// Output #0 is on pin 21 so connect an LED or whatever from that to ground
Adafruit_MCP23017 mcp;
void setup() {
mcp.begin(); // use default address 0
mcp.pinMode(0, OUTPUT);
delay(5000);
mcp.digitalWrite(0, HIGH);
}