Skip to content

Potentiometer

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.

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:

  1. Visual Studio Code which is also available for Raspberry PI by following this guide
  2. Thonny which is installed by default on Raspberry Pi OS
  3. Mu
  1. Arduino Uno board or any Arduino-like board
  2. Potentiometer (i.e. 10k ohm)
  3. Hook-up wires of different colors
  4. 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:

Arduino writing schema

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 potentiometer
int sensorPin = A0;
// variable to store the value coming from the sensor
int 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.

Some light-weight IDEs are:

  1. Visual Studio Code which is also available for Raspberry PI by following this guide
  2. Thonny which is installed by default on Raspberry Pi OS
  3. Mu

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:

  1. Press on File
  2. Examples
  3. Firmata
  4. 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:

Terminal window
pip install pyfirmata

Create a new Python file called potentiometer.py and copy the content below:

from pyfirmata import util, Arduino
import 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:

Terminal window
python _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!