DEV Community

Cover image for Raspberry Pi - Potentiometer
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi - Potentiometer

A potentiometer is a knob that turns physical rotation into a variable voltage, the same part behind volume dials and brightness sliders. Since the Raspberry Pi has no built-in ADC, reading that voltage means pairing the pot with an external ADC module. This gets you a smooth analog input for the Pi, useful anywhere you want manual control over a value in your code.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi - Potentiometer

What you need

Wiring

Raspberry Pi - Potentiometer wiring diagram

A potentiometer has three pins: GND, VCC, and an output pin. Connect GND to ground (0V) and VCC to 5V or 3.3V. The output pin carries the variable voltage, but since the Pi can't read analog voltage directly, that output goes into the ADS1115 ADC module instead of straight into a GPIO pin. The ADS1115 then connects to the Raspberry Pi over the I2C interface. A screw terminal block shield can make these connections more secure if you're not keen on breadboard wires coming loose.

How it works

Turning the shaft changes the potentiometer's resistance, which changes the voltage on its output pin, somewhere between the GND and VCC pins the shaft sweeps between. The ADS1115 is a 16-bit ADC, so it converts that voltage, in a 0V to 3.3V range, into a digital value from 0 to 65535. The Pi reads this value over I2C. From there you can rescale the number into whatever makes sense for your project, like the original voltage or a volume level.

Code

The Python script on the Pi uses the Adafruit_ADS1x15 library to talk to the ADS1115 over I2C and read the raw ADC value from the potentiometer. You'll also need the RPi.GPIO library installed, via sudo apt-get install python3-rpi.gpio, and the Adafruit library installed separately. The script then reads and prints the value continuously, which you can rescale in your own code depending on what you're controlling.

raspberry-pi-potentiometer.py

"""
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.")
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-potentiometer

Top comments (0)