CNAP
03 / Templates · beginner

Read 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
Circuit
Code
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);
}
Components
  • Arduino Uno
    01
  • Potentiometer
    02
  • Servo Motor
    03
Wiring
Component PinArduino Pin
Potentiometer VCC5V
Potentiometer GNDGND
Potentiometer SignalA0
Servo V+5V
Servo GNDGND
Servo PWMPin 9