Potentiometer
Introduction
Section titled “Introduction”A potentiometer is a knob that provides a variable resistance. It can be used to read an analog input value from the user. In your everyday projects, you could use a potentiometer to change the brightness of a dimmer led or adjust the volume of an amplifier.
In this post, you’ll learn how to use a potentiometer with both Arduino. In a real-world project, you will use a specific board and a specific language depending on what is the project’s goal: do not be limited by your fear of exploring new hardware and software technologies.
Software Setup for Python
Section titled “Software Setup for Python”One of the best environments for Python programming (and definitely my favorite) is PyCharm by JetBrains. However, PyCharm is not really suitable for running python scripts from the Raspberry Pi board, since it’s very resource-consuming.
Some light-weight IDEs are:
- Visual Studio Code which is also available for Raspberry PI by following this guide
- Thonny which is installed by default on Raspberry Pi OS
- Mu
Hardware requirements & setup
Section titled “Hardware requirements & setup”- Arduino Uno board or any Arduino-like board
- Potentiometer (i.e. 10k ohm)
- Hook-up wires of different colors
- Breadboard
The potentiometer has tree pin, those on the side are ground and 5V while the once in the middle is the analog input. Connect the potentiometer as shown below:
The Arduino boards are usually programmed in the standard C-like language, whose files have the .ino
extension and must be placed inside a folder with the same name of the sketch.
Into the arduino website, you can find a very well-made language reference.
The code is shown below:
// select the input pin for the potentiometerint sensorPin = A0;
// variable to store the value coming from the sensorint sensorValue = 0;
void setup() { // initialize the serial communication Serial.begin(9600);}
void loop() { // read the value from the sensor sensorValue = analogRead(sensorPin);
// print out the value on the Serial Monitor Serial.println(sensorValue);
// wait for half a second delay(500);}
A similar example can be found at File
> Examples
> 03.Analog
> AnalogInput
.
Hit Ctrl
+ U
to upload the code. Open the Serial Monitor by hitting Ctrl
+ Shift
+ M
.
MicroPython
Section titled “MicroPython”Software Setup
Section titled “Software Setup”Some light-weight IDEs are:
- Visual Studio Code which is also available for Raspberry PI by following this guide
- Thonny which is installed by default on Raspberry Pi OS
- Mu
Python Code
Section titled “Python Code”The processor on the board can only process Arduino code, so you need to “translate” your Python code into C-like code. StandardFirmata
sketch will translate it for you and let you write the code in the language you prefer to.
First, you need to load StandardFirmata
inside the board to allow Python code to be executed.
From the Arduino IDE, let’s load StandardFirmata
sketch:
- Press on
File
Examples
Firmata
StandardFirmata
Hit Ctrl
+ U
to upload the code.
Open your favorite Python IDE and configure a virtual environment: it’s a common practice to have a requirements.txt
file in the root of the project to list all the needed packages. To avoid introducing too many topics, we will talk about it in another article.
Add the pyfirmata
library with the following line on the Terminal:
pip install pyfirmata
Create a new Python file called potentiometer.py
and copy the content below:
from pyfirmata import util, Arduinoimport time
def potentiometer(): board = Arduino('/dev/ttyACM0') it = util.Iterator(board) it.start()
analog_input = board.get_pin('a:0:i')
while True: analog_value = analog_input.read() print(analog_value) time.sleep(0.1)
if __name__ == '__main__': potentiometer()
Please Note: a:0:i
stands for:
- analog
- pin
A0
- taken as input
Be aware of respecting tabs and spaces since Python is sometime a bit tricky!
To run the script, open a Terminal and type:
python _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”- Arduino Official Documentation
- pyFirmata - Python library to communicate with Arduino pins.