ENع
←I2C in firmware
Lesson 27 · UART, I2C and SPI in firmware

I2C in firmware: addresses, registers and ACKs

14 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 27 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. START: 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 low
  3. Address 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 bit
  4. ACK: 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)
  5. 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)
  6. 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)
  7. 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 kHz
  8. Bytes 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 °C
  9. A 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 again
  10. The 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

Quiz Unlocks after the last step.