MCUATmega328Philo5V5V01010101010101010176543210ackMCP9808 · 0x180x05C1740x0600540x0704001const int ADDR = 0x18;const int ADDR = 0x30;2Wire.beginTransmission(ADDR);3Wire.write(0x05); // T_A4Wire.endTransmission(false);5Wire.requestFrom(ADDR, 2);6byte hi = Wire.read();7byte lo = Wire.read();LOGIC ANALYSER · I2C DECODESDA · SCL probesSSrP0x300x050x310xC10x740x60ACKACKNACK23.25 °C
Back: the Uno's microcontroller, the MCP9808 sensor, SDA, SCL and the pull-upsFront: the sketch card and a logic analyserTokens: the bytes on SDA, the ACKs and NACKs, the clock on SCLAn Arduino Uno R3 (the controller) and an MCP9808 temperature sensor (the target, address 0x18) on SDA and SCL, with pull-ups to 5 V. To read register 0x05 the Uno sends START, 0x30, 0x05, a repeated START and 0x31; the sensor ACKs each byte, then sends 0xC1 0x74, which the Uno ACKs and NACKs: 23.25 °C. With the 8-bit byte 0x30 pasted as the address, nobody answers: a NACK, which the logic analyser decodes.

The bus

  1. 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.

Reading a register

Finding a bug

What happens

;; step 1/10 · bus
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

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. 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.