How to Download and Use the Wire.h Library for Arduino
The Wire.h library is a useful tool for communicating with I2C/TWI devices using the Arduino platform. I2C/TWI is a two-wire serial protocol that allows multiple devices to share the same bus. In this article, we will show you how to download and use the Wire.h library for Arduino.
Step 1: Download the Wire.h Library
The Wire.h library is included in the Arduino IDE by default, so you don’t need to download it separately. However, if you want to update it or use a different version, you can find it on GitHub or on the Arduino Reference page. To download it from GitHub, follow these steps:
- Go to https://github.com/codebendercc/arduino-library-files/blob/master/libraries/Wire/Wire.h
- Click on the “Raw” button to view the source code of the library.
- Right-click on the page and select “Save as” to save the file as “Wire.h” on your computer.
- Copy the file to your Arduino libraries folder, which is usually located at
C:\Users\username\Documents\Arduino\librarieson Windows or
Step 2: Include the Wire.h Library in Your Sketch
To use the Wire.h library in your sketch, you need to include it at the top of your code with this line:
This will allow you to access the functions and variables of the library. You can also use other libraries that depend on the Wire.h library, such as the LiquidCrystal_I2C library for LCD displays.
Step 3: Initialize the Wire.h Library
Before you can communicate with I2C/TWI devices, you need to initialize the Wire.h library with this line:
This will set up the SDA (data) and SCL (clock) pins as inputs and enable the internal pull-up resistors. You can also specify an address for your Arduino device if you want it to act as a slave on the I2C/TWI bus. For example:
This will assign the address 8 to your Arduino device. The address range is from 8 to 127, as 0 to 7 are reserved for special purposes.
Step 4: Communicate with I2C/TWI Devices
Once you have initialized the Wire.h library, you can communicate with I2C/TWI devices using these functions:
Wire.beginTransmission(address): This function starts a transmission to a device with a given address. You need to call this function before sending any data.
Wire.write(data): This function sends one byte of data to the device. You can also send an array of bytes or a string.
Wire.endTransmission(stop): This function ends a transmission and optionally sends a stop bit. The stop bit indicates that no more data will be sent and releases the bus. The stop parameter can be either
false. If you set it to
false, you can start another transmission without sending a stop bit, which is useful for repeated starts.
Wire.requestFrom(address, length, stop): This function requests data from a device with a given address. The length parameter specifies how many bytes of data you want to receive. The stop parameter works the same way as in
Wire.endTransmission(). This function returns the number of bytes available for reading.
Wire.available(): This function returns how many bytes of data are available for reading.
Wire.read(): This function