Control Servo Motor with Potentiometer: Arduino Tutorial

Difficulty Level: Beginner to Intermediate

Learn how to use an Arduino to control a servo motor's position based on input from a potentiometer. This hands-on project is perfect for beginner to intermediate robotics enthusiasts.

Needed Components with eBay Links

Wiring the Circuit

Connect the potentiometer and servo motor to your Arduino as follows:

Arduino Code to Control Servo Motor

Before uploading the code, ensure the Servo library is included (it comes pre-installed in the Arduino IDE).


// Include the Servo library
#include <Servo.h>

// Create a Servo object
Servo myServo;

int potPin = A0;    // Potentiometer connected to analog pin A0
int val;            // Variable to store potentiometer value

void setup() {
  myServo.attach(9);  // Attach the servo to digital pin 9
}

void loop() {
  val = analogRead(potPin);              // Read potentiometer value
  val = map(val, 0, 1023, 0, 180);       // Map value to servo angle
  myServo.write(val);                    // Move servo to the mapped angle
  delay(15);                             // Short delay for stability
}
            

Upload & Test

  1. Connect your Arduino to your PC via USB.
  2. Open the Arduino IDE and paste the provided code into the editor.
  3. Select your board and the appropriate port in the IDE settings.
  4. Upload the code to the Arduino.
  5. Turn the potentiometer's knob and observe the servo motor's movement.

Conclusion

This experiment demonstrates how to control a servo motor's position using a potentiometer and Arduino. Use this concept as a foundation for advanced projects, such as robotics arms, pan-tilt systems, or remote-controlled devices.