Arduino

What Exactly is the Wire.Write() Function?

The Wire library in Arduino provides an easy way for exchanging data between I2C devices. The Wire library allows communication between two or more devices through a two-wire interface which is SDA and SCL. The Wire library has a function called “Wire.write()” that is used to send data from one device to another.

Here we will describe the Arduino Wire.write() function and its use in Arduino programming.

What is the Wire.write() Function

The Arduino Wire.write() is a function that helps Arduino boards to send data from one device to another through the I2C bus. The function takes a single argument, which is the data to be sent. The data can be in the form of an integer, a character, or an array of integers or characters.

The Wire.write() function sends data to the I2C bus in binary form. This means that the data is sent as a series of 1s and 0s. The receiving device interprets the data based on its own programming and converts it back into a usable format.

Syntax

The syntax of Wire.write() function is:

When an integer value needs to be passed use:

Wire.write(value);

When a string is to be transferred using I2C communication:

Wire.write(string);

When a specific data of such as arrays of bytes with specific length is to be passed use following syntax:

Wire.write(data, length);

Parameters

  • value: Single byte value.
  • string: String to send as bytes.
  • data: Data array to send.
  • length: No of bytes to transmit.

Return

Total read bytes from data.

How Does the Wire.write() Function Work

To use the Wire.write() function, you first need to initialize the I2C bus using the Wire.begin() function. This function sets up the communication between the two devices.

Once the I2C bus has been initialized, you can then use the Wire.write() function to send data. The function takes the data you want to send as its argument.

For example, to send a number 42 to another device, you would use the following code:

Wire.write(42);

If you want to send a string of characters, you can use an array of characters instead:

char myString[] = "Hello World";

Wire.write(myString);

The Wire.write() function can also be used to send multiple values at once. To do this, you can use an array of integers:

int myArray[] = {1, 2, 3, 4, 5};

Wire.write(myArray, sizeof(myArray));

In this example, the Wire.write() function sends the entire array of integers to the other device.

What is the I2C Address for Wire.write() Function

In Arduino to use the I2C communication between devices, you need to specify the device’s address and the data you want to send. The code for using the Wire.Write() function typically looks like this:

Wire.beginTransmission(address);

Wire.write(data);

Wire.endTransmission();

In this code, “address” is the address of the device you want to send data to, and “data” is the data you want to send. The Wire.beginTransmission() function initiates the transmission, the Wire.write() function sends the data, and the Wire.endTransmission() function finishes the transmission.

Following articles guide on how one can check the I2C address for devices and interfaced them with Arduino board:

Frequently Asked Questions (FAQs)

Q: What is the difference between Wire.write() and Wire.send()?

The Wire.write() function and the Wire.send() function are functionally equivalent. The only difference is in their names.

Q: Can I use the Wire.write() function to send data to a specific device on the I2C bus?

No, the Wire.write() function sends data to all devices on the I2C bus. If you want to send data to a specific device, you need to address that device using its unique address.

Q: Can I use the Wire.write() function to send data to an Arduino board from another microcontroller?

Yes, if the other microcontroller supports the I2C protocol and is connected to the same I2C bus as the Arduino board.

Q: What is the maximum data that can be transferred using the Wire.write() function?

The maximum data Wire.write() function can send is 32 bytes.

Q: How do I receive data sent using the Wire.write() function?

To receive data sent using the Wire.write() function, you need to define the Wire.read() on receiver Arduino code.

Conclusion

The Wire.write() function in the Wire library for Arduino is used to send data from one device to another through the I2C bus. By understanding the basics of the Wire library, you can start building your own projects that communicate with other devices and sensors.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.