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:
When a string is to be transferred using I2C communication:
When a specific data of such as arrays of bytes with specific length is to be passed use following syntax:
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:
If you want to send a string of characters, you can use an array of characters instead:
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:
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.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:
- What is an I2C bus in Arduino?
- How to interface LCD with Arduino using I2C module
- How to Use Multiple I2C devices with Arduino
- How to scan I2C Address using Arduino Nano
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.