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.



















![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.](/_astro/i2c-smbus2-block.DViPhC5U_1sO0p8.webp)
![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.](/_astro/i2c-smbus2-rdwr.p86DcXVo_W7a4z.webp)
![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.](/_astro/i2c-word-swap.DtXDfuTq_ZOXoOd.webp)








