Built-in project · Arduino Uno R3

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.

BeginnerRuns in your browser. Free, and no account needed.

Step by step, with the wiring and the common mistakes: Arduino Servo Motor with a Potentiometer

Servo fader · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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 1 ms once every 20 ms puts the horn at one end of its travel, 2 ms
// puts it at the other, 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);
}

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 9; Hobby servo pin PWM
  • Arduino Uno R3 pin 5V; Slide potentiometer pin 3; Hobby servo pin VCC
  • Ground: Arduino Uno R3 pin GND; Slide potentiometer pin 1; Hobby servo pin GND
  • Arduino Uno R3 pin A0; Slide potentiometer pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.