C1230 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 30 · I²C and a temperature sensor

Two wires, many devices, and a conversation for each read

A board with a temperature sensor, an accelerometer, a clock chip and a memory would run out of pins if each had its own wires. I²C connects them all to the same two: a data line (SDA) and a clock line (SCL). Every device has an address, and every exchange is a short conversation the controller starts: a start signal, an address, a direction, then bytes, each acknowledged by whoever received it. Reading one register of a sensor takes about half a millisecond at the standard 100 kHz, and the waveforms are simple enough to read by eye on a logic analyzer.

  • The program reads the temperature sensor at address 0x48 every 100 ms and prints its reading in hundredths of a degree. Run it, then look at the SDA and SCL lanes (zoomed to the first read).
  • Find the pieces: SDA falling while SCL is high (start), 9 clocks for the address byte with its write bit and the sensor's acknowledgment, 9 for the register number, a repeated start, 9 for the address with the read bit, then two data bytes, and SDA rising while SCL is high (stop).
  • Change the sensor's temperature slider and run again: the printed value follows it.

Predict

A device's 7-bit address is 0x48. What byte does the controller send to start reading from it?

Name it

The bus

SDA and SCL are open-drain: devices only pull them low, and pull-up resistors bring them high. The controller drives SCL; data changes only while SCL is low, except for two special events:

  • start: SDA falls while SCL is high;
  • stop: SDA rises while SCL is high.

Each byte is 8 bits, most significant first, followed by a ninth clock in which the receiver pulls SDA low to acknowledge (ACK) or leaves it high (NACK). A controller reading NACKs the last byte to say it's done.

Addresses and transactions

Every device has a 7-bit address; the first byte after a start is the address shifted left with the direction in bit 0 (0 write, 1 read). If no device answers, the address byte is NACKed. Reading a register is a write then a read:

start, address+W, register, repeated start, address+R, data..., stop

Standard mode is 100 kHz; fast mode 400 kHz. Each byte takes 9 clocks.

Pull-ups and speed

The bus's capacitance C (devices plus wiring) charges through the pull-up R. From 30% to 70% of the supply takes 0.8473RC, and the standard allows at most 1000 ns at 100 kHz and 300 ns at 400 kHz, so

R≤tr0.8473C

This sensor

A TMP102-style sensor returns two bytes: a 12-bit two's-complement temperature, left-justified, in units of 1/16 °C (0.0625 °C).

Worked example

The sensor returns 0x19 then 0x80. What's the temperature?

Show the solution
  1. Join the bytes. Together they make 0x1980.
  2. Shift right by 4. 0x1980≫4=0x198=408
  3. Check the sign. Bit 11 is 0, so it's positive.
  4. Scale to degrees. 40816=25.5°C

Answer 25.5 °C.

Worked example

A 400 kHz bus has 100 pF of capacitance. What's the largest pull-up that meets the 300 ns rise time?

Show the solution
  1. Solve the rise time for R. R=300ns0.8473×100pF=3.54kΩ
  2. Pick a standard value. A standard 3.3 kΩ works.
  3. A bigger pull-up. A 10 kΩ pull-up would only manage about 100 kHz on that bus.

Answer 3.54 kΩ, so a standard 3.3 kΩ.

Worked example

Reading a 2-byte register from an I²C sensor at 100 kHz takes an address byte, a register byte, a repeated start with the address again, and two data bytes. About how long?

Show the solution
  1. Count the byte bits. Five bytes of 9 bits each (8 data plus acknowledge): 5×9=45bit-times
  2. Add the bus conditions. About 3 more for the start, repeated start and stop make 48 bit-times.
  3. Time at 100 kHz. Each bit-time is 10 µs: 48×10μs=480μs
  4. At 400 kHz. It's 120 µs.
  5. What it means. Reading many sensors over I²C at 100 kHz adds up quickly.

Answer about 480 µs at 100 kHz, and 120 µs at 400 kHz.

Check

A device's address is 48. What's the first byte after start to 0? (0 write, 1 read)

Check

A positive reading arrives as 19, 00. What's the temperature?

Check

In 12-bit two's complement at 0.0625 °C per count, what raw value does −10 °C produce?

Check

At 100 kHz, a transaction sends 2 bytes (each 9 clocks) plus about one clock for the start and stop. How long does it take?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

The bus has 50 pF and must rise in 300 ns. What's the largest pull-up?

Write units the way you normally would: 1.5 kΩ, 1500 Ω and 0.0015 MΩ are the same answer.

Check

A read ends with the controller sending NACK after the last byte. Why?

Check

A temperature sensor sits somewhere on the I²C bus. Scan every address from 0x08 to 0x77 and print the one that acknowledges.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

The sensor at 0x48 is below freezing. Read it and print its temperature in °C, handling negative values.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

An I²C bus has 50 pF and must rise in 300 ns.

(a)

What is the largest pull-up?

Write units the way you normally would: 1.5 kΩ, 1500 Ω and 0.0015 MΩ are the same answer.

(b)

What current does a device sink pulling it low?

Check

A temperature sensor returns bytes 50 then 128; the reading is the 16-bit value shifted right by 4, in sixteenths of a degree, and positive here.

(a)

What is the 12-bit reading?

(b)

What temperature?

Explain why

How can many devices share one SDA wire without fighting over it?

Practice

Keep practicing

12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.