How would you write a driver for a new I2C temperature sensor?
What the interviewer wants to hear
- Read the datasheet: address, registers, timing, conversion time
- Layered design: sensor driver over an I2C bus abstraction
- Clear API: init, read, configure, with error codes
- Handle timeouts, NACKs and retries
- Test with a logic analyser and unit tests with a mocked bus
Model answer
I start with the datasheet: the I2C address, register map, power-on timing, conversion time, data format and any CRC. Then I design layers. The sensor driver does not call the MCU's HAL directly; it uses a small I2C interface, like function pointers for write and read, passed in at init. This makes it portable across MCUs and easy to unit test with a fake bus. The API is simple: sensor_init, sensor_read_celsius, sensor_set_mode, each returning an error code instead of failing silently. Inside, I check the chip ID at init, respect conversion delays, convert raw values with the datasheet formula using fixed point if there is no FPU, and handle NACKs and timeouts with a limited number of retries, plus bus recovery if SDA gets stuck low. I verify the real traffic with a logic analyser, compare readings with a reference thermometer, and write unit tests for the conversion and error paths.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Embedded Systems Engineer questions
- Explain the difference between a mutex and a semaphore in an RTOS, with examples.
- A device freezes randomly once every few days in the field. How do you debug it?
- Explain the memory layout of a typical microcontroller program: .text, .data, .bss, stack and heap.
- When would you use fixed-point arithmetic instead of floating point on a microcontroller?
- What is DMA, when would you use it, and what pitfalls do you watch for?
- Design firmware that receives text commands over UART, like "SET LED 1", and responds reliably.
- Explain how the CAN bus works and why it is popular in automotive and industrial systems.
- Your firmware is close to the flash and RAM limits of the MCU. How do you reduce memory usage?
- How would you design a watchdog strategy for firmware running several RTOS tasks?