Skip to content

Potentiometer with RPI

Raspberry Pi boards are ARM-based micro-computers. You can install almost any operating systems on them but the most popular is of course Raspberry Pi OS (which was once called Raspbian).

In order to connect the potentiometer to the board, you first need to know the GPIO pin layout. Open a Terminal in your Raspberry board and run:

Terminal window
pinouts

However, the pin layout is shown in the image below:

GPIO pin layout

The wiring diagram is quote simple since you only need to use a MCP3008 analog converter and other flew wires:

  • pin 2 (or pin 4) to carry out the 5V power
  • pin 6 for Ground (you can also use pins 9, 14, 20, 25, 30, 34, 39)

You must pay a lot of attention with the pin numbering: GPIO pins numbering is different from the physical numbering of the board pins.

Raspberry Pi wiring schema

Python is the default language with which you program on the Raspberry Pi board. You’ll use the GPIO Zero library, in particular the section 2.28 Potentiometer, to write the on below:

raspberry-pi-potentiometer.py
from gpiozero import MCP3008
pot = MCP3008(channel=0)
while True:
print(pot.value)

Save the code in a file named raspberry-pi-potentiometer.py. To run the code open a Terminal and type:

Terminal window
python raspberry-pi-_potentiometer.py

The act itself of getting the value from a potentiometer is not such an amazing project, but even the most complex IoT project of the world is made up by simple tasks like this. The aim of this tutorial is to start a journey of learning: you’re at the very beginning but don’t worry: everything starts from here!