Raspberry Pi practice problem · I²C, SPI, UART & 1-Wire
Decode the scratchpad
The first line of w1_slave is the probe's raw 9-byte scratchpad:
5e ff 4b 46 7f ff 02 10 c3 : crc=c3 YES
Bytes 0 and 1 are the temperature, little-endian, signed 16-bit, in 1/16 °C (0xFF5E = −162 → −10.125 °C). Byte 8 is a Dallas CRC-8 of bytes 0–7: start with crc = 0; for each byte, for each of its 8 bits from the lowest: mix = (crc ^ byte) & 1, crc >>= 1, if mix then crc ^= 0x8C, byte >>= 1.
Build: a DS18B20 on GPIO4 with a 4.7 kΩ pull-up.
Program: for every read, take the 9 hex bytes from the first line and compute the CRC yourself (don't trust the YES/NO). If it doesn't match byte 8, print CRC mismatch. Otherwise decode bytes 0–1 and print the temperature with three decimals:
-10.125 C
The test's probe is in a freezer at −10.125 °C, then −0.5 °C from 4 s, on a noisy line where about a third of the reads are corrupted.