03 / Templates · beginner
SERVO SWEEP
Open in PlaygroundRead a potentiometer's position and use it to set a servo's angle in real time — a classic analog-input-to-motion demo.
beginnerservoanalog inputmotorPWM
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);
}- Arduino Uno01
- Potentiometer02
- Servo Motor03
| Component Pin | Arduino Pin |
|---|---|
| Potentiometer VCC | 5V |
| Potentiometer GND | GND |
| Potentiometer Signal | A0 |
| Servo V+ | 5V |
| Servo GND | GND |
| Servo PWM | Pin 9 |