PyRaspBeta

← All practice problems

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

Two's complement

MediumBudget $9Python · gpiozero

The MPU-6050 has a temperature sensor too. Its reading is a signed 16-bit number, big-endian, in registers 0x41 (high byte) and 0x42 (low byte), and

°C = raw / 340 + 36.53

At room temperature raw is negative, so it arrives in two's complement: if the 16-bit value is 32768 or more, subtract 65536.

Build: an MPU-6050 on the I²C pins (3V3, AD0 to GND → address 0x68).

Program: wake the chip (write 0 to register 0x6B — it starts asleep and reads all zeros), then every 0.5 s print the temperature with one decimal:

Chip: 22.5 C

The test holds 22.5 °C, then 40.0 °C from 3 s.

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