Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

Mastering Servo Motor Control with Arduino and Potentiometer: A Step-by-Step Guide

小编

Published2025-10-15

In this guide, we will show you how to use a potentiometer to control a servo motor with an Arduino. Learn how to write the code, build the circuit, and experiment with different applications. Whether you’re a beginner or an enthusiast looking to expand your Arduino projects, this guide will help you master the basics and spark new ideas for innovation.

servo motor, Arduino, potentiometer, servo motor control, Arduino projects, Arduino code, potentiometer circuit, motor control, electronics, DIY projects, Arduino tutorials

Introduction to Servo Motors and Potentiometers

Arduino, an open-source electronics platform, has gained immense popularity among hobbyists, educators, and professionals alike. One of the best ways to learn and experiment with electronics is by creating simple yet powerful projects. Among the various components you can control with Arduino, the servo motor stands out as an incredibly versatile and easy-to-use part of any project. Whether you're building a robotic arm, a steering mechanism, or any other movement-driven project, servo motors can provide precision control and reliability.

What Is a Servo Motor?

A servo motor is a small but powerful motor that is designed to rotate to a specific angle. Unlike regular DC motors, which only spin continuously in one direction, a servo motor can be controlled to move to a certain angle within a given range. This feature makes them ideal for applications that require precise positioning, such as in robotics, camera gimbals, or even model airplanes.

A typical servo motor consists of a small DC motor, a gearbox, a control circuit, and a potentiometer. The control circuit adjusts the motor's rotation, while the potentiometer helps feedback the motor's position.

Understanding the Potentiometer

A potentiometer is a variable resistor that can be used to measure the position of an object, control the brightness of lights, or even adjust the speed of a motor. It has three pins: one connected to the supply voltage (VCC), one to ground (GND), and the third, the wiper, which gives a variable output. By rotating the knob of the potentiometer, the resistance changes, thus altering the voltage on the wiper pin.

In our case, we will use the potentiometer to provide an analog input to the Arduino. This will allow the Arduino to read the potentiometer’s position and then translate it into an angle for the servo motor. By adjusting the potentiometer, we can precisely control the angle of the servo motor in real time.

The Circuit Setup

Before diving into the code, let’s first set up the circuit. You’ll need:

Arduino Board (Uno, Nano, or any compatible version)

Servo Motor (e.g., SG90)

Potentiometer (10kΩ is a good standard)

Jumper wires

Breadboard (optional, but helpful for prototyping)

Circuit Connections:

Servo Motor:

The red wire connects to 5V on the Arduino.

The brown or black wire connects to GND on the Arduino.

The yellow or white wire connects to a PWM-capable digital pin on the Arduino (e.g., Pin 9).

Potentiometer:

One pin connects to 5V on the Arduino.

The other pin connects to GND on the Arduino.

The wiper pin (middle pin) connects to an analog input pin on the Arduino (e.g., Pin A0).

This simple circuit will allow the Arduino to read the analog value from the potentiometer and control the servo motor accordingly.

Writing the Arduino Code and Testing the Project

Now that the circuit is set up, let’s look at how to write the Arduino code that will read the potentiometer and control the servo motor. We will write a program that continuously reads the potentiometer value, maps it to a suitable servo angle, and adjusts the motor accordingly.

Step 1: Installing the Servo Library

The first thing we need to do is install the Servo library. Fortunately, this library comes pre-installed with the Arduino IDE, so you don’t have to worry about downloading it separately. If you’re unsure whether the library is installed, follow these steps:

Open the Arduino IDE.

Navigate to Sketch > Include Library > Servo.

Once the library is installed, you can start coding!

Step 2: Writing the Code

Here’s the code that controls the servo motor with a potentiometer:

#include // Include the Servo library

Servo myServo; // Create a Servo object

int potPin = A0; // Pin for the potentiometer

int potValue = 0; // Variable to store potentiometer value

int angle = 0; // Variable to store the servo angle

void setup() {

myServo.attach(9); // Attach the servo to pin 9 on the Arduino

Serial.begin(9600); // Start the serial communication for debugging

}

void loop() {

potValue = analogRead(potPin); // Read the potentiometer value

angle = map(potValue, 0, 1023, 0, 180); // Map the value to a range from 0 to 180

myServo.write(angle); // Set the servo position

delay(15); // Wait for the servo to reach the position

// Debugging output (optional)

Serial.print("Potentiometer Value: ");

Serial.print(potValue);

Serial.print(" | Servo Angle: ");

Serial.println(angle);

}

Explanation of the Code

Servo Library: The first line #include imports the Servo library, which makes controlling the servo motor easy.

Servo Object: The line Servo myServo; creates an instance of the Servo class named myServo. This object will control the motor.

Pin Definitions:

int potPin = A0; assigns the analog pin A0 to read the potentiometer.

int potValue = 0; initializes a variable to store the raw potentiometer reading.

int angle = 0; will hold the angle for the servo.

Setup Function:

myServo.attach(9); attaches the servo to pin 9 on the Arduino.

Serial.begin(9600); initializes the serial communication for debugging purposes.

Loop Function:

analogRead(potPin) reads the potentiometer's position and stores it in potValue.

map(potValue, 0, 1023, 0, 180) converts the potentiometer’s raw value (ranging from 0 to 1023) into a corresponding angle between 0 and 180 degrees.

myServo.write(angle); moves the servo to the desired angle.

delay(15); provides a small delay to allow the servo to adjust to its new position smoothly.

Debugging: The Serial.print() lines output the potentiometer value and the corresponding servo angle to the serial monitor, helping you track the behavior in real time.

Step 3: Uploading the Code and Testing

Connect your Arduino to your computer using a USB cable.

Open the Arduino IDE, paste the code above, and select the correct board and port under Tools.

Upload the code to the Arduino.

Open the Serial Monitor (Tools > Serial Monitor) to see the potentiometer value and servo angle.

Test the potentiometer by rotating its knob. You should see the servo motor move accordingly.

Conclusion

By following this guide, you’ve successfully learned how to control a servo motor with a potentiometer using an Arduino. This simple yet powerful project provides a foundation for exploring more complex applications, such as building robotic arms, automated systems, and even interactive art installations.

With this setup, you can experiment further by adding more servos, sensors, or even using digital controls to create more sophisticated movement systems. The possibilities are endless, and mastering servo control with Arduino opens the door to a world of exciting electronics projects.

If you're looking for more inspiration or want to dive deeper into Arduino, there are plenty of resources available to keep your creativity flowing. Whether you're a hobbyist or a professional, this project is just the beginning of what you can achieve with Arduino and servo motors!

Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.