Lesson plan

Write a driver from a datasheet lesson plan

Raw Wire transactions against real register maps, no library: an I2C scan and a chip id, an INA219 driver with big-endian registers and its calibration, the BMP280 init sequence, burst reads, Bosch’s compensation formulas, error handling for a chip that is missing or unplugged, and a complete BMP280 driver flying a weather balloon.

4 chapters, 7 lessons, about 7 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Addresses, ACKs, register maps, and 16-bit reads and writes.
  • The startup sequence, and bursts of calibration and data.
  • The compensation math, and a driver that survives a missing chip.
  • A complete BMP280 driver class flying a weather balloon.

Materials for the whole unit

  • Arduino Uno R3
  • Current monitor, INA219
  • Half breadboard
  • Pressure and temperature, BMP280
  • Resistor
  • Slide switch

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 2 h

Talking to a chip

Addresses, ACKs, register maps, and 16-bit reads and writes.

LessonTimeWhat students do
Who is on the busAn I2C scan, an ACK and a NACK, and a first read from a register map: the BMP280 answers 0x58 from register 0xD0, its power-on values say it has measured nothing yet, and a missing chip reads 0xFF.60 min
  1. Run it: scan the bus
  2. A register map
  3. Power-on values
  4. Unplug it
  5. The wrong address
  6. Read the datasheet like an engineer
Reads and writes: the INA219A first complete driver: the INA219 current monitor, with 16-bit big-endian registers, signed shunt counts, a bus register with status bits in it, and a calibration register that has to be written before the current reads anything.60 min
  1. Run it: a 16-bit register
  2. Shunt and bus voltages
  3. The register that makes it look broken
  4. Write a register, read it back
  5. Change the load
  6. Pick your own resolution

Materials

  • Arduino Uno R3
  • Current monitor, INA219
  • Half breadboard
  • Pressure and temperature, BMP280
  • Resistor
  • Slide switch

Check for understanding

Each student finishes "Reads and writes: the INA219" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Pick your own resolution".

Chapter 2. About 2 h

Getting data out

The startup sequence, and bursts of calibration and data.

LessonTimeWhat students do
Init sequencesThe BMP280 startup as the datasheet orders it: check the id, soft reset, wait for the NVM copy, write config while asleep, start a forced measurement, and poll the status register, timing every wait.60 min
  1. Run it: six steps
  2. Soft reset, and the NVM copy
  3. Forced mode and the measuring bit
  4. Read back what you wrote
  5. Oversample the pressure
  6. Your init checklist
Burst readsRead 24 calibration bytes and 6 data bytes in one transfer each: little-endian words, signed and unsigned coefficients, 20-bit counts assembled from three bytes, and the time a burst saves.60 min
  1. Run it: 24 bytes, one transfer
  2. Little-endian words
  3. Signed or unsigned
  4. Twenty bits from three bytes
  5. What a burst saves
  6. More samples, more bits

Materials

  • Arduino Uno R3
  • Current monitor, INA219
  • Half breadboard
  • Pressure and temperature, BMP280
  • Resistor
  • Slide switch

Check for understanding

Each student finishes "Burst reads" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "More samples, more bits".

Chapter 3. About 2 h 10 min

Units and failures

The compensation math, and a driver that survives a missing chip.

LessonTimeWhat students do
Calibration mathBosch’s integer compensation formulas, copied from the datasheet: t_fine first, then the 32-bit pressure formula, checked against the part’s sliders, and broken on purpose to see what a byte-order bug looks like.60 min
  1. Run it: degrees and pascals
  2. t_fine, and why order matters
  3. Pressure, and an honest pascal
  4. Heat it up
  5. Climb a mountain
  6. Break it on purpose
Error handlingA driver that never trusts the bus: a status from every transfer, the wrong chip caught by its id, timeouts on every wait, a sensor unplugged mid-run and re-initialized when it comes back, and a second address to try.70 min
  1. Run it: every call returns a status
  2. The wrong chip
  3. Unplug it while it runs
  4. Plug it back in
  5. Strapped the other way
  6. Try both addresses
  7. The failure list

Materials

  • Arduino Uno R3
  • Current monitor, INA219
  • Half breadboard
  • Pressure and temperature, BMP280
  • Resistor
  • Slide switch

Check for understanding

Each student finishes "Error handling" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The failure list".

Chapter 4. About 1 h

Boss project

A complete BMP280 driver class flying a weather balloon.

LessonTimeWhat students do
Boss: a BMP280 driverEverything in one class: id check, reset, a calibration burst, normal mode at 16x, the compensation formulas and a status from every call, with an INA219 driver beside it, flying a weather balloon that survives a loose connector.60 min
  1. Run it: a balloon flight computer
  2. Normal mode
  3. Launch
  4. A loose connector at altitude
  5. Two drivers, one bus
  6. Set the ceiling

Materials

  • Arduino Uno R3
  • Current monitor, INA219
  • Half breadboard
  • Pressure and temperature, BMP280
  • Resistor
  • Slide switch

Check for understanding

Each student finishes "Boss: a BMP280 driver" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Set the ceiling".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.