Writing a sensor driver from a datasheet
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.
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
Read the lesson step by step
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 meansFind 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)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 firstPoint 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 startCheck 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 foundRead 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 wireA 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 bitsRead 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 temperatureMask, 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)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
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01The MCP9808 datasheet gives its address as 0011 A2 A1 A0. A2 and A1 are tied to ground and A0 to the supply. What address do you pass to Wire.beginTransmission()?
0011 001 in binary is 0x19. The Wire library takes the 7-bit address; it adds the read/write bit itself, which is why 0x30 and 0x31 appear on the wire for address 0x18.
02Why does read16() call Wire.endTransmission(false) after writing the register pointer?
false means no STOP: the controller keeps the bus and the read that follows starts with a repeated start, as the datasheet's read sequence shows.
03begin() reads register 0x06 and gets 0x0054. What has it learned?
The datasheet says the MCP9808's manufacturer ID register always holds 0x0054. Reading the expected ID is a cheap check that the right chip is there before you trust any other number.
04The temperature register reads 0xC194. Which code gives the right temperature for a value at or above 0 °C?
Bits 15 to 13 are alert flags and bit 12 is the sign, so only the low 12 bits are the value: 0x194 = 404, and 404 / 16 = 25.25 °C. Dividing all of r by 16 gives 3,097.25.
05A logic analyser shows the sensor sent exactly the bytes your code received, yet the printed value is wrong. Where is the bug most likely?
If the bytes on the wire match what the code received, the bus is doing its job. The mistake is in the conversion, which is where the datasheet's bit map comes in.
Glossary
- Driver
- Code that talks to one chip and turns its raw bytes into useful values, so the rest of the program never sees the bytes.
- Datasheet
- The maker's manual for a chip: pins, electrical limits, bus address, registers and data formats.
- Register
- A small numbered storage slot inside a chip. Reading or writing it is how firmware talks to the chip.
- Register pointer
- The number you write first to choose which register the next read or write uses.
- Repeated start
- An I2C START sent without a STOP before it, so the controller keeps the bus between writing the pointer and reading.
- ACK / NACK
- The ninth bit after each I2C byte: the receiver pulls SDA low to acknowledge (ACK); leaving it high is a NACK.
- Mask
- An AND with a bit pattern that keeps only the bits you want, for example r & 0x0FFF keeps the low 12 bits.
- Logic analyser
- A tool that records digital signals such as SDA and SCL and decodes them into bytes, so you can see what was really sent.
Notes and sources
- The card shows the heart of the driver. In the full sketch, lines 1 to 6 live in read16(reg), which returns r; mcpBegin() checks read16(0x06) == 0x0054; mcpReadC() runs lines 7 and 8 on read16(0x05).
- On the Uno an int is 16 bits, so shifting 0xC1 left by 8 inside a plain int would run into the sign bit. The (uint16_t) cast keeps the shift unsigned.
- The MCP9808 datasheet (Register 5-4) puts three alert flags in bits 15 to 13, comparing the temperature with the TCRIT, TUPPER and TLOWER limit registers. Those limits are 0 °C at power-up, so a fresh sensor in a warm room reports flags 110 and in a freezer 001.
- The datasheet's Equation 5-1 writes the negative case as 256 − (UpperByte × 16 + LowerByte / 16). For 0x3FB0 that gives +5, not −5: the 13-bit two's complement value is (count − 256), as line 8 computes. Datasheets have mistakes too, which is one more reason to test.
- At the power-up resolution of 0.0625 °C the sensor makes a new reading about every 250 ms (typical), so reading TA faster than that returns the same value. The datasheet promises ±0.25 °C typical and ±0.5 °C at most between −20 and 100 °C.
- SDA and SCL are open-drain: each needs a pull-up resistor to the supply. Most MCP9808 breakout boards include them.
Quiz Unlocks after the last step.