beginner 20 minMotorAnalog
Control a Servo with a Knob
Turn a potentiometer and a servo follows your hand: the basis of every robot arm.
A potentiometer gives a voltage between 0 and 5 V. analogRead() turns that into a number from 0 to 1023, and map() scales it to a servo angle from 0 to 180 degrees.
Build it
- 1
Wire the potentiometer
Outer legs to 5V and GND, middle leg to A0.
- 2
Wire the servo
Brown to GND, red to 5V and orange to pin 9.
- 3
Upload and turn the knob
The servo horn follows the knob through half a turn.
The code
servo-knob-control.ino
#include <Servo.h>
Servo arm;
void setup() {
arm.attach(9);
}
void loop() {
int angle = map(analogRead(A0), 0, 1023, 0, 180);
arm.write(angle);
delay(15);
}