Built-in project · Raspberry Pi 5

Potentiometer through an MCP3008 on a Pi, on a Raspberry Pi 5

gpiozero MCP3008 on a Raspberry Pi 5: a knob read through the ADC on SPI0 sets the brightness of an LED on GPIO18, and the reading is printed twice a second. The whole build, a Raspberry Pi 5 and 4 more parts, runs here in your browser on the Python below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $66. On Mokxi they're free.

Potentiometer through an MCP3008 on a Pi · Raspberry Pi 5live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The Python exactly as the editor opens it. Change a line there and press Run: the board runs main.py in a Python engine in the page, with gpiozero and RPi.GPIO, and nothing to compile.

main.py · Raspberry Pi 5
# A potentiometer through an MCP3008 on a Raspberry Pi 5, with gpiozero.
# The Pi has no analog inputs, so the MCP3008 converts: CLK to GPIO11 (pin 23),
# DOUT to GPIO9 (pin 21), DIN to GPIO10 (pin 19), CS to GPIO8 (pin 24, CE0).
from gpiozero import MCP3008, PWMLED
from time import sleep

pot = MCP3008(channel=0)    # spidev0.0: SPI0 with chip select CE0
led = PWMLED(18)

led.source = pot            # the knob sets the LED's brightness

while True:
    print("value %.3f  raw %4d  %.2f V" % (pot.value, pot.raw_value, pot.voltage))
    sleep(0.5)

Parts list

6 parts, plus the jumper wires. They retail for about $66; on Mokxi they're free.

How it is wired

11 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Raspberry Pi 5 pin 3V3; Eight-channel ADC, MCP3008 pin VREF; Eight-channel ADC, MCP3008 pin VDD
  • Ground: Raspberry Pi 5 pin GND; Eight-channel ADC, MCP3008 pin DGND; Eight-channel ADC, MCP3008 pin AGND; LED, green pin C
  • Ground: Raspberry Pi 5 pin GND; Potentiometer pin 1
  • Raspberry Pi 5 pin GPIO18; Resistor, 330 Ω pin 1
  • Raspberry Pi 5 pin 3V3; Potentiometer pin 3
  • Raspberry Pi 5 pin GPIO10; Eight-channel ADC, MCP3008 pin DIN
  • Raspberry Pi 5 pin GPIO9; Eight-channel ADC, MCP3008 pin DOUT
  • Raspberry Pi 5 pin GPIO11; Eight-channel ADC, MCP3008 pin CLK
  • Raspberry Pi 5 pin GPIO8; Eight-channel ADC, MCP3008 pin CS
  • Eight-channel ADC, MCP3008 pin CH0; Potentiometer pin 2
  • Resistor, 330 Ω pin 2; LED, green pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.