Wire (I2C)
write()
Queues bytes for transmission from a master to slave device (in-between calls to beginTransmission() and endTransmission()), or writes data from a slave device in response to a request from a master.
The default buffer size is 32 bytes; writing bytes beyond 32 before calling endTransmission() will be ignored. The buffer size can be increased by using acquireWireBuffer().
// SYNTAX
Wire.write(value);
Wire.write(string);
Wire.write(data, length);
// PROTOTYPE
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *, size_t);
Parameters:
value: a value to send as a single bytestring: a string to send as a series of bytesdata: an array of data to send as byteslength: the number of bytes to transmit (Max. 32)
Returns: byte
write() will return the number of bytes written, though reading that number is optional.
Because the Wire class inherits from Stream you can also use methods from the Print classes to format output.
// EXAMPLE USAGE
// Master Writer running on Device No.1 (Use with corresponding Slave Reader running on Device No.2)
void setup() {
Wire.begin(); // join i2c bus as master
}
byte x = 0;
void loop() {
Wire.beginTransmission(4); // transmit to slave device #4
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}