PyRaspBeta

← All practice problems

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

Check the CRC

MediumBudget $8Python · gpiozero

1-Wire is a long, noisy single wire, so the kernel checks every reading with a CRC. The first line of w1_slave ends with YES when the data is good and NO when it isn't:

4e 01 4b 46 7f ff 0c 10 aa : crc=f0 NO

Build: a DS18B20 on GPIO4 with a 4.7 kΩ pull-up (the probe module kit works).

Program: read the probe in a loop. When the first line ends in NO, print CRC error, retrying and read again straight away. When it ends in YES, print the temperature with two decimals, Temp: 19.50 C, then wait 1 s.

The test's probe sits at 19.5 °C on a bad line: about a third of the reads fail their CRC.

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