Servo fader on an Arduino Uno
A slide potentiometer on A0 sets the angle of a servo on pin 9. Push the fader and the horn follows. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino Servo Motor with a Potentiometer
What you'll learn
- Reading a slide potentiometer
- Servo pulse widths and angles
- Mapping a range with map
- Averaging readings to reduce noise
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A slide potentiometer has 5 V on one end and ground on the other. Its wiper goes to A0, so the reading follows the slider position from 0 to 1023. The sketch turns that into an angle from 0 to 180 degrees for a servo on pin 9.
A servo does not take PWM like an LED. It takes a pulse width: about 544 microseconds puts the horn at one end, 2400 at the other. The servo holds the last angle it was told, and moves there at its own speed.
The sketch averages four readings so the horn does not twitch.
Parts list
4 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Slide potentiometerIn Spaceship Panel Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Hobby servo pin PWM | Arduino Uno R3 pin 9 |
| Slide potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Hobby servo pin VCC | Arduino Uno R3 pin 5V |
| Slide potentiometer pin 1 | Arduino Uno R3 pin GND |
| Hobby servo pin GND | Arduino Uno R3 pin GND |
| Slide potentiometer pin 2 | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Servo fader: push the slider, the horn follows.
//
// A0 the slide potentiometer's wiper (its ends on 5 V and GND)
// pin 9 the servo's signal wire (its red wire on 5 V, brown on GND)
//
// A servo does not take PWM the way an LED does. It takes a pulse width: a
// pulse of 544 us once every 20 ms puts the horn at one end of its travel,
// 2400 us puts it at the other (the Arduino Servo library's numbers), and
// anything between is an angle between. The servo holds whatever it was last
// told, and it walks there at its own speed, about a tenth of a second for
// sixty degrees.
//
// So the sketch reads the fader, turns 0..1023 into 0..180 degrees and sends
// the matching pulse, fifty times a second. mokxi_servo.h makes the pulse with
// delayMicroseconds, since the Uno's analogWrite runs far too fast for a servo
// to read.
//
// The reading is averaged over a few samples so the horn does not twitch on
// the last count of the ADC, and the angle is printed only when it changes.
#include "mokxi_servo.h"
const int FADER = A0;
const int SIGNAL = 9;
Servo horn(SIGNAL);
int shown = -1;
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: a fader on A0 drives a servo on pin 9");
horn.begin();
horn.write(90);
}
void loop() {
long sum = 0;
for (int i = 0; i < 4; i++) {
sum += analogRead(FADER);
}
int reading = (int)(sum / 4);
int angle = (int)map(reading, 0, 1023, 0, 180);
horn.write(angle);
if (angle != shown) {
shown = angle;
Serial.print("fader ");
Serial.print(reading);
Serial.print(" -> ");
Serial.print(angle);
Serial.println(" degrees");
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens serial at 115200 baud, starts the servo with horn.begin(), and sends it to 90 degrees so it starts in the middle.
void setup() {
Serial.begin(115200);
Serial.println("Mokxi Uno: a fader on A0 drives a servo on pin 9");
horn.begin();
horn.write(90);
}loop sketch.ino
Adds four analogRead samples from FADER and divides by four. It maps 0 to 1023 onto 0 to 180 degrees and sends the angle with horn.write. It prints only when the angle changes, then waits 20 ms, so the pulse goes out about fifty times a second.
void loop() {
long sum = 0;
for (int i = 0; i < 4; i++) {
sum += analogRead(FADER);
}
int reading = (int)(sum / 4);
int angle = (int)map(reading, 0, 1023, 0, 180);
horn.write(angle);
if (angle != shown) {
shown = angle;
Serial.print("fader ");
Serial.print(reading);
Serial.print(" -> ");
Serial.print(angle);
Serial.println(" degrees");
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The servo moves backward This project
Swap the potentiometer's outer legs, or change themapso 0 to 1023 gives 180 to 0. - The servo buzzes at the ends This project
Keep the angle within 0 to 180. A servo pushed past its limit strains against its stop. - Readings jump around, or the pin reads random values. Slide potentiometer
The wiper must go to an analog input and both end pins to 5 V and GND. If an end is loose, the wiper floats. Check all three connections. - Pushing the slider up lowers the number. Slide potentiometer
Swap the two end pins, or flip it in code with map(value, 0, 1023, 1023, 0). The wiper reads high when it is nearer the end tied to 5 V. - The servo jitters, twitches or resets the board when it moves. Hobby servo
Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply. - The servo hums at the end of its travel or will not reach full angle. Hobby servo
Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.