Potentiometer with RPI
Introduction
Section titled “Introduction”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).
Hardware requirements & setup
Section titled “Hardware requirements & setup”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:
pinouts
However, the pin layout is shown in the image below:
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.
Python Code
Section titled “Python Code”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:
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:
python raspberry-pi-_potentiometer.py
Conclusion
Section titled “Conclusion”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!
Documentation
Section titled “Documentation”- Raspberry Pi Official Documentation
- GPIO Zero - Python library to communicate with RPI’s GPIO pins.
- onoff library - Node.js library to communicate with RPI’s GPIO pins.