← PHYS 351Lecture 06 · Digital temperature sensingReading notes →

Lecture 06: Digital temperature sensing

1 / 30
PHYS 351 · Lecture 0601

Lecture 06

Serial Protocols, I2C and the MCP9808

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

Today, in one line

A digital sensor does not send you a temperature.

It sends bits from a numbered register, in a format the datasheet defines.

The protocol delivers the bits. The datasheet gives them meaning.

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

Where each task points

TaskYou doIdea
1lab directory; enable I2C in raspi-config; rebootbringing the bus up
2wire with the Pi off; i2cdetect -y 1 shows 18; provided module; note its limitbus, address, resolution
3write read_temp_high_res() for exactly 0.125C0.125\degC, sign handledregister layout, decoding
4^\circC and ^\circF; a warning threshold; 100+ points; plot; CSVreading to dataset
5optional live scrolling plot

VDDV_{DD} goes straight to the Pi’s 3.3 V. The hard part is the conversation.

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

Serial communication

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

Two ways to move a byte

Eight-bit parallel transfer: bits D7 through D0 travel simultaneously on eight separate signal lines between devices.
Parallel: eight wires, one tick

Eight-bit serial transfer: bits of a byte travel one after another on a single signal line, beginning with the most-significant bit in this illustration.
Serial: one wire, eight ticks, MSB first

Which is faster?

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

Serial won

InterfaceYearTypeMB per second
PATA, Parallel ATA1986parallel66 / 100 / 133
SATA, Serial ATA2003serial150 / 300 / 600

Skew: eight bits must arrive together; the worst wire sets the clock.

Crosstalk: eight lines switching together couple into each other.

Pins: the scarcest resource on a 40-pin header.

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

Asynchronous or synchronous

FamilyHow timing is agreedExamples
Asynchronousno clock sent; both ends preset to one baud rateUART, USB. Wrong baud = garbage
Synchronousa clock line travels with the dataI2C (Lab 6), SPI (Lab 8)

One extra wire buys freedom from a baud-rate agreement.

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

The I2C bus

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

Two wires, any number of devices

Shared I²C bus: a controller and multiple devices connect to SDA and SCL. Both lines are pulled up to 3.3 V; devices signal low by pulling a line toward ground.
SDA carries data, SCL the clock. The Pi starts every transaction.

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

Open drain, pull-ups, active low

Every device can only pull a line down. Pull-up resistors pull it up.

Two devices can never fight: the worst case is both pulling low.

Idle is both lines high. Low wins. No pull-ups, no bus.

The Adafruit breakout and the Pi both carry pull-ups. You add none.

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

START: SDA falls while SCL is high

I²C start condition: SDA falls from high to low while SCL remains high.
The controller claims the bus.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0612

STOP: SCL rises, then SDA rises

I²C stop condition: SCL is high when SDA rises from low to high, releasing the bus.
The bus is released. Both lines idle high.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0613

The rule that makes both unmistakable

During data

SDA holds steady while SCL is high. SDA changes only while SCL is low. The receiver samples on the rising edge.

So an SDA transition while SCL is high cannot be data.

It can only be START or STOP. One rule gives sampling and framing.

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

Nine clocks per byte: the receiver answers

I²C byte followed by an acknowledgement: the receiver holds SDA low on the ninth clock, indicating that more data may follow without a stop condition.
Bit 9 low = ACK: received, send more.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0615

NAK: nobody pulled the line down

I²C byte followed by a negative acknowledgement: SDA is high on the ninth clock, signaling completion; a stop condition ends the transaction.
Bit 9 high = NAK. A missing device answers NAK automatically, by doing nothing.
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0616

Address byte: 7 bits plus a direction

I²C read transaction: start condition, seven address bits sent most-significant first, read bit equal to one, acknowledgement, and repeated start. SDA carries data and SCL clocks each bit.

Bit 0 = 1 read, 0 write. MCP9808: 7-bit 0x18; shifted, 0x30 write, 0x31 read.

Linux and Python use the 7-bit form.

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

A complete register read

  1. START
  2. device address + write \rightarrow ACK
  3. register pointer \rightarrow ACK
  4. repeated START
  5. device address + read \rightarrow ACK
  6. first byte in; controller sends ACK: “another”
  7. second byte in; controller sends NAK: “enough”
  8. STOP
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0618

The MCP9808

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

Four blocks, four wires

MCP9808 temperature-sensor breakout board with labeled Vdd, ground, SCL, SDA, alert, and address-selection pins A0, A1, and A2. Photo: Adafruit.
sensor \cdot ADC \cdot registers \cdot I2C

SensorPi
VDD3.3 V, physical 1 or 17not 5 V
GNDany ground
SDABCM 2, physical 3
SCLBCM 3, physical 5

Alert, A0–A2: leave unconnected.

Wire with the Pi off. VDD/GND swapped kills the sensor.

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

The register map

RegisterPointerAccessContents / power-up
Configuration0x01R/W0x0000
T upper / lower / critical0x02 / 0x03 / 0x04R/W0x0000
Ambient temperature TAT_A0x05Rthe measurement
Manufacturer ID0x06R0x0054, fixed
Device ID / revision0x07R0x0400, fixed
Resolution0x08R/W0x03 at power-up
Best debugging trick in this lab

Read 0x06 first. 0x0054 back proves wiring, address and library at once.

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

The ambient temperature register, bit by bit

bit15141312111098
meansTA ⁣ ⁣TcritT_A\!\ge\!T_{\text{crit}}TA ⁣> ⁣TupT_A\!>\!T_{\text{up}}TA ⁣< ⁣TlowT_A\!<\!T_{\text{low}}sign272^7262^6252^5242^4
bit76543210
means232^3222^2212^1202^00.50.50.250.250.1250.1250.06250.0625

Three flags, a sign, eight integer bits, four fraction bits.

Which fraction bits must you keep for exactly 0.125C0.125\degC? Derive it here. The wrong count scores zero.

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

Decode +17.75C+17.75\degC: upper 0xC1, lower 0x1C

1100 0001 0001 1100

Strip the flags \cdot upper nibble up by 4 \cdot lower byte down by 4 \cdot integer 16+116+1 \cdot fraction 0.5+0.250.5+0.25

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

Decode 17.75C-17.75\degC: upper 0x3E, lower 0xFC

0011 1110 1111 1100   bit 12 set: below zero, two’s complement.

Assemble 224+15=239224 + 15 = 239 \cdot 256239=17256 - 239 = 17 \cdot fraction 0.75 \cdot negate. Room temperature never tests this path.

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

Resolution register 0x08: finer costs time

Bits 1–0ResolutionConversion
000.5C0.5\degC30 msfastest
010.25C0.25\degC65 ms
100.125C0.125\degC130 ms
110.0625C0.0625\degC250 mspower-up default (0x03)

Two “resolutions”: what the hardware measures, and how many fraction bits your code keeps. Task 3 is the second.

Poll faster than the conversion and you re-read the same value.

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

Bringing the bus up (no internet needed)

sudo raspi-config        # Interface Options -> I2C; reboot
ls /dev/i2c-*            # the bus device exists?
i2cdetect -y 1           # the MCP9808 appears as 18
i2cget -y 1 0x18 0x06 w  # manufacturer ID

Empty grid: VDD and GND first, then SDA/SCL, then swapped.

Run i2cdetect before one line of Python.

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

From a reading to a dataset (Task 4)

TF=95TC+32T_{^\circ\mathrm{F}} = \tfrac{9}{5}\,T_{\degC} + 32
  • threshold in a named constant; demonstrate it with a finger
  • perf_counter() for elapsed time; collect in a list, plot afterwards
  • CSV headers exactly as specified; open it and look
  • warm the sensor mid-run: rise, peak, decay
© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0627

A debugging order that works

1. /dev/i2c-1 exists? No: enable, reboot.

2. i2cdetect shows 18? No: wiring or power, not code.

3. Register 0x06 reads 0x0054? No: byte order or library use.

4. Print the two raw bytes in hex. Decode by hand.

5. Both signs handled? Feed it the known byte pairs.

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

Destroys hardware / wastes your afternoon

Destroys hardware

VDD and GND swapped
5 V on the sensor
wiring a live bus

Wastes your afternoon

SDA and SCL swapped
7-bit vs 8-bit address
bytes combined in the wrong order
flags not masked off
sampling faster than the conversion
loose jumpers

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

Take away

Two wires shared by every device, pull-ups making the bus active low so nobody can fight for it.

A transaction: START, address plus direction, register pointer, data, an acknowledge on every byte, STOP.

What comes back is sixteen bits whose meaning the datasheet defines. Reading them correctly, and proving it on known values, is Task 3.

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

Exit check

1. Address 0x18: the full first byte for a read, and for a write?

2. Why does a missing device produce NAK without doing anything?

3. Decode upper 0x01, lower 0x91.

4. Which two registers hold constants? Why read first?

© Ran Yang, Ph.D.Advanced Instrumentation

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

Figure descriptions

Slide 5 · Two ways to move a byte

Eight-bit parallel transfer: bits D7 through D0 travel simultaneously on eight separate signal lines between devices.

Eight-bit serial transfer: bits of a byte travel one after another on a single signal line, beginning with the most-significant bit in this illustration.

Slide 9 · Two wires, any number of devices

Shared I²C bus: a controller and multiple devices connect to SDA and SCL. Both lines are pulled up to 3.3 V; devices signal low by pulling a line toward ground.

Slide 11 · START: SDA falls while SCL is high

I²C start condition: SDA falls from high to low while SCL remains high.

Slide 12 · STOP: SCL rises, then SDA rises

I²C stop condition: SCL is high when SDA rises from low to high, releasing the bus.

Slide 14 · Nine clocks per byte: the receiver answers

I²C byte followed by an acknowledgement: the receiver holds SDA low on the ninth clock, indicating that more data may follow without a stop condition.

Slide 15 · NAK: nobody pulled the line down

I²C byte followed by a negative acknowledgement: SDA is high on the ninth clock, signaling completion; a stop condition ends the transaction.

Slide 16 · Address byte: 7 bits plus a direction

I²C read transaction: start condition, seven address bits sent most-significant first, read bit equal to one, acknowledgement, and repeated start. SDA carries data and SCL clocks each bit.

Slide 19 · Four blocks, four wires

MCP9808 temperature-sensor breakout board with labeled Vdd, ground, SCL, SDA, alert, and address-selection pins A0, A1, and A2. Photo: Adafruit.