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

Servo Potentiometer Arduino Code: A Simple Guide to Control Your Servo Motor with Precision

小编

Published2025-10-15

Introduction to Servo Motors and Potentiometers in Arduino Projects

The world of Arduino is a playground for hobbyists, engineers, and tech enthusiasts alike, offering endless possibilities for creative projects. One of the most common applications in Arduino projects is controlling a servo motor. A servo motor provides precise control over angular position, which makes it ideal for applications like robotics, camera gimbals, and even automated doors. But what if you could control a servo motor using a simple, user-friendly component like a potentiometer?

A potentiometer is a three-terminal variable resistor that can be adjusted manually to change the voltage in a circuit. When combined with an Arduino, a potentiometer provides a simple way to control the position of a servo motor, giving you analog input to control your device precisely. This article will guide you through the process of writing Arduino code to control a servo motor with a potentiometer, from setting up your hardware to implementing the code.

What You’ll Need for This Project:

To begin, you'll need a few essential components:

Arduino board (e.g., Arduino Uno) – This will be the brain of the operation, processing the input from the potentiometer and controlling the servo motor accordingly.

Servo motor – A standard servo motor, like the SG90, is perfect for this kind of project. It allows you to control the angle of the motor’s shaft with precision.

Potentiometer – A 10kΩ potentiometer works well, as it offers a sufficient range for controlling the servo. It will act as an analog input to the Arduino.

Jumper wires – Used to make connections between the Arduino, potentiometer, and the servo motor.

Breadboard (optional) – To organize your components and make it easier to connect everything.

External power source (optional) – For larger servo motors, it’s best to use an external power source instead of relying solely on the Arduino’s 5V output.

Wiring the Components:

Before diving into the code, let’s first look at how to wire everything. Here’s a simple schematic for the connections:

Connect the potentiometer to the Arduino. The potentiometer has three pins: one for ground (GND), one for voltage (5V), and one for the output (which will send the analog signal to the Arduino). Connect the GND pin of the potentiometer to the Arduino GND, the VCC pin to the Arduino 5V, and the output pin to one of the Arduino's analog input pins (A0 is a common choice).

Connect the servo motor to the Arduino. The servo has three pins: one for power (5V), one for ground (GND), and one for control (signal). Connect the servo’s power pin to the Arduino 5V, the ground pin to the Arduino GND, and the signal pin to one of the Arduino’s PWM (Pulse Width Modulation) pins, typically pin 9.

With everything wired up, your hardware is now ready for the magic to happen.

Writing the Arduino Code for Servo Potentiometer Control

Now that the hardware setup is complete, it’s time to focus on the code. The idea is simple: the potentiometer will send an analog signal to the Arduino, and based on the input voltage, the Arduino will adjust the position of the servo motor. Here’s how you can write the code for this process.

Step 1: Include the Servo Library

Arduino makes controlling servos easy with its built-in Servo library. Begin your code by including the library at the top of your sketch. This gives you access to all the functions needed to control the servo motor.

#include

Step 2: Define Variables

Next, define the variables for the potentiometer input and the servo motor. You’ll also need to define the pin number where the servo is connected. In this example, we’ll use pin 9 for the servo and pin A0 for the potentiometer.

int potPin = A0; // Pin where the potentiometer is connected

int val = 0; // Variable to store the potentiometer value

Servo myServo; // Create a Servo object to control the motor

Step 3: Setup the Servo Motor

In the setup() function, initialize the servo motor and start the serial monitor for debugging. Setting the servo’s pin to the appropriate PWM pin (in this case, pin 9) will allow us to send the control signal.

void setup() {

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

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

}

Step 4: Read the Potentiometer Value

In the loop() function, read the analog value from the potentiometer using analogRead(). This function returns a value between 0 and 1023, corresponding to the potentiometer’s position. You’ll need to map this value to the servo’s range of 0 to 180 degrees.

void loop() {

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

val = map(val, 0, 1023, 0, 180); // Map the value to the servo range

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

Serial.println(val); // Print the value for debugging

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

}

Step 5: Upload the Code

Once you’ve written the code, it’s time to upload it to the Arduino. Open the Arduino IDE, select the correct board and port, and click on the upload button. The code will compile and upload to your Arduino board.

How the Code Works:

The analogRead() function reads the voltage from the potentiometer. The voltage is between 0 and 5V, and analogRead() converts this into a digital value between 0 and 1023.

The map() function takes the potentiometer value and maps it to a range suitable for the servo (0 to 180 degrees).

The myServo.write(val) function sends this value to the servo, which adjusts its position accordingly.

The delay(15) ensures the servo has enough time to move to the new position before the next reading.

Troubleshooting Tips:

Servo not moving correctly? Ensure that your potentiometer is wired correctly, and that it’s providing an analog signal between 0 and 5V.

Servo jittering? The delay(15) is crucial for ensuring smooth operation. Too little delay may cause jittering, while too much can slow down the response.

Servo not turning to full range? Check the range of the potentiometer. A faulty or incorrectly wired potentiometer might not cover the full range from 0 to 5V.

Conclusion

By following these simple steps, you’ve learned how to use an Arduino, a potentiometer, and a servo motor to create a precise control system. Whether you’re using this project for robotics, automation, or just as a learning experience, the combination of analog input and digital control is incredibly powerful and versatile.

This project is just one example of what you can achieve with Arduino. Once you’re comfortable with the basics of controlling a servo using a potentiometer, you can expand your project by adding more servos, incorporating sensors, or even adding wireless control via Bluetooth or Wi-Fi. The possibilities are endless, and the only limit is your imagination.

Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.

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.