Needed Components with eBay Links
Wiring the Circuit
Connect the potentiometer and servo motor to your Arduino as follows:
- Connect the potentiometer's middle pin to analog pin A0 on the Arduino.
- Connect one side pin of the potentiometer to the 5V pin on the Arduino.
- Connect the other side pin of the potentiometer to GND on the Arduino.
- Connect the servo's signal pin (yellow or white) to digital pin 9 on the Arduino.
- Connect the servo's power pin (red) to 5V on the Arduino.
- Connect the servo's ground pin (black or brown) to GND on the Arduino.
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
- Connect your Arduino to your PC via USB.
- Open the Arduino IDE and paste the provided code into the editor.
- Select your board and the appropriate port in the IDE settings.
- Upload the code to the Arduino.
- 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.