Sub topic 4
How Devices Are Addressed
Assume you know the Arduino setup() and loop() functions, can use the Serial Monitor, and understand binary, hexadecimal, voltage, and resistors.
The 7-bit address
I2C uses one shared bus. Every target device on that bus needs an address so the controller can select it. An I2C address is normally a 7-bit number, from 0x00 to 0x7F.
The address is usually printed in hexadecimal. For example, a temperature sensor might use address 0x48. The controller sends this address after the START condition. Only the target with that address should respond with an acknowledge, or ACK.
Some addresses are reserved for special protocol functions. In normal Arduino projects, use addresses in the device's documented range rather than choosing an arbitrary value.
| Address form | Meaning |
|---|---|
| `0x48` | The 7-bit address used by the Arduino `Wire` library |
| `0x90` | The older 8-bit write-byte form of the same address |
| `0x91` | The older 8-bit read-byte form of the same address |
The Arduino Wire library expects the 7-bit form. If a datasheet lists 0x90 and 0x91, shift either value right by one bit to get 0x48. Passing 0x90 directly to Wire usually causes the device not to respond.
Address and read/write direction
The physical transaction includes an address byte. Its upper seven bits contain the target address, and its least significant bit indicates the direction:
0means the controller will write data to the target.1means the controller will read data from the target.
The direction bit is added by the I2C hardware or library. Your Arduino code supplies only the 7-bit address.
Choosing and changing addresses
Many devices have address pins. Connecting an address pin to ground, supply voltage, or another defined level changes the device address. This lets you place several identical devices on one bus.
Read the datasheet carefully. A device may offer only two or four address choices. Address pins must never be left floating because electrical noise can make the address change unpredictably.
Two targets must not normally share the same address. If they do, both may acknowledge and respond at the same time. Their data can collide, producing incorrect readings. You can solve this by changing an address pin, using an I2C multiplexer, or placing one device on a separate bus.
Common addressing errors
- Using an 8-bit datasheet address instead of a 7-bit
Wireaddress. - Forgetting that address pins change the default address.
- Leaving an address pin unconnected.
- Connecting two devices with the same address.
- Treating a missing ACK as proof that the device is damaged. Wiring, power, pull-ups, and voltage levels can also cause it.
Exercise
A datasheet lists write address 0xA0 and read address 0xA1. Which address should you pass to Wire.beginTransmission()?
Show answer
Pass 0x50. The two datasheet values include the direction bit; shifting 0xA0 right by one gives the 7-bit address 0x50.