The bench
Every lesson opens the same circuit on an Uno. A BMP280 sits on A4 and A5 with its supply through a slide switch, so flipping the switch unplugs it while the sketch runs. An INA219 sits on the header's SDA and SCL with 5 V going into VIN+, through its 0.1 ohm shunt, and out of VIN- into a 100 ohm load. On an Uno those are the same two I2C wires, so both chips share one bus: the INA219 at 0x40 and the BMP280 at 0x76.
Take the course at Write a driver from a datasheet on /learn.
What each sketch does
None of them includes a sensor library. Every byte is a Wire transaction.
drvscanscans 0x08 to 0x77 every two seconds, reads the BMP280's chip id (0x58 from register 0xD0) and two power-on values.drvinareads the INA219's 16-bit big-endian registers: the 0x399F configuration, the shunt (499 counts of 10 uV) and the bus (4996 mV), then writes calibration 4096 and reads it back, after which the current register reads 49.9 mA.drvinitruns the BMP280 init sequence: id check, soft reset, the NVM copy onim_update, config, a forced measurement timed onmeasuring, and a read-back ofctrl_meas.drvburstreads the 24 calibration bytes and the 6 data bytes in one burst each, assembles little-endian words and 20-bit counts, and times a burst against six single reads.drvcalibapplies Bosch's integer compensation formulas: 22.00 C and 101326 Pa from the default sliders.drverrreturns a status from every transfer, catches the wrong chip by its id, times out every wait, and re-initializes a sensor that was unplugged and came back. It can also try a second address.drvbossis aBmp280class and anIna219class flying a weather balloon: normal mode at 16x pressure, relative altitude, a load current, and a ceiling alarm.
What the simulated chips do
The BMP280 is register exact: the memory map, the reset values, im_update
for 2 ms after a reset, the datasheet's measurement times for each
oversampling, forced and normal modes, and 16 to 20 bits of result. Its
calibration is Bosch's own worked example (dig_T1 27504, dig_T2 26435, dig_T3
-1000, and so on), and its data registers hold the raw counts that the
datasheet's formulas turn back into the temperature and pressure on its
sliders. Get a shift, a sign or a byte order wrong and it reads nonsense, as a
real one does.
The INA219 is a real 0.1 ohm resistor in the circuit, so it reads the circuit's own current. Its current and power registers read zero until calibration is written, as the datasheet says.
Honest limits
- No noise. A real BMP280 wanders by a few pascals, which is what oversampling and the IIR filter are for; here they change only the measurement time and the number of bits.
- The sketches use the datasheet's 32-bit pressure formula, which reads a pascal or two off the 64-bit one (101326 Pa for a slider at 101325). The AVR has no fast 64-bit multiply, so this is what a real Uno driver does too.
- The Uno's I2C here is bit-banged and runs near 30 kHz, so one register read takes about 1.4 ms and every polled wait is measured in those steps.
- The balloon's altitude uses the straight-line slope of the barometric formula, 8.33 cm a pascal, good to a few percent over a few hundred meters.
Related
Talk to chips for the I2C, SPI and UART basics, and Datasheets and schematics for reading the documents these drivers come from.