IO6: Buses and drivers: I2C, SPI, UART and reading a datasheet
Reading a datasheet
🎯You'll be able to
Find the address, registers, bit fields, timing, byte order, signed values and power modes in a datasheet, and turn them into code.
🏠The everyday picture
A recipe lists ingredients, quantities, oven temperature and time. Skip the "rest the dough 30 minutes" line, and the bread fails, even though you followed everything else.
📘What it really is
- Electrical limits: supply voltage, pin voltages, current in each mode. Stay inside them.
- Interface: the bus, the address, the speed.
- Register map: each register's address, whether it's read or write, and what each bit means.
- Timing: how long a measurement or a reset takes before data is valid.
- Data format: byte order (little- or big-endian), signed or unsigned, units and scale (such as 0.5 °C per step).
- Power modes: how to put the chip in standby, and what it draws in each mode.
- Errata: known bugs in the chip, often in a separate document.
📝A worked example
Input: the LV-20 datasheet.
Steps:
- Address 0x29; WHO_AM_I at 0x0F is 0x5A.
- Start with CTRL (0x20) = 0x01; wait 30 ms or until STATUS (0x27) bit 0 is 1.
- Read 3 bytes from 0x28: distance = DIST_L + 256 × DIST_H (little-endian, mm); TEMP is a signed byte in 0.5 °C steps.
- Standby: CTRL = 0x00, because the sensor draws 1.5 mA if left on.
Expected output: a page of notes that becomes firmware/sensor.py.
🌍Where it's used
- Every new part a team adds.
- Debugging odd readings, which often come from a missed datasheet line.
- Choosing parts, by comparing datasheets.
📝Case scenario
A learner read the LV-20 straight after starting a measurement and got the previous reading every time. The datasheet's timing section said 30 ms. Waiting for the ready bit fixed it.
🖱️Try it
Find a datasheet for a real I2C temperature sensor online. Write its address, its ID register (if any), its measurement time and its data format. Success: four facts, each with the page or section where you found it.
⚠️Common mistakes
- Skipping the timing section. Sign: stale or zero readings. Fix: wait as the datasheet says.
- Guessing byte order. Sign: huge numbers. Fix: read the data format.
- Ignoring power modes. Sign: a flat battery. Fix: standby when idle.
🛠️For your project
Project 1's tests 1, 2 and 4 come straight from the LV-20 datasheet: the ID, the timing and data format, and standby.
✅Quick check
Which datasheet section tells you when data is valid?
Show the answer
The timing section, such as a measurement time or a ready bit.
Why read the errata?
Show the answer
They list known bugs in the chip that the main datasheet may not mention.
