Sub topic 4
How devices are addressed
This chapter assumes you know what SDA and SCL are, why the bus needs pull-up resistors, and how to write a basic Arduino sketch that prints to the Serial Monitor. It also assumes you can read hexadecimal numbers. Here we look at how the controller picks one target out of many on a shared pair of wires.
Why I2C needs addresses
I2C has no chip-select line. Every target hears every byte on SDA. The only thing that tells a target "this message is for you" is an address, sent as the first byte of every transaction. A target whose address does not match must ignore the rest of the transaction and leave SDA alone.
This is the main saving over SPI. SPI needs one extra wire per device. I2C needs the same two wires for 1 device or for 20, as long as every device has a different address.
The 7-bit address
An I2C address is 7 bits, so there are 128 possible values (0x00 to 0x7F). The first byte on the bus is 8 bits long. The upper 7 bits are the address. The lowest bit is the R/W bit, which tells the target the direction of the transfer:
- 0 means the controller will write to the target.
- 1 means the controller will read from the target.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Meaning | A6 | A5 | A4 | A3 | A2 | A1 | A0 | R/W |
After this byte, the addressed target pulls SDA low during the ninth clock pulse. That is an ACK. If no target answers, SDA stays high, which is a NACK. The next chapter covers this framing in detail. Here the point is that an ACK on the address byte is how the controller learns a device is present.
The 7-bit versus 8-bit address trap
The most common addressing mistake comes from datasheets. Some list the 7-bit address. Others list the full 8-bit byte, with the R/W bit included, as a "write address" and a "read address".
Take a device whose datasheet says "write address 0xD0, read address 0xD1". Shift the write address right by one bit and you get the 7-bit address:
0xD0 = 1101 0000
0xD0 >> 1 = 110 1000 = 0x68The Arduino Wire library always expects the 7-bit address. It adds the R/W bit for you. If you pass 0xD0 to Wire.beginTransmission(), the device will not answer, because Wire treats 0xD0 as a 7-bit value and the result does not match what the device is listening for.
A quick check helps. If the address in a datasheet is larger than 0x7F, it is an 8-bit form. If it is 0x7F or less, it is probably 7-bit, but read the surrounding text to be sure.
Reserved addresses
Not all 128 values are available. The I2C specification reserves two ranges.
| Range (7-bit) | Use |
|---|---|
| 0x00 to 0x07 | Reserved: general call, START byte, CBUS, high-speed mode and other special purposes |
| 0x78 to 0x7F | Reserved: 10-bit addressing and future use |
| 0x08 to 0x77 | Available for ordinary devices (112 addresses) |
The chapter on bus scanning uses the full range 1 to 126, which is why a scanner can occasionally report a strange hit in a reserved block. The practical range for real devices is 0x08 to 0x77.
Address 0x00 is the general call address. A controller can send a message to it and every target that supports general call will accept it. Most hobby sensors ignore it, so you rarely need it.
10-bit addressing
The specification also defines 10-bit addresses, signaled by the reserved prefix 11110xx in the first byte. They exist to allow more devices on large systems. Almost all hobby parts use 7-bit addresses, and the standard Arduino Wire library does not support 10-bit addressing. You will seldom meet it, so the rest of this tutorial assumes 7-bit addresses.
How a device gets its address
A target's address comes from one of three sources.
- Fixed in silicon. The address cannot change. Many sensors work this way. For example, a given display controller may always answer at 0x3C. If you need two of these on one bus, you need a multiplexer.
- Set by address pins. The chip has one to three pins, often labelled A0, A1 and A2. You tie each pin to GND or to VCC, and the combination selects the lower bits of the address. The upper bits are fixed by the manufacturer.
- Set by software. Some devices let you write a new address to a register, sometimes stored in non-volatile memory. This is less common.
The TCA9548A multiplexer used later in this tutorial is a good example of the second kind. Its address is 0x70 plus the value of three pins:
| A2 | A1 | A0 | Address |
|---|---|---|---|
| GND | GND | GND | 0x70 |
| GND | GND | VCC | 0x71 |
| GND | VCC | GND | 0x72 |
| VCC | VCC | VCC | 0x77 |
Three pins give 2^3 = 8 combinations, so you can have up to eight of this chip on one bus. Always read the table in the datasheet for the exact part. Some chips have pins that can also connect to SDA or SCL, which gives four states per pin instead of two.
Addresses in Wire code
Wire takes the 7-bit address as the first argument of every transfer call. Here is the smallest useful example. It checks whether one specific device answers:
#include <Wire.h>
const uint8_t DEVICE_ADDRESS = 0x68; // 7-bit address
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(DEVICE_ADDRESS);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.println("Device answered with ACK");
} else {
Serial.print("No ACK, endTransmission returned ");
Serial.println(error);
}
}
void loop() {
}The return value of endTransmission() tells you what happened:
| Value | Meaning |
|---|---|
| 0 | Success: the target ACKed |
| 1 | Data too long for the transmit buffer |
| 2 | NACK on transmit of the address |
| 3 | NACK on transmit of data |
| 4 | Other error |
| 5 | Timeout (on cores that report it) |
A result of 2 is the one you see most often when an address is wrong or the device is not wired correctly. The code above sends no data bytes, so the address byte is the only thing being tested. This is exactly the trick the bus scanner uses.
Printing addresses in hexadecimal
Datasheets and scanners use hex, so print addresses in hex too. Use Serial.print(address, HEX). Note that this does not pad with zeros, so 0x08 prints as 8. Pad it yourself if you want a consistent look:
void printAddress(uint8_t address) {
Serial.print("0x");
if (address < 16) Serial.print('0');
Serial.print(address, HEX);
}Choosing addresses on a shared bus
When you plan a bus, list every device and its address before you wire anything. Follow these steps.
- Write down the 7-bit address of each device from its datasheet.
- Check for duplicates. Two devices with the same address will both ACK and both try to drive SDA. The result is garbled data, not a clean error.
- For each clash, look for address pins on one of the chips, or a software-configurable address.
- If neither is possible, use a multiplexer, or a second I2C bus on a second set of pins.
The same datasheet address can also appear under a different part number from another maker. Two unrelated chips can both default to 0x68. Planning on paper takes a few minutes and avoids an hour of debugging.
Common errors
- Using the 8-bit address. Shift right by one. See the trap above.
- Wrong address pin wiring. A floating address pin gives an unpredictable address. Always tie it to GND or VCC.
- Assuming a breakout board uses the chip's default. Many boards add resistors or solder jumpers that change the address. Check the board's documentation.
- Reading a scanner result as the chip's name. The scanner shows only an address. Several different parts can share one.
Exercise
A datasheet lists a device with "write address 0x3A and read address 0x3B". What 7-bit address do you pass to Wire.beginTransmission(), and what happens to the R/W bit?
Show answer
Shift 0x3A right by one bit: 0x3A is 0011 1010, so the 7-bit address is 001 1101, which is 0x1D. The Wire library appends the R/W bit itself: 0 for a write (giving 0x3A on the wire) and 1 for a read (giving 0x3B).
Exercise
You want to put four sensors that all have a fixed address of 0x29 on one bus. Why will the scanner show only one device, and what are two ways to solve the problem?
Show answer
All four answer the same address, so they ACK together and appear as one entry in the scan. You can place each sensor behind a channel of a multiplexer such as the TCA9548A, or you can put them on separate I2C buses. If the sensor had address pins, you could also give each a different address.