Raspberry Pi - Potentiometer Servo Motor

This tutorial instructs you how to use Raspberry Pi to control the angle of a servo motor based on the input value from a potentiometer. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Servo Motor
1×ADS1115 ADC Module
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. We appreciate your support.

Overview of Servo Motor and Potentiometer

If you are unfamiliar with servo motors and potentiometers (pinouts, how they work, how to program them, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Servo Motor Potentiometer

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

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_servo.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-servo-motor import time import RPi.GPIO as GPIO import Adafruit_ADS1x15 # Constants SERVO_PIN = 26 # Raspberry Pi GPIO pin connected to the servo motor ADC_CHANNEL = 0 # Analog channel on ADS1015 GAIN = 1 # Gain (1, 2/3, 1, 2, 4, 8, 16) # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(SERVO_PIN, GPIO.OUT) # Create PWM instance for servo servo_pwm = GPIO.PWM(SERVO_PIN, 50) # 50 Hz frequency servo_pwm.start(0) # Initialize servo position # Create ADS1x15 instance ads = Adafruit_ADS1x15.ADS1015() def map_value(value, in_min, in_max, out_min, out_max): # Map the input value from one range to another return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min try: while True: # Read the raw ADC value from the potentiometer pot_value = ads.read_adc(ADC_CHANNEL, gain=GAIN) # Map the ADC value to the servo angle (0 to 180 degrees) angle = int(map_value(pot_value, 0, 32767, 0, 180)) # Control the servo motor according to the angle duty_cycle = (angle / 18) + 2.5 # Convert angle to duty cycle servo_pwm.ChangeDutyCycle(duty_cycle) print(f"Potentiometer Value: {pot_value}, Servo Angle: {angle}") time.sleep(0.1) except KeyboardInterrupt: servo_pwm.stop() GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 potentiometer_servo.py

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

  • Turn the potentiometer
  • Check out the servo motor's rotation
  • View the outcome in the Serial Monitor
PuTTY - Raspberry Pi
Potentiometer Value: 100, Servo Angle: 3 Potentiometer Value: 200, Servo Angle: 6 Potentiometer Value: 300, Servo Angle: 9 Potentiometer Value: 400, Servo Angle: 13 Potentiometer Value: 500, Servo Angle: 16 Potentiometer Value: 600, Servo Angle: 19 Potentiometer Value: 700, Servo Angle: 23 Potentiometer Value: 800, Servo Angle: 26 Potentiometer Value: 900, Servo Angle: 29 Potentiometer Value: 1000, Servo Angle: 33

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial


  • 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!