I²C between 2 Arduinos sends additional receive event I hooked up two Arduino Nanos with I²C, using 3 wires between GND &lt;-> GND, A4 &lt;-> A4, A5 &lt;-> A5Ω and 1kΩ pull-up resistors on A4 &amp; A5.

When I use the following code to communicate between them I get a weird empty receive event.

master.cpp

#include &lt;Arduino.h&gt;
#include &lt;Wire.h&gt;

#define SERIAL_BAUD 9600

#define I2C_SLAVE_ADDRESS 8

void setup () {
    Wire.begin();
    Serial.begin(SERIAL_BAUD);
}

void getFromState (unsigned char state) {
    Wire.beginTransmission(I2C_SLAVE_ADDRESS);
    Serial.print("s");
    Serial.print(Wire.write(state));
    Serial.print("e");
    Serial.print(Wire.endTransmission());
    delay(5000);
    Wire.requestFrom(I2C_SLAVE_ADDRESS, 2);
    int c = (unsigned char) Wire.read();
    Wire.flush();
    Serial.print("r");
    Serial.println(c);
    Wire.endTransmission();
}

void loop () {
    Serial.println("loop");
    getFromState(0);
    delay(10000);
    getFromState(1);
    delay(10000);
}


slave.cpp

#include &lt;Arduino.h&gt;
#include &lt;Wire.h&gt;

#define SERIAL_BAUD 9600

#define I2C_ADDRESS 8

unsigned char state = 0;

void requestEvent () {
    Serial.print("Request ");
    Serial.println(state);
    Wire.write(state);
}

void receiveEvent (int amount) {
    Serial.print("a");
    Serial.print(amount);
    Serial.print("r");
    state = (unsigned char) Wire.read();
    Serial.println(state);
}

void setup() {
    Serial.begin(SERIAL_BAUD);
    Wire.begin(I2C_ADDRESS);
    Wire.onRequest(requestEvent);
    Wire.onReceive(receiveEvent);
}

void loop() {
    delay(10);
}


Serial Output Master After 1.5 Loops

loop
s1e0r0
s1e0r1
loop
s1e0r0
s1e0


Serial Output Slave After 1.5 Loops

a1r0
Request 0
a0r255
a1r1
Request 0
a0r255
a1r0
Request 0
a0r255
a1r1


Question

Why is there an empty receive event a0r255 after every request? Is that normal? Am I doing something wrong? Is this some sort of hardware issue?

EDIT: I opensourced this project here, take a look if you want to!
Your code has several issues:

For the master:


Your I2C transactions should be quick and not delayed with Serial.print()s like in getFromState()
You requested 2 bytes from the slave but you are reading only one from the buffer. Wire.flush() flushes the outgoing buffer, not the receive buffer. Use while (Wire.read() != -1); instead.
Its also advised that you use Wire.available() to iterate through the receive buffer when read()ing; its how you check if anything was received at all and how many bytes were received. 
After using requestFrom(), you shouldn't call endTransmission(). Its self-contained.


For the slave:


You should never call Serial.print() or delay() in requestEvent() or receiveEvent(); both functions are called from within ISRs and ISRs are meant to execute quickly and return. 
You are requesting 2 bytes from the slave in your master code, whereas your slave's requestEvent() writes only one byte.
In receiveEvent(), again its best to use either Wire.available() to iterate through the RX buffer or you use the variable amount. This way, you read everything in the buffer; you can also flush the RX buffer like I showed you before.


Finally those delays are rather long; 10ms would do, if they are even needed at all.