ENع
←Sensor driver
Lesson 30 · UART, I2C and SPI in firmware

Writing a sensor driver from a datasheet

14 min10 steps5 questionsIntermediate
About this lesson

A driver is the code that turns a chip's bytes into a useful value, and the datasheet is where you learn how. Write one for a Microchip MCP9808 temperature sensor on an Arduino Uno R3: find its I2C address (0x18), its register map and its data format, then write read16() with the Wire library and check the maker's ID. The first version prints 3,097.25 degrees. A logic analyser shows the bytes are right, the datasheet shows why the code is wrong, and one mask fixes it. The lessons “I2C in firmware: addresses, registers and ACKs” and “Debugging your driver with a logic analyser: triggers, NACKs and glitches” cover the bus and the tool; the Hardware lesson “Serial buses: UART, I2C and SPI” explains the wires.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 30 · Interactive
01 / 10

Start from the datasheet

A driver is the code that turns a chip's bytes into a useful value. Ours runs on an Arduino Uno R3 and reads a Microchip MCP9808 temperature sensor over I2C. Everything it needs is in the sensor's datasheet.

driver     code that talks to one chip and hides its bytes
board      Arduino Uno R3 (ATmega328P), I2C on A4 = SDA, A5 = SCL
sensor     Microchip MCP9808, 2.7 to 5.5 V, I2C up to 400 kHz
from the datasheet we need three things:
  1  the bus address
  2  the register map
  3  what each bit of the data means
Loading figure…
or ← →
Read the lesson step by step
  1. Start from the datasheet

    A driver is the code that turns a chip's bytes into a useful value. Ours runs on an Arduino Uno R3 and reads a Microchip MCP9808 temperature sensor over I2C. Everything it needs is in the sensor's datasheet.

    driver     code that talks to one chip and hides its bytes
    board      Arduino Uno R3 (ATmega328P), I2C on A4 = SDA, A5 = SCL
    sensor     Microchip MCP9808, 2.7 to 5.5 V, I2C up to 400 kHz
    from the datasheet we need three things:
      1  the bus address
      2  the register map
      3  what each bit of the data means
  2. Find the address

    The datasheet gives the address as 0011 followed by pins A2, A1, A0. Ours are wired to ground, so it is 0x18. The Uno sends 0x18 on SDA, and the sensor answers ACK: it pulls the line low.

    address    0011 A2 A1 A0  =  0011 000  =  0x18
    on SDA     0x18 << 1 | 0 = 0x30  (write)
    ninth bit  sensor holds SDA low = ACK, "I am here"
               nobody pulls it low = NACK
    line 1     Wire.beginTransmission(0x18);  (Wire takes 7-bit addresses)
  3. Find the registers

    Inside the sensor are 16-bit registers, each picked by a pointer number. Pointer 0x05 holds the temperature, 0x06 the maker's ID and 0x07 the device ID. To read one, you write its pointer, then read two bytes.

    register map (datasheet, Register 5-1)
      0x05  TA      ambient temperature
      0x06  MFR ID  always 0x0054
      0x07  DEV ID  0x0400  (device 0x04, revision 0x00)
    read = write the pointer, then read 2 bytes, high byte first
  4. Point at a register

    Our helper read16(reg) writes the pointer byte, here 0x06 for the maker's ID. endTransmission(false) ends without a stop, so the read follows at once with a repeated start and no other controller can take the bus.

    line 2     Wire.write(reg);               queues reg = 0x06
    line 3     Wire.endTransmission(false);   sends it, no STOP
    returns    0 = success, 2 = NACK on the address
    on SDA     S  0x30 ACK  0x06 ACK  then a repeated start
  5. Check who answered

    requestFrom asks for two bytes, high byte first: 0x00, then 0x54. 0x0054 is Microchip's ID, as the datasheet promises. If begin() reads anything else, the wiring or address is wrong, so the driver stops and says so.

    line 4     Wire.requestFrom(0x18, 2);   on SDA: Sr 0x31 (read)
               sensor sends 0x00, Uno ACKs: one more please
               sensor sends 0x54, Uno NACKs: that is enough, then STOP
    line 5-6   r = 0x00 << 8 | 0x54  (from the 32-byte buffer)
    r          0x0054 == 0x0054  ->  sensor found
  6. Read the temperature

    Now read16(0x05): the bytes 0xC1 and 0x94 arrive, so r = 0xC194. The logic analyser clipped onto SDA and SCL records the same two bytes, so we know exactly what the chip sent.

    read16(0x05)
    on SDA     S 0x30 A 0x05 A  Sr 0x31 A  0xC1 A  0x94 N  P
    r          0xC194
    analyser   decodes the same bytes from the wire
  7. A bug: 3,097 degrees

    Our first line 7 was t = r / 16.0, because each step is 1/16 of a degree. It prints 3,097.25 degrees. The analyser showed the bytes on the wire were right, so the bug is in our code.

    line 7     t = r / 16.0;     (first version)
               0xC194 = 49,556
               49,556 / 16 = 3,097.25
    analyser   C1 94  matches r: the bus is fine
    so the bug is in how we read the bits
  8. Read the bit map again

    The datasheet says the top three bits are alert flags, not temperature. At power-up the alert limits are 0 °C, so in a warm room two flags are set: 0xC1 begins 110. Bit 12 is the sign; only the low 12 bits count.

    0xC194 = 1100 0001 1001 0100
    bits 15-13 110   alert flags: TA >= TCRIT, TA > TUPPER, TA < TLOWER
               limits are 0 C at power-up, so 25.25 C sets the first two
    bit 12     0     sign: 0 = at or above 0 C
    bits 11-0  0x194  temperature
  9. Mask, then divide

    Line 7 now keeps only the low 12 bits: r & 0x0FFF is 0x194, which is 404. Each step is 0.0625 degree, so 404 / 16 = 25.25 °C. One mask turns nonsense into a sensible room temperature.

    line 7     t = (r & 0x0FFF) / 16.0;
               0xC194 & 0x0FFF = 0x194 = 404
               404 / 16 = 25.25 C   (0.0625 C per step)
  10. Test below zero

    Now try the freezer. At -5 °C the register holds 0x3FB0. The low 12 bits give 4,016 / 16 = 251, and the sign bit makes line 8 subtract 256: -5 °C. Test edge cases like this before trusting any driver.

    0x3FB0 = 0011 1111 1011 0000
    bits 15-13 001   flag: TA < TLOWER (0 C)
    bit 12     1     sign: below 0 C
    line 7     0xFB0 = 4,016,  / 16 = 251
    line 8     251 - 256 = -5 C

Quiz Unlocks after the last step.