I2C in firmware: addresses, registers and ACKs
About this lesson
Write the code that reads a register over I2C. An Arduino Uno R3 asks an MCP9808 temperature sensor at address 0x18 for its temperature register, 0x05: START, the address byte, the register pointer, a repeated START, the address again for reading, two data bytes and STOP, with an ACK or NACK after every byte. You turn the bytes into 23.25 °C, then meet the most common I2C bug, a wrong address, and see how a logic analyser shows it. The Hardware lesson “Serial buses: UART, I2C and SPI” explains the wires and pull-ups, and the Hardware lesson “Test and debug” explains what a logic analyser is; here we stay with the firmware.
Two wires, many chips
The Uno (the controller) and an MCP9808 temperature sensor (a target) share two wires: SDA for data, SCL for the clock. Pull-up resistors hold both at 5 V; a chip can only pull a wire low. The sensor answers to address 0x18.
controller Arduino Uno R3 (ATmega328P) target MCP9808, address 0x18 = 0011000 SDA pin A4 data, both directions SCL pin A5 clock, from the controller pull-ups keep both wires high when idle open drain chips pull low or let go, never drive high
Read the lesson step by step
Two wires, many chips
The Uno (the controller) and an MCP9808 temperature sensor (a target) share two wires: SDA for data, SCL for the clock. Pull-up resistors hold both at 5 V; a chip can only pull a wire low. The sensor answers to address 0x18.
controller Arduino Uno R3 (ATmega328P) target MCP9808, address 0x18 = 0011000 SDA pin A4 data, both directions SCL pin A5 clock, from the controller pull-ups keep both wires high when idle open drain chips pull low or let go, never drive highSTART: SDA falls first
Wire.beginTransmission() only fills a buffer in RAM. endTransmission() sends it, and every transfer begins with START: the Uno pulls SDA low while SCL is still high. Every chip on the bus starts listening.
line 2 Wire.beginTransmission(ADDR); buffer only line 4 Wire.endTransmission(false); sends it START SDA high -> low while SCL is high STOP SDA low -> high while SCL is high any other time, SDA only changes while SCL is lowAddress byte: 0x30
The address 0x18 is seven bits: 0011000. Wire shifts it left and adds R/W = 0 for write, making 0x30. Eight bits go out, most significant first, one per SCL pulse. A lit lamp is 1: SDA left high.
ADDR 0x18 = 0011000 (7 bits) << 1 | W 0x30 = 00110000 (R/W = 0, write) MSB first, one bit per SCL pulse 100 kHz -> 10 µs per bitACK: the ninth bit
On a ninth clock pulse the Uno lets go of SDA. The sensor recognises its address and pulls SDA low: that low bit is the ACK, meaning "I am here, go on". If no chip pulls, SDA stays high: a NACK.
clock 9 the controller releases SDA ACK the receiver pulls SDA low -> bit 0 NACK nobody pulls, the pull-up wins -> bit 1 frame 00110000 + 0 (ACK)Point at register 0x05
Inside, the sensor keeps numbered registers. Wire.write(0x05) sends the register pointer: 0x05 is T_A, the ambient temperature. The sensor stores the number and ACKs again. Nothing is read yet.
line 3 Wire.write(0x05); reg 0x05 T_A 0xC174 reg 0x06 MFR 0x0054 reg 0x07 DEV 0x0400 frame 00000101 + 0 (ACK)Repeated START, then read
endTransmission(false) ends with a repeated START instead of a STOP, so the Uno keeps the bus. requestFrom(ADDR, 2) sends the address again with R/W = 1: 0x31. The sensor ACKs and gets ready to talk.
line 4 Wire.endTransmission(false); -> Sr, no STOP line 5 Wire.requestFrom(ADDR, 2); << 1 | R 0x31 = 00110001 (R/W = 1, read) frame 00110001 + 0 (ACK)Two bytes: 0xC1 0x74
Now the sensor drives SDA: 0xC1, then 0x74. The Uno ACKs the first byte to ask for more, and NACKs the last to say "enough". Then STOP: SDA rises while SCL is high, and the bus is free.
byte 1 0xC1 = 11000001 Uno sends ACK -> hi byte 2 0x74 = 01110100 Uno sends NACK -> lo STOP SDA low -> high while SCL is high lamps fill left to right in time order (bit 7 first), not by position on the wire 45 clock pulses = 450 µs at 100 kHzBytes to 23.25 °C
The top three bits of hi are alarm flags (two are set: the limits start at 0). hi & 0x1F clears them: 0x01. lo is 0x74 = 116. t = 1 × 16 + 116 / 16 = 16 + 7.25 = 23.25 °C.
hi 0xC1 = 11000001 bit 7 T_A >= T_CRIT, bit 6 T_A > T_UPPER, bit 5 T_A < T_LOWER hi & 0x1F 0x01 (bit 4 = sign = 0) t 1 * 16 + 116 / 16 = 23.25 °CA bug: nobody answers
A common slip: copying 0x30, the 8-bit write byte, as the address. Wire shifts it again and sends 0x60. No chip has address 0x30, so SDA stays high on the ninth clock: NACK. endTransmission returns 2.
line 1 const int ADDR = 0x30; sent 0x30 << 1 = 0x60 = 01100000 clock 9 nobody pulls SDA low -> NACK endTransmission() returns 2: NACK on the address the sketch prints 2 and tries againThe analyser shows why
A logic analyser decodes the two wires: "S 0x30 W NACK". Wire wants the 7-bit address, so ADDR must be 0x18. Fixed, the analyser shows ACKs again and the sketch prints 23.25.
bug S 0x30 W NACK P fix S 0x18 W ACK 0x05 ACK Sr 0x18 R ACK 0xC1 ACK 0x74 NACK P Wire: 7-bit addresses; datasheets often print 8-bit bytes
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A datasheet gives a sensor's 7-bit address as 0x18. Which address byte does the controller send when it writes to it?
The address is shifted left one place and the R/W bit is added: 0x18 << 1 = 0x30, and R/W = 0 for a write. Wire does this for you, so you pass it 0x18.
02During the ninth clock pulse of a byte, SDA stays high because no chip pulls it low. What is that?
The receiver acknowledges by pulling SDA low on the ninth clock. If nobody pulls, the pull-up keeps SDA high, and a high ninth bit is a NACK.
03Why does the sketch call Wire.endTransmission(false) after writing the register number 0x05?
With false, endTransmission() sends a restart instead of a stop, so the controller keeps the bus and goes straight on to requestFrom(), as the MCP9808 datasheet's read example does.
04The Uno reads two bytes from the sensor. What does it send after the second, last byte?
When the controller is receiving, it ACKs every byte except the last, which it NACKs; then it sends STOP. Wire's requestFrom() does this for you.
05ADDR is set to 0x30 and Wire.endTransmission() returns 2. What does that mean?
2 means a NACK on the address. 0x30 is the sensor's 8-bit write byte, not its address; Wire shifts it again and sends 0x60, which no chip answers. Use the 7-bit address, 0x18.
Glossary
- Controller and target
- The I2C controller (the microcontroller here) drives the clock and starts every transfer; a target (the sensor) answers when its address is called.
- 7-bit address
- The number a target answers to. It is sent shifted left one place, with the R/W bit in the lowest place.
- R/W bit
- The eighth bit of the address byte: 0 means the controller will write, 1 means it will read.
- ACK and NACK
- The ninth bit after each byte. The receiver pulls SDA low to acknowledge (ACK); SDA left high is a not-acknowledge (NACK).
- START, repeated START and STOP
- START is SDA falling while SCL is high and begins a transfer; STOP is SDA rising while SCL is high and frees the bus; a repeated START begins a new transfer without freeing the bus.
- Register pointer
- The register number the controller writes first, telling the target which of its registers the next read or write uses.
- Pull-up resistor
- A resistor from a bus wire to the supply that holds the wire high when no chip pulls it low.
- Logic analyser
- A tool that records digital wires over time and can decode them into bytes, addresses, ACKs and NACKs.
Notes and sources
- The card shows only the read. The whole sketch also calls Wire.begin() in setup(), checks the value endTransmission() returns, converts the two bytes and prints the temperature once a second.
- Every I2C bus needs pull-up resistors on SDA and SCL, and many sensor breakout boards already carry them. Wire.begin() also switches on the ATmega328P's internal pull-ups, but a real bus should have its own resistors; section 7.1 of the NXP I2C manual (UM10204) shows how to size them for the bus speed and wiring.
- The MCP9808 compares each reading with three limit registers (critical, upper and lower), which are 0 at power on. A room above 0 °C is therefore at or above T_CRIT and above T_UPPER, so bits 15 and 14 of T_A read 1 until you set real limits. Below 0 °C the sign bit (0x10 in hi) is set and the sketch subtracts 256.
- The Uno's Wire library clocks the bus at 100 kHz (Standard-mode) unless you call Wire.setClock(); the MCP9808 accepts up to 400 kHz.
- The MCP9808's address is 0011 followed by its A2, A1 and A0 pins, so 0x18 is the address with all three tied to ground; up to eight can share one bus.
- Microchip MCP9808 datasheet (DS25095A): addressing, ACK/NAK, registers, Example 5-1
- NXP UM10204: I2C-bus specification and user manual, Rev. 7.0
- Arduino language reference: Wire (source)
- Arduino language reference: Wire.endTransmission() (source)
- Arduino language reference: Wire.requestFrom() (source)
- ArduinoCore-avr Wire: twi.h (100 kHz default clock)
- ArduinoCore-avr Wire: twi.c (address byte, return codes)
Quiz Unlocks after the last step.