Description

write() writes data from a peripheral device in response to a request from a controller device, or queues bytes for transmission from a controller to a peripheral device (in-between calls to beginTransmission() and endTransmission()).

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

Syntax

  • Wire.write(value)
  • Wire.write(string)
  • Wire.write(data, length)

Parameters

  • value: a value to send as a single byte. Allowed data types: byte.
  • string: a string to send as a series of bytes. Allowed data types: char*.
  • data: an array of data to send as bytes. Allowed data types: byte[].
  • length: the number of bytes to transmit. Allowed data types: size_t.

Returns

The number of bytes written (reading this number is optional). Data type: size_t.

Example Code

#include <Wire.h>

byte val = 0;

void setup() {
  Wire.begin();  // Join I2C bus
}

void loop() {
  Wire.beginTransmission(44);  // Transmit to device number 44 (0x2C)
  Wire.write(val);             // Send value byte
  Wire.endTransmission();      // Stop transmitting

  val++;  // Increment value

  // If reached 64th position (max), start over from lowest value
  if (val == 64) {
    val = 0;
  }

  delay(500);
}