PyRaspBeta

← All practice problems

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

Three bytes over SPI

EasyBudget $12Python · gpiozero

An SPI transfer is full duplex: every byte you send brings one back. To read channel n of an MCP3008, send three bytes:

[0x01, (0x08 + n) << 4, 0x00]

(a start bit, then "single-ended" and the channel number). The 10-bit result is the low 2 bits of reply byte 1 followed by all of byte 2.

Build: a potentiometer on channel 0 of an MCP3008, wired to the SPI pins with CS on CE0. The knob + ADC board kit is pre-wired.

Program: with spidev (open(0, 0), mode 0), read channel 0 every 0.5 s and print the raw code (0–1023):

Raw: 256

The test sets the knob to 25 %, then 75 % at 2 s.

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