Controlling a Servo with a Potentiometer Using Arduino

Controlling a Servo with a Potentiometer Using Arduino

In this tutorial, we will learn how to control a servo motor using a potentiometer with an Arduino. The potentiometer allows us to adjust the angle of the servo smoothly, giving us real-time control over its position. By the end of this project, you will have a functional setup that displays the potentiometer value and the corresponding servo angle.

As the potentiometer is turned, the angle of the servo motor changes from 0 to 180 degrees. We will use the Arduino's analog input to read the potentiometer value and then map this value to the servo's range using a simple formula. This project is an excellent way to understand how analog inputs work and how to control motors with them (in video at 01:45).

Hardware Explained

For this project, we need an Arduino board, a servo motor, and a potentiometer. The potentiometer acts as a variable resistor, providing a voltage output that corresponds to its position. This output is read by the Arduino's analog input, allowing us to determine how far to turn the servo.

The servo motor is a type of motor that can be positioned at a specific angle. It receives a control signal, which tells it the desired position. The servo will rotate to that angle based on the input it receives from the Arduino, making it useful for various applications such as robotics and remote control devices.

Datasheet Details

ManufacturerVarious
Part numberStandard Servo
Logic/IO voltage5 V
Supply voltage4.8 – 6 V
Output current (per channel)Up to 1 A
Peak current (per channel)Up to 2 A
PWM frequency guidance50 Hz
Input logic thresholds0.8 V (low) / 2.0 V (high)
Voltage drop / RDS(on) / saturation0.5 V
Thermal limits85 °C
PackageStandard size
Notes / variantsAvailable in different sizes and torque ratings

  • Ensure power supply is within limits (4.8 – 6 V).
  • Use a suitable resistor value for the potentiometer (10 kΩ recommended).
  • Check polarity of connections to avoid reversed operation.
  • Be cautious of heat; ensure servo is not overloaded.
  • Keep wiring short to reduce interference.
  • Use decoupling capacitors across power inputs if needed.
  • Verify that the potentiometer is correctly connected to analog pin A0.

Wiring Instructions

Arduino wiring or servo motor with pot
Arduino wiring or servo motor with pot

To wire the components, first connect the potentiometer. The middle pin of the potentiometer should be connected to the analog pin A0 on the Arduino. The left pin should go to the 5V pin on the Arduino, and the right pin should connect to the ground (GND).

Next, connect the servo motor. The ground wire (typically black or brown) from the servo should be connected to the Arduino's GND. The power wire (usually red) should connect to the 5V pin on the Arduino. Finally, the control wire (often yellow or white) should be connected to digital pin 9 on the Arduino. This setup will allow the Arduino to control the servo based on the potentiometer's position.

Arduino Schemtic of wiring servo motor with pot

Code Examples & Walkthrough

Let's take a look at the key parts of the code. First, we include the Servo library and create a servo object:

#include 
Servo myservo;  // create servo object to control a servo

This code initializes the servo and prepares it for control. The next step involves reading the potentiometer value:

int val = analogRead(potpin); // reads the value of the potentiometer

Here, the value read from the potentiometer is stored in the variable val. The value will be between 0 and 1023, corresponding to the potentiometer's position. We then map this value to the servo's range:

val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo

This line converts the potentiometer's value to a range suitable for the servo motor. Finally, we set the servo to the calculated position:

myservo.write(val); // sets the servo position according to the scaled value

This command tells the servo to move to the specified angle based on the potentiometer's position. You can find the complete code loaded below the article.

Demonstration / What to Expect

When you run the program, turning the potentiometer will adjust the angle of the servo motor smoothly. You should see the angle values being printed in the serial monitor as you turn the knob. If the servo behaves unexpectedly, check your wiring, especially the connections to the potentiometer and the servo (in video at 04:30).

The servo should respond immediately to the changes in the potentiometer's position. If you notice any lag or jumpiness, consider reducing the delay in the code to improve responsiveness.

Images

Arduino wiring or servo motor with pot
Arduino wiring or servo motor with pot
Arduino Schemtic of wiring servo motor with pot
Arduino Schemtic of wiring servo motor with pot
SG90_servo_motor-1
SG90_servo_motor-1
SG90_servo_motor-0
SG90_servo_motor-0
96-Controlling a servo position using a potentiometer
Language: C++
Copied!

Things you might need

Resources & references

Files📁

No files available.