Sub topic 7
Using the Wire Library
Prerequisites and the Wire model
This chapter assumes that you know Arduino setup() and loop(), can use the Serial Monitor, and understand basic voltage, current, resistors, binary, and hexadecimal numbers.
The Arduino Wire library provides an interface to I2C. I2C is a two-wire bus. The controller starts transactions, and target devices respond at 7-bit addresses. The library handles START conditions, address bits, ACK responses, and STOP conditions for you.
The two bus signals are:
SDA: serial data.SCL: serial clock.
Call Wire.begin() before using the bus. On a typical Arduino controller, the board's I2C pins are fixed by the board design.
Main Wire operations
The common operations are:
| Operation | Purpose |
|---|---|
| `beginTransmission(address)` | Start a write transaction for a 7-bit target address |
| `write(value)` | Add bytes to the outgoing message |
| `endTransmission()` | Send the message and finish with STOP |
| `requestFrom(address, count)` | Ask a target to return bytes |
| `available()` and `read()` | Check for and retrieve received bytes |
The address is written without the read/write bit. For example, use 0x68, not a shifted address such as 0xD0.
A write transaction often sends a register number followed by a value:
Wire.beginTransmission(0x68);
Wire.write(0x10); // register number
Wire.write(0x25); // value
byte result = Wire.endTransmission();A result of zero normally means that the target acknowledged the transaction. A nonzero result indicates a problem, such as an address with no responding device.
Scanning for connected devices
A scanner tests each valid 7-bit address. It is useful before writing device-specific code.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device found at 0x");
if (address < 16) {
Serial.print('0');
}
Serial.println(address, HEX);
}
}
Serial.println("Scan done");
delay(5000);
}Open the Serial Monitor at 9600 baud. A missing device may mean a wrong address, missing power, incorrect SDA or SCL wiring, or missing pull-up resistors.
Reading a register
Many devices use a register-pointer pattern. First, the controller writes the register number. Then it keeps control of the bus with a repeated START and requests the result.
bool readRegister16(uint8_t address, uint8_t reg, uint16_t &value) {
Wire.beginTransmission(address);
Wire.write(reg);
if (Wire.endTransmission(false) != 0) {
return false;
}
if (Wire.requestFrom(address, (uint8_t)2) != 2) {
return false;
}
value = (uint16_t)Wire.read() << 8;
value |= Wire.read();
return true;
}The false argument prevents a STOP after the register number. The target therefore sees one continuous read operation. Check every return value, especially when wiring is still being tested.
Exercise
Which Wire call should you use to obtain bytes from an I2C target, and how do you retrieve those bytes?
Show answer
Call Wire.requestFrom(address, count), then use Wire.available() and Wire.read() until the expected bytes have been received.