Raspberry Pi - Potentiometer

This tutorial instructs you how to use Raspberry Pi with a potentiometer, which is also known as pot, trimmer, variable resistor, rheostat, or rotary angle sensor. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Potentiometer
1×Potentiometer Kit
1×ADS1115 ADC Module
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Potentiometer

A rotary potentiometer, also known as a rotary angle sensor, is utilized to manually modify the value of an item. Examples include the volume of a stereo, the brightness of a lamp, and the zoom level of an oscilloscope.

Potentiometer pinout

The Potentiometer Pinout

A potentiometer typically has three pins:

  • The GND pin should be connected to ground (0V)
  • The VCC pin should be linked to VCC (5V or 3.3v)
  • The output pin sends the voltage to the Raspberry Pi's input pin.
Potentiometer pinout

※ NOTE THAT:

The GND pin and the VCC pin can be swapped.

How It Works

The rotatable shaft of the potentiometer can be moved from 0°, which is closest to GND, to a maximum angle, which is closest to the VCC pin. This maximum angle is referred to as ANGLE_MAX.

※ NOTE THAT:

The value of ANGLE_MAX is determined by the manufacturer. Generally, we do not pay attention to it, except when we need to compute the angle of rotation (see the use cases section).

How Potentiometer Works

The working principle:

  • An user rotates shaft of the potentiometer
  • ⇒ The angle of the potentiometer is changed
  • ⇒ The resistance of the potentiometer is changed
  • ⇒ The voltage in the output pin of the potentiometer is changed
  • ⇒ The analog value read by Raspberry Pi is changed

Raspberry Pi - Potentiometer

Raspberry Pi board does not have ADC built-in. We need to use an external ADC module (e.g ADS1115 module) to read the analog voltage from potentiometer.

  • The potentiometer is connected to the ADS1115 module.
  • The ADS1115 module converts the voltage from the potentiometer to the ADC value
  • The Raspberry Pi connects to the ADS1115 module and read the ADC value via I2C interface.

The ADS1115 module is a high-resolution 16-bit ADC converter module. It converts a voltage range from 0V to 3.3V into an ADC value range from 0 to 65535.

Once we have obtained the ADC value, we can rescale this value to a different one. Let us consider to some example applications

  • Rescale to back to the voltage of the potentiometer.
  • Rescale to the volume of a stereo
  • Rescale to the brightness of LED
  • Rescale to the speed of a DC motor
  • Rescale to the angle of the servo motor.

Rescale Range

FROM TO
Anglerotated by userANGLE_MAX
Voltagefrom potentiometer's pin 0V3.3
ADC valueread by Raspberry Pi 065536
Other valueconverted by Raspberry Pi VALUE_MINVALUE_MAX

Wiring Diagram

  • Raspberry Pi Potentiometer wiring diagram without breadboard
The wiring diagram between Raspberry Pi and Potentiometer

This image is created using Fritzing. Click to enlarge image

  • Raspberry Pi Potentiometer wiring diagram with breadboard
The wiring diagram between Raspberry Pi and ADS1115

This image is created using Fritzing. Click to enlarge image

How To Program Raspberry Pi Code to read value from Potentiometer

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file potentiometer.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-potentiometer """ import time import Adafruit_ADS1x15 # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Main loop to read and display the analog value try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Convert the raw value to voltage voltage = raw_value / 32767.0 * 4.096 # Assumes 4.096 V range for GAIN=1 # Print the results print("Raw Value: {} \t Voltage: {:.2f} V".format(raw_value, voltage)) # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the Terminal:
python3 potentiometer.py
  • Turn the potentiometer.
  • Check out the outcome in the Terminal.
PuTTY - Raspberry Pi
Raw Value: 0 Voltage: 0.00 V Raw Value: 620 Voltage: 0.38 V Raw Value: 1024 Voltage: 0.63 V Raw Value: 1850 Voltage: 1.13 V Raw Value: 2000 Voltage: 1.22 V Raw Value: 3000 Voltage: 1.83 V Raw Value: 3270 Voltage: 2.00 V Raw Value: 4100 Voltage: 2.51 V Raw Value: 4095 Voltage: 2.51 V

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!