Description

onReceive() registers a callback function to be called when a peripheral device receives a transmission from a controller device.

This function is part of the Wire library. See the Wire main page for more information.

Syntax

Wire.onReceive(handler)

Parameters

  • handler: the callback function to be called when data is received. It should have the following signature:
    • Parameter: int numBytes (the number of bytes received from the controller device).
    • Return: void (nothing).

Returns

Nothing.

Example Code

void setup() {
  Wire.begin(0x08);             // Join I2C bus as peripheral with address 0x08
  Wire.onReceive(receiveEvent); // Register callback function
}

void receiveEvent(int numBytes) {
  while (Wire.available()) {
    char c = Wire.read();
    // Process received byte
  }
}