Description

read() reads a byte that was transmitted from a peripheral device to a controller device after a call to requestFrom(), or transmitted from a controller device to a peripheral device (inside the onReceive() handler).

This function inherits from the Stream utility class. See the Wire main page for more information.

Syntax

Wire.read()

Parameters

None.

Returns

The next byte received, or -1 if none is available. Data type: int.

Example Code

#include <Wire.h>

void setup() {
  Wire.begin();        // Join I2C bus (address is optional for controller device)
  Serial.begin(9600);  // Start serial for output
}

void loop() {
  Wire.requestFrom(2, 6);  // Request 6 bytes from peripheral device #2

  // Peripheral may send less than requested
  while (Wire.available()) {
    char c = Wire.read();  // Receive a byte as character
    Serial.print(c);       // Print the character
  }

  delay(500);
}