PyRaspBeta

← All practice problems

Raspberry Pi practice problem · I²C, SPI, UART & 1-Wire

Factory calibration

HardBudget $10Python · gpiozero

A BME280's raw counts mean nothing on their own: every chip stores factory trim values that its datasheet formula needs. Do it without a driver.

Build: a BME280 on I²C at 0x76 (3V3, SDO to GND).

Program:

  1. Read the trim words from 0x88: dig_T1 (unsigned), dig_T2 and dig_T3 (signed) — each 16-bit little-endian, at 0x88, 0x8A, 0x8C.
  2. Write 0x27 to ctrl_meas (0xF4) for normal mode.
  3. Every 0.5 s read adc_T from 0xFA–0xFC ((msb << 12) | (lsb << 4) | (xlsb >> 4)) and apply Bosch's integer formula:
var1 = (((adc_T >> 3) - (dig_T1 << 1)) * dig_T2) >> 11
var2 = (((((adc_T >> 4) - dig_T1) * ((adc_T >> 4) - dig_T1)) >> 12) * dig_T3) >> 14
t = ((var1 + var2) * 5 + 128) >> 8      # hundredths of a degree

Print one decimal:

T = 23.4 C

The test holds 23.4 °C, then −4.2 °C from 3 s.

More in I²C, SPI, UART & 1-Wire