← All course materialsLecture 06 · Digital temperature sensingReading notes →

Lecture 06: I²C and Digital Temperature

1 / 30
PHYS 351 · Lecture 0601

Lecture 06

I²C and Digital Temperature

Part 1 · serial vs parallel · Part 2 · the bus · Part 3 · rules on the wire · Part 4 · registers and smbus2 · Part 5 · bits to °C · Part 6 · in the lab

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0602

Part 1 · One byte, two ways to send it

Timing diagram of the byte 0x33 sent two ways. Top: eight data wires D7 to D0 each hold one bit during a single clock tick, marked by one strobe pulse, leaving the next seven ticks free. Bottom: one data wire carries the bits 0 0 1 1 0 0 1 1 one per tick, most-significant bit first, with a clock pulse in every tick, taking eight ticks.

Parallel: one wire per bit. All 8 bits arrive together.

Serial: the bits queue on one wire. A clock marks when to read each one.

Parallel costs 8× the wires. Serial costs 8× the ticks.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0603

Why a serial link can be faster

Eight parallel data wires switching at slightly different times. With a slow clock a wide window remains between the latest edge and the next transition in which all eight are settled and safe to read. With a clock three times faster the skew regions of successive transitions overlap and there is no moment when all eight wires are settled.
PATASATA
Year19862003
Data16 wires2 pairs
Plug40 pins7 pins
Speed133 MB/s600 MB/s

A parallel bus must wait for its slowest wire (skew). A serial lane has nothing to line up, though it has limits of its own.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0604

Inside the MCP9808: sensor, ADC, registers

MCP9808 datasheet functional block diagram: a band-gap temperature sensor feeds a delta-sigma ADC with selectable resolution of 0.5 to 0.0625 degrees; its result goes to the temperature register, alongside configuration, limit, ID and resolution registers selected by a register pointer, all reached through an SMBus/I2C interface on pins SDA and SCL, with address pins A0 to A2.

The datasheet's block diagram: a band-gap sensor makes a voltage, and a ΔΣ ADC turns it into a number every 30–250 ms.

The number waits in a register, one of eight.

The Pi never sees a voltage. It asks for a register over I²C.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0605

Synchronous: the clock travels with the data

Two timing diagrams. Top, UART: one transmit line carries a start bit, eight data bits least-significant first and a stop bit; the receiver samples the middle of each bit with its own clock, so both ends must agree on 9600 baud. Bottom, I2C: SDA carries eight bits most-significant first while SCL pulses with uneven periods; the receiver reads SDA on every SCL rising edge.

UART needs both ends to agree on the bit timing. I²C sends its clock on SCL.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0606

One bus, many chips, one address each

I2C bus schematic: a Raspberry Pi controller and three MCP9808 sensors all connect to the same two lines, SDA and SCL, which are each pulled up to 3.3 volts by a resistor. The sensors' address pins A2 A1 A0 are 000, 001 and 010, giving addresses 0x18, 0x19 and 0x1A.

Only the chip whose 7-bit address is called answers. Pins A2 A1 A0 set the last 3 bits.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0607

Nobody drives the line high

Open-drain SDA line in two states. The line is pulled up to 3.3 volts by a resistor and three N-channel MOSFETs, for the Pi, the sensor and another chip, can each connect it to ground. With all switches off the line sits near 3.3 volts, high. With the sensor's switch on, current flows through the pull-up and the switch and the line sits near 0 volts, low.

An N-MOSFET pulls SDA low.

Release it, and the pull-up raises SDA.

MCP9808 datasheet Figure 5-9: the Alert output is an internal N-channel MOSFET to ground with an external pull-up resistor R_PU to V_DD.
Same idea: ALERT output
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0608

The pull-up sets how fast the line rises

SDA voltage against time in microseconds. A chip pulls the line from 3.3 volts to 0 volts almost instantly, then releases it at 2 microseconds. With a 10 kilohm pull-up and 100 picofarads the line recovers with a 1 microsecond time constant and crosses the 2.31 volt input-high threshold about 1.2 microseconds later; with 1.5 kilohms, the Pi's 1.8 kilohm pull-up in parallel with the breakout's 10 kilohm one, the time constant is 0.15 microseconds and the crossing comes after about 0.18 microseconds. Capacitance is illustrative.

Falling: the MOSFET discharges the wire fast.

Rising: only the resistor recharges the wire's capacitance, τ=RC\tau = RC.

Pi (1.8 kΩ) ∥ breakout (10 kΩ) ≈ 1.5 kΩ.

Low costs 3.3 V / 1.5 kΩ ≈ 2.2 mA.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0609

Rule: SDA changes only while SCL is low

I2C data-valid rule for the bits 1 0 1 1 0. SDA changes only in the shaded intervals where SCL is low; during each SCL high interval SDA is held steady, and arrows at the SCL rising edges mark where the receiver reads the bit.

The Pi makes the clock. Whoever is sending sets SDA while SCL is low; the receiver reads it while SCL is high.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0610

START and STOP break the rule on purpose

A short I2C frame. START: with SCL high, SDA falls. Then three data bits 1 0 1, with SDA changing only while SCL is low. STOP: SCL rises and then SDA rises while SCL is high. Both lines are high when the bus is idle.

A data bit never moves SDA while SCL is high, so doing exactly that is an unmistakable marker: SDA falling = START, SDA rising = STOP. Idle bus: both lines high.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0611

Eight bits, then a ninth clock for the answer

One I2C byte with its acknowledge. After START the Pi sends 0 0 1 1 0 0 0 0, most-significant bit first, on clocks 1 to 8; on clock 9 the sensor holds SDA low as ACK. Line color shows who holds SDA: Pi, sensor, or nobody (the pull-up).

The Pi makes all 9 clocks. On clock 9 the sender lets go of SDA and the receiver answers: ACK or NAK. The color shows who holds SDA.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0612

ACK or NAK: who holds SDA low on clock 9?

Three versions of clock 9. ACK: the receiver pulls SDA low. NAK: nobody pulls SDA low, so the pull-up keeps it high, meaning no chip answered that address. NAK from the Pi: after receiving the last byte it leaves SDA high to say it wants no more.

Low means "got it". High can mean "nobody is there" (check the wiring and address) or, when the Pi is receiving, "that was the last byte I want".

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0613

The first byte: 7-bit address + read/write

The MCP9808 address byte: bits 7 to 4 are fixed at 0011, bits 3 to 1 are the address pins A2 A1 A0, and bit 0 is read (1) or write (0). With the pins at 000 the 7-bit address is 0011000 = 0x18, used by Linux and Python; on the wire the write byte is 0x30 and the read byte is 0x31.
MCP9808 datasheet Figure 4-1, device addressing: after START, SCL clocks 1 to 8 carry the address code 0 0 1 1, the pin-selected bits A2 A1 A0 and the read/write bit, and on clock 9 the MCP9808 responds with ACK.
Datasheet, Fig. 4-1

The address is 7 bits. The 8th bit says the direction.

0x18 in code, 0x30 or 0x31 on the wire.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0614

Your turn: decode this first byte

Unlabelled I2C trace for an exercise: START, then nine SCL clocks numbered 1 to 9 with SDA levels to be read at each clock, then STOP.

1. Which 7-bit address is called? 2. Read or write? 3. Who answers on clock 9, and why?

Read SDA at the middle of each SCL high. Our sensor sits at 0x18.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0615

Address picks the chip, pointer the register

Two-level selection. On the bus, the address 0x18 picks one chip out of three. Inside that MCP9808, the register pointer 0x05 picks the ambient-temperature register from the list 0x01 CONFIG, 0x02 T_UPPER, 0x03 T_LOWER, 0x04 T_CRIT, 0x05 T_A, 0x06 manufacturer ID 0x0054, 0x07 device ID 0x0400 and 0x08 resolution.

Two different numbers. 0x18 is on the bus and set by the board. 0x05 exists only inside the chip: the register pointer, which you write as the first data byte.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0616

Writing a register: address, pointer, value

Register-write transaction: START, 0x30 (address 0x18 plus write), ACK from the sensor, 0x08 (pointer to the resolution register), ACK, 0x01 (new value, 0.25 degree steps), ACK, STOP. All three bytes come from the Pi.

Example: set the resolution register (0x08) to 0x01. The sensor ACKs every byte.

bus.write_byte_data(0x18, 0x08, 0x01)  # address, register, value

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0617

Reading a register: set the pointer, then read

Register-read transaction in two parts. First the Pi sets the pointer: START, 0x30, sensor ACK, 0x05, sensor ACK. Then, after a repeated START with no STOP, the Pi sends 0x31, the sensor ACKs and sends the upper byte 0xC1, the Pi ACKs, the sensor sends the lower byte 0x1C, the Pi NAKs, and the Pi sends STOP.

Sr turns the bus around without a STOP. The upper byte comes first.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0618

The datasheet draws the same read, bit by bit

MCP9808 datasheet Figure 5-5, reading +25.25 degrees from the temperature register: START, address byte with write, ACK, T_A pointer 0000 0101, ACK; then START, address byte with read, ACK, MSB data 0000 0001, ACK from the master, LSB data 1001 0100, NAK from the master, STOP. A note says the pointer need not be sent again if it was already set.
Datasheet, Fig. 5-5: reading +25.25 °C

Same five bytes. Here the sensor sent 0x01 0x94 = 25.25 °C.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0619

smbus2: one call runs the whole read

How bus.read_i2c_block_data(0x18, 0x05, 2) maps onto the wire: the address 0x18 becomes both address bytes 0x30 and 0x31, the register 0x05 becomes the pointer byte, and the count 2 asks for the two data bytes 0xC1 and 0x1C, which the call returns as the list [0xC1, 0x1C] in wire order.

read_i2c_block_data(address, register, count). You give the 7-bit address; the Pi adds the R/W bit, the START, repeated START, ACKs and STOP for you.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0620

The same read, spelled out as two messages

The same read as two smbus2 messages: i2c_msg.write(0x18, [0x05]) covers the address byte and pointer, i2c_msg.read(0x18, 2) covers the read address byte and two data bytes, and bus.i2c_rdwr(write, read) sends them as one transaction with a repeated START between them and one STOP at the end.
from smbus2 import SMBus, i2c_msg

write = i2c_msg.write(0x18, [0x05])
read = i2c_msg.read(0x18, 2)
with SMBus(1) as bus:
    bus.i2c_rdwr(write, read)
hi, lo = list(read)      # 0xC1, 0x1C

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

i2c_rdwr is plain I²C: any mix of writes and reads in one transaction.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0621

read_word_data hands you the bytes swapped

Byte order. The sensor sends 0xC1 then 0x1C. read_i2c_block_data returns [0xC1, 0x1C], and shifting the first byte left by 8 gives 0xC11C, correct. read_word_data returns 0x1CC1 because SMBus words are low byte first; swapping the bytes gives 0xC11C.

SMBus defines a "word" as low byte first; the MCP9808 sends the high byte first. The provided mcp9808_mod.py uses read_word_data and swaps the bytes back.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0622

The 16 bits of the temperature register

The 16 bits of the MCP9808 ambient-temperature register. Bits 15 to 13 are alert flags (T at or above T_CRIT, above T_UPPER, below T_LOWER), bit 12 is the sign, bits 11 to 4 are whole degrees weighted 128 down to 1, and bits 3 to 0 are fractions weighted one half, one quarter, one eighth and one sixteenth of a degree. The upper byte is sent first.
MCP9808 datasheet Register 5-4, the ambient-temperature register: bits 15 to 13 compare T_A with T_CRIT, T_UPPER and T_LOWER, bit 12 is the sign, and bits 11 to 0 weigh 2 to the 7 down to 2 to the minus 4 degrees Celsius.
Datasheet, Register 5-4
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0623

Positive example: 0xC1 0x1C

Decoding 0xC1 0x1C: bits 1100 0001 0001 1100. The flag bits 1 1 0 are masked off, the sign bit is 0, the whole-degree bits give 16 + 1 = 17, and the fraction bits give one half plus one quarter = 0.75, so T = 17.75 degrees Celsius.

At power-up T_UPPER and T_CRIT are 0 °C, so a room reading sets both flags. Mask them first. Or count sixteenths: 0x011C = 284, and 284 / 16 = 17.75.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0624

Negative example: the sign bit weighs −256 °C

Decoding 0x3E 0xE4: bits 0011 1110 1110 0100. The flag bits are masked off; the sign bit is set and in two's complement weighs minus 256 degrees; the remaining bits sum to 128 + 64 + 32 + 8 + 4 + 2 + 0.25 = 238.25, so T = −256 + 238.25 = −17.75 degrees Celsius. In code: mask with 0x1FFF, subtract 8192, divide by 16.

Read all 13 bits as one signed number, fraction included: the sign bit weighs −256 °C and every other bit adds as usual. Flag 13 is set because −17.75 °C is below T_LOWER = 0 °C.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0625

Test the formula, even the datasheet's

MCP9808 datasheet Equation 5-1: for T at or above 0 degrees, T = UpperByte × 2^4 + LowerByte × 2^−4; for T below 0 degrees the datasheet writes T = 256 − (UpperByte × 2^4 + LowerByte × 2^−4).
Datasheet, Equation 5-1

Take 0x3E 0xE4, sign bit cleared: 14 × 16 + 228 / 16 = 238.25.

Datasheet: 256 − 238.25 = +17.75 °C. Wrong sign.

Correct: 238.25 − 256 = −17.75 °C.

Always check code against bytes whose answer you know.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0626

Resolution costs conversion time

MCP9808 datasheet Table 5-2, temperature conversion time: 0.5 degree resolution takes 30 ms (33 samples per second), 0.25 degree 65 ms (15), 0.125 degree 130 ms (7), and 0.0625 degree, the power-up default, 250 ms (4).
Datasheet, Table 5-2

Register 0x08, bits 1–0, sets the ADC step.

Power-up: 0x03, 0.0625 °C, 250 ms.

The datasheet contradicts itself (p. 24 says 0.25 °C). Read 0x08 and see.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0627

Task 3: which fraction bits give 0.125 °C steps?

Illustration of truncation: number lines with steps of 0.5, 0.25, 0.125 and 0.0625 degrees. Dropping lower bits from an ideal value of 17.80 gives 17.5 at the coarsest step and 17.75 at the finer steps; this is not a sensor accuracy specification.

Each fraction bit halves the step: ½, ¼, ⅛, 1/16 °C. Keep the bits you need, drop the rest with a mask or a shift.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0628

Wire it, then look for 18

Wiring from the Raspberry Pi header to the MCP9808 breakout: physical pin 1 (3.3 V) to Vdd, pin 6 (ground) to Gnd, pin 5 (SCL) to SCL and pin 3 (SDA) to SDA. The 5 V pins 2 and 4 are not used.

Power off while wiring.

18 in the grid: a chip ACKed address 0x18.

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- 18 -- -- -- -- -- -- --

Blank grid: check power, ground, SDA/SCL swapped, or I²C not enabled in raspi-config.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0629

First script: check the chip, then read T_A

from smbus2 import SMBus

ADDR = 0x18                 # 7-bit address

with SMBus(1) as bus:       # /dev/i2c-1
    mid = bus.read_i2c_block_data(ADDR, 0x06, 2)
    print(mid)              # [0, 84] = 0x0054
    hi, lo = bus.read_i2c_block_data(ADDR, 0x05, 2)
    print(hex(hi), hex(lo)) # 0xc1 0x1c, say

raw = hi << 8 | lo          # 0xc11c: now decode it

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

smbus2 is already installed on the lab Pis. If it is missing, ask a TA.

No 18: check power, wiring and address.

Wrong ID: check the chip, the pointer and the byte order.

Odd °C: check the masks and the sign.

Fix one layer before the next.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0630

Poll no faster than the sensor converts

Illustration of polling faster than conversion: the true temperature rises smoothly; the register updates in steps every 250 milliseconds; the Pi reads every 50 milliseconds, so each register value appears five times in a row.

At 0.0625 °C a conversion typically takes 250 ms (about 4 per second). Reading every 50 ms returns each value five times: more rows in your CSV, not more information.

© Ran Yang, Ph.D.Advanced Instrumentation

Use ← → to move, Home / End to jump, and F for fullscreen.

Figure descriptions

Slide 2 · Part 1 · One byte, two ways to send it

Timing diagram of the byte 0x33 sent two ways. Top: eight data wires D7 to D0 each hold one bit during a single clock tick, marked by one strobe pulse, leaving the next seven ticks free. Bottom: one data wire carries the bits 0 0 1 1 0 0 1 1 one per tick, most-significant bit first, with a clock pulse in every tick, taking eight ticks.

Slide 3 · Why a serial link can be faster

Eight parallel data wires switching at slightly different times. With a slow clock a wide window remains between the latest edge and the next transition in which all eight are settled and safe to read. With a clock three times faster the skew regions of successive transitions overlap and there is no moment when all eight wires are settled.

Slide 4 · Inside the MCP9808: sensor, ADC, registers

MCP9808 datasheet functional block diagram: a band-gap temperature sensor feeds a delta-sigma ADC with selectable resolution of 0.5 to 0.0625 degrees; its result goes to the temperature register, alongside configuration, limit, ID and resolution registers selected by a register pointer, all reached through an SMBus/I2C interface on pins SDA and SCL, with address pins A0 to A2.

Slide 5 · Synchronous: the clock travels with the data

Two timing diagrams. Top, UART: one transmit line carries a start bit, eight data bits least-significant first and a stop bit; the receiver samples the middle of each bit with its own clock, so both ends must agree on 9600 baud. Bottom, I2C: SDA carries eight bits most-significant first while SCL pulses with uneven periods; the receiver reads SDA on every SCL rising edge.

Slide 6 · One bus, many chips, one address each

I2C bus schematic: a Raspberry Pi controller and three MCP9808 sensors all connect to the same two lines, SDA and SCL, which are each pulled up to 3.3 volts by a resistor. The sensors' address pins A2 A1 A0 are 000, 001 and 010, giving addresses 0x18, 0x19 and 0x1A.

Slide 7 · Nobody drives the line high

Open-drain SDA line in two states. The line is pulled up to 3.3 volts by a resistor and three N-channel MOSFETs, for the Pi, the sensor and another chip, can each connect it to ground. With all switches off the line sits near 3.3 volts, high. With the sensor's switch on, current flows through the pull-up and the switch and the line sits near 0 volts, low.

MCP9808 datasheet Figure 5-9: the Alert output is an internal N-channel MOSFET to ground with an external pull-up resistor R_PU to V_DD.

Slide 8 · The pull-up sets how fast the line rises

SDA voltage against time in microseconds. A chip pulls the line from 3.3 volts to 0 volts almost instantly, then releases it at 2 microseconds. With a 10 kilohm pull-up and 100 picofarads the line recovers with a 1 microsecond time constant and crosses the 2.31 volt input-high threshold about 1.2 microseconds later; with 1.5 kilohms, the Pi's 1.8 kilohm pull-up in parallel with the breakout's 10 kilohm one, the time constant is 0.15 microseconds and the crossing comes after about 0.18 microseconds. Capacitance is illustrative.

Slide 9 · Rule: SDA changes only while SCL is low

I2C data-valid rule for the bits 1 0 1 1 0. SDA changes only in the shaded intervals where SCL is low; during each SCL high interval SDA is held steady, and arrows at the SCL rising edges mark where the receiver reads the bit.

Slide 10 · START and STOP break the rule on purpose

A short I2C frame. START: with SCL high, SDA falls. Then three data bits 1 0 1, with SDA changing only while SCL is low. STOP: SCL rises and then SDA rises while SCL is high. Both lines are high when the bus is idle.

Slide 11 · Eight bits, then a ninth clock for the answer

One I2C byte with its acknowledge. After START the Pi sends 0 0 1 1 0 0 0 0, most-significant bit first, on clocks 1 to 8; on clock 9 the sensor holds SDA low as ACK. Line color shows who holds SDA: Pi, sensor, or nobody (the pull-up).

Slide 12 · ACK or NAK: who holds SDA low on clock 9?

Three versions of clock 9. ACK: the receiver pulls SDA low. NAK: nobody pulls SDA low, so the pull-up keeps it high, meaning no chip answered that address. NAK from the Pi: after receiving the last byte it leaves SDA high to say it wants no more.

Slide 13 · The first byte: 7-bit address + read/write

The MCP9808 address byte: bits 7 to 4 are fixed at 0011, bits 3 to 1 are the address pins A2 A1 A0, and bit 0 is read (1) or write (0). With the pins at 000 the 7-bit address is 0011000 = 0x18, used by Linux and Python; on the wire the write byte is 0x30 and the read byte is 0x31.

MCP9808 datasheet Figure 4-1, device addressing: after START, SCL clocks 1 to 8 carry the address code 0 0 1 1, the pin-selected bits A2 A1 A0 and the read/write bit, and on clock 9 the MCP9808 responds with ACK.

Slide 14 · Your turn: decode this first byte

Unlabelled I2C trace for an exercise: START, then nine SCL clocks numbered 1 to 9 with SDA levels to be read at each clock, then STOP.

Slide 15 · Address picks the chip, pointer the register

Two-level selection. On the bus, the address 0x18 picks one chip out of three. Inside that MCP9808, the register pointer 0x05 picks the ambient-temperature register from the list 0x01 CONFIG, 0x02 T_UPPER, 0x03 T_LOWER, 0x04 T_CRIT, 0x05 T_A, 0x06 manufacturer ID 0x0054, 0x07 device ID 0x0400 and 0x08 resolution.

Slide 16 · Writing a register: address, pointer, value

Register-write transaction: START, 0x30 (address 0x18 plus write), ACK from the sensor, 0x08 (pointer to the resolution register), ACK, 0x01 (new value, 0.25 degree steps), ACK, STOP. All three bytes come from the Pi.

Slide 17 · Reading a register: set the pointer, then read

Register-read transaction in two parts. First the Pi sets the pointer: START, 0x30, sensor ACK, 0x05, sensor ACK. Then, after a repeated START with no STOP, the Pi sends 0x31, the sensor ACKs and sends the upper byte 0xC1, the Pi ACKs, the sensor sends the lower byte 0x1C, the Pi NAKs, and the Pi sends STOP.

Slide 18 · The datasheet draws the same read, bit by bit

MCP9808 datasheet Figure 5-5, reading +25.25 degrees from the temperature register: START, address byte with write, ACK, T_A pointer 0000 0101, ACK; then START, address byte with read, ACK, MSB data 0000 0001, ACK from the master, LSB data 1001 0100, NAK from the master, STOP. A note says the pointer need not be sent again if it was already set.

Slide 19 · smbus2: one call runs the whole read

How bus.read_i2c_block_data(0x18, 0x05, 2) maps onto the wire: the address 0x18 becomes both address bytes 0x30 and 0x31, the register 0x05 becomes the pointer byte, and the count 2 asks for the two data bytes 0xC1 and 0x1C, which the call returns as the list [0xC1, 0x1C] in wire order.

Slide 20 · The same read, spelled out as two messages

The same read as two smbus2 messages: i2c_msg.write(0x18, [0x05]) covers the address byte and pointer, i2c_msg.read(0x18, 2) covers the read address byte and two data bytes, and bus.i2c_rdwr(write, read) sends them as one transaction with a repeated START between them and one STOP at the end.

Slide 21 · read_word_data hands you the bytes swapped

Byte order. The sensor sends 0xC1 then 0x1C. read_i2c_block_data returns [0xC1, 0x1C], and shifting the first byte left by 8 gives 0xC11C, correct. read_word_data returns 0x1CC1 because SMBus words are low byte first; swapping the bytes gives 0xC11C.

Slide 22 · The 16 bits of the temperature register

The 16 bits of the MCP9808 ambient-temperature register. Bits 15 to 13 are alert flags (T at or above T_CRIT, above T_UPPER, below T_LOWER), bit 12 is the sign, bits 11 to 4 are whole degrees weighted 128 down to 1, and bits 3 to 0 are fractions weighted one half, one quarter, one eighth and one sixteenth of a degree. The upper byte is sent first.

MCP9808 datasheet Register 5-4, the ambient-temperature register: bits 15 to 13 compare T_A with T_CRIT, T_UPPER and T_LOWER, bit 12 is the sign, and bits 11 to 0 weigh 2 to the 7 down to 2 to the minus 4 degrees Celsius.

Slide 23 · Positive example: 0xC1 0x1C

Decoding 0xC1 0x1C: bits 1100 0001 0001 1100. The flag bits 1 1 0 are masked off, the sign bit is 0, the whole-degree bits give 16 + 1 = 17, and the fraction bits give one half plus one quarter = 0.75, so T = 17.75 degrees Celsius.

Slide 24 · Negative example: the sign bit weighs −256 °C

Decoding 0x3E 0xE4: bits 0011 1110 1110 0100. The flag bits are masked off; the sign bit is set and in two's complement weighs minus 256 degrees; the remaining bits sum to 128 + 64 + 32 + 8 + 4 + 2 + 0.25 = 238.25, so T = −256 + 238.25 = −17.75 degrees Celsius. In code: mask with 0x1FFF, subtract 8192, divide by 16.

Slide 25 · Test the formula, even the datasheet's

MCP9808 datasheet Equation 5-1: for T at or above 0 degrees, T = UpperByte × 2^4 + LowerByte × 2^−4; for T below 0 degrees the datasheet writes T = 256 − (UpperByte × 2^4 + LowerByte × 2^−4).

Slide 26 · Resolution costs conversion time

MCP9808 datasheet Table 5-2, temperature conversion time: 0.5 degree resolution takes 30 ms (33 samples per second), 0.25 degree 65 ms (15), 0.125 degree 130 ms (7), and 0.0625 degree, the power-up default, 250 ms (4).

Slide 27 · Task 3: which fraction bits give 0.125 °C steps?

Illustration of truncation: number lines with steps of 0.5, 0.25, 0.125 and 0.0625 degrees. Dropping lower bits from an ideal value of 17.80 gives 17.5 at the coarsest step and 17.75 at the finer steps; this is not a sensor accuracy specification.

Slide 28 · Wire it, then look for 18

Wiring from the Raspberry Pi header to the MCP9808 breakout: physical pin 1 (3.3 V) to Vdd, pin 6 (ground) to Gnd, pin 5 (SCL) to SCL and pin 3 (SDA) to SDA. The 5 V pins 2 and 4 are not used.

Slide 30 · Poll no faster than the sensor converts

Illustration of polling faster than conversion: the true temperature rises smoothly; the register updates in steps every 250 milliseconds; the Pi reads every 50 milliseconds, so each register value appears five times in a row.