小编
Published2025-10-15
Discover the power of combining Arduino, a potentiometer, and a servo motor in your DIY projects. Learn how to control servo movements precisely using a potentiometer and create interactive and dynamic systems with this easy-to-follow guide.

Introduction to Arduino, Potentiometers, and Servo Motors
Arduino is a powerful, open-source electronics platform that allows makers, hobbyists, and professionals to create interactive projects quickly and easily. One of the key features of Arduino is its ability to control various components such as lights, motors, and sensors. Among the most popular projects is the combination of a potentiometer and a servo motor, which offers an intuitive way to control mechanical movement with a simple dial or slider.
In this article, we will explore how to connect a potentiometer to an Arduino and use it to control the position of a servo motor. This project not only provides hands-on experience with essential electronics concepts but also introduces you to the world of servo control, PWM (Pulse Width Modulation), and analog-to-digital conversion. Whether you’re building a robot, a model, or just experimenting, this setup will lay the foundation for endless creative applications.
Understanding the Components: Potentiometer, Servo Motor, and Arduino
To begin, let’s take a closer look at the components we will use in this project:
1. Potentiometer: The Analog Input
A potentiometer is a variable resistor that can be adjusted manually to change the amount of resistance in a circuit. It typically consists of three pins: two for the ends of the resistor and one for the wiper, which moves across the resistor to adjust its resistance.
The potentiometer works by sending an analog voltage between 0V and 5V to the Arduino, depending on the position of the wiper. This signal can be read using an analog input pin on the Arduino, which allows you to control various devices, including motors, lights, and displays. In this case, the potentiometer’s value will dictate the angle at which the servo motor turns.
2. Servo Motor: The Mechanical Actuator
A servo motor is a type of motor that can be precisely controlled to rotate to a specific angle, typically between 0° and 180°. Servos are controlled using a PWM signal, which tells the motor how far to rotate. In our project, the potentiometer will determine the PWM signal sent to the servo, controlling its position.
Servos are often used in robotics, RC vehicles, and automation systems because they can hold a position accurately. They are usually equipped with gears that allow for smooth rotation and hold their position when not in motion.
3. Arduino: The Brain of the Operation
Arduino is a microcontroller platform that is easy to use and supports a vast range of sensors, actuators, and other electronic components. It’s programmed using the Arduino IDE (Integrated Development Environment), which simplifies writing code for the board. For this project, we’ll use the analog-to-digital conversion capability of the Arduino to read the potentiometer’s input and the PWM output functionality to control the servo.
The Wiring Diagram: Connecting the Potentiometer and Servo Motor
Before diving into the code, it's crucial to understand how to wire the components together. The wiring is simple and requires just a few connections:
Connect the first pin (one end of the resistor) to 5V on the Arduino.
Connect the second pin (the wiper) to an analog input pin, such as A0 on the Arduino.
Connect the third pin (the other end of the resistor) to GND (ground) on the Arduino.
Connect the red wire (VCC) of the servo to 5V on the Arduino.
Connect the black wire (GND) of the servo to GND on the Arduino.
Connect the yellow or white wire (signal) of the servo to a digital pin on the Arduino, such as pin 9.
With these connections in place, you’re ready to start programming the Arduino to control the servo’s position based on the potentiometer’s input.
Writing the Arduino Code and Testing the Setup
Now that we’ve covered the hardware setup, let’s move on to the software part of the project. The Arduino code will be responsible for reading the analog input from the potentiometer and converting it into a PWM signal to control the servo.
1. Understanding the Code: Key Concepts
To achieve this, we need to:
Read the Potentiometer’s Input: Use the analogRead() function to get the value from the potentiometer. This function returns a value between 0 and 1023, corresponding to the 10-bit resolution of the Arduino’s analog-to-digital converter.
Map the Input Value: The potentiometer provides values between 0 and 1023, but the servo expects a value between 0 and 180 (for its rotation angle). To convert the potentiometer value into a usable range for the servo, we’ll use the map() function.
Control the Servo: Use the Servo library, which simplifies controlling the servo motor with PWM. The write() function in the Servo library allows us to send the corresponding angle to the servo.
2. Sample Code for Arduino
Here’s the full code for this project:
#include // Include the Servo library
// Define the potentiometer and servo pin
const int potentiometerPin = A0; // Potentiometer connected to A0
const int servoPin = 9; // Servo connected to pin 9
Servo myServo; // Create a servo object
myServo.attach(servoPin); // Attach the servo to pin 9
Serial.begin(9600); // Start the serial monitor
int potValue = analogRead(potentiometerPin); // Read the potentiometer value (0-1023)
// Map the potentiometer value to a range of 0-180 degrees
int angle = map(potValue, 0, 1023, 0, 180);
// Write the mapped angle to the servo
// Print the potentiometer value and angle for debugging
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" | Servo Angle: ");
delay(15); // Delay to allow the servo to reach the position
3. Explanation of the Code
Servo Library: We begin by including the Servo.h library, which makes it easy to control the servo motor.
Analog Input: The analogRead(potentiometerPin) function reads the analog signal from the potentiometer and stores it in potValue.
Mapping the Input: The map() function maps the potentiometer value (0-1023) to a range suitable for the servo (0-180).
Controlling the Servo: The myServo.write(angle) function sends the mapped angle to the servo motor, which rotates to the desired position.
Serial Output: We use the Serial.print() function to output the potentiometer and servo values to the Serial Monitor for debugging.
Testing and Troubleshooting
Once the code is uploaded to the Arduino, you should see the servo motor rotate based on the position of the potentiometer. If the servo isn’t responding as expected, here are a few troubleshooting tips:
Check Wiring: Ensure that the potentiometer and servo are wired correctly to the Arduino.
Power Supply: Make sure the Arduino is properly powered. Some servos may require more current than the Arduino can supply, so consider using an external power source if necessary.
Serial Monitor: Open the Serial Monitor to check the potentiometer’s values and ensure the program is reading them correctly.
By now, you’ve learned how to connect a potentiometer to your Arduino, control a servo motor, and write a program that allows you to adjust the servo’s angle using the potentiometer. This project is just the beginning, and you can apply these principles to more complex systems involving sensors, motors, and interactive controls.
In the next part of this article, we will explore advanced topics such as integrating multiple servos, improving system efficiency, and expanding the project to create more complex mechanical systems. Stay tuned for more exciting Arduino tutorials!
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.