TPL-18Templates/Servo Sweep
Servo Sweep
Read a potentiometer's position and use it to set a servo's angle in real time — a classic analog-input-to-motion demo.
Open in Playgroundbeginner · servo · analog input · motor · PWM
FIG. 1Servo Sweep · circuitTPL-18
02Sketch
sketch.inoArduino C++
29 lines
// Servo Motor — Sweep via Potentiometer
// Turn the knob to move the servo horn from 0° to 180°.
#include <Servo.h>
const int POT_PIN = A0;
const int SERVO_PIN = 9;
Servo myServo;
void setup() {
myServo.attach(SERVO_PIN);
Serial.begin(9600);
Serial.println("Turn the potentiometer to move the servo!");
}
void loop() {
int raw = analogRead(POT_PIN); // 0–1023
int angle = map(raw, 0, 1023, 0, 180);
myServo.write(angle);
Serial.print("Pot: ");
Serial.print(raw);
Serial.print(" Servo angle: ");
Serial.println(angle);
delay(50);
}03Components
- 01Arduino Uno
- 02Potentiometer
- 03Servo Motor
04Wiring
| Component pin | Arduino pin |
|---|---|
| Potentiometer VCC | 5V |
| Potentiometer GND | GND |
| Potentiometer Signal | A0 |
| Servo V+ | 5V |
| Servo GND | GND |
| Servo PWM | Pin 9 |
Ready when you are
Have a circuit in mind?
No hardware, no installs — a browser is enough.