Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.

Day 14 of 30 · about 32 minutes

Servo motors: a pointer gauge

Today you build: A knob-controlled pointer gauge

  • Read 4 min
  • Wire servo, pot & capacitor 8 min
  • Upload & test 4 min
  • Try it 16 min

Community draft — report a mistake

A servo is a small motor with a gearbox and a position sensor inside. You tell it an angle from 0° to 180° and it holds there. Cardboard dial + servo arm = a gauge needle.

Wiring

SG90 wire Uno
Brown (GND) GND
Red (power) 5V
Orange (signal) pin 9

The potentiometer from day 4 goes back on: outer legs to 5V and GND, middle to A0.

Put a 100–470 µF capacitor across the servo’s 5V and GND, close to the servo (long leg / + side to 5V). When the motor starts it pulls a short burst of current; without the capacitor, the Uno’s 5V dips and the board can reset.

The Servo library

Servo comes with Arduino IDE (no install). pointer.attach(9) starts sending the control pulse — a 1–2 ms pulse 50 times a second, where the pulse length is the angle. pointer.write(angle) changes it.

The library uses the Uno’s Timer1, so analogWrite() stops working on pins 9 and 10 while a servo is attached. That’s why day 4’s LED had to move.

Smoother needle

  • Averaging eight readings removes most of the pot’s jitter.
  • Only writing when the angle changes by 2° or more stops the servo from buzzing back and forth between two angles.

If it still twitches, the servo jitter fix covers power and noise.

Powering more than one servo

One SG90 doing light work from the Uno’s 5V is fine for learning. Two or more, or a big metal-gear servo like the MG996R, need a separate 5 V 2 A supply with its GND joined to the Uno’s GND. SG90 vs MG996R explains the difference.

Try it

  • Draw a dial on cardboard (0–100 %) and tape it behind the arm.
  • Show day 10’s temperature on the gauge: map(temp, 15, 45, 180, 0).

You’re done when…

  • Turning the knob end to end sweeps the arm smoothly from 0° to 180°, and the Serial Monitor (9600 baud) shows raw=… angle=… only when the angle changes.
  • With the knob still, the servo is silent — no buzzing back and forth.
  • Fast knob turns don’t make the board reset (the capacitor is doing its job).

Today's code

Verified — core avr 1.8.8, Servo 1.3.0, 3 Nov 2026Compile-only
lesson-servo.inoArduino C++Open in PlaygroundOpen in Playground
// Day 14 — Servo motors: a pointer gauge (Arduino Uno / Nano)
// SG90 servo: brown → GND, red → 5V, orange (signal) → pin 9.
// Potentiometer from day 4: outer legs → 5V and GND, middle leg → A0.
// Put a 100–470 µF capacitor across the servo's 5V and GND, close to the servo.
#include <Servo.h>

const int SERVO_PIN = 9;
const int POT_PIN = A0;

Servo pointer;
int lastAngle = -1;

void setup() {
  Serial.begin(9600);
  pointer.attach(SERVO_PIN);   // the Servo library takes over Timer1: no analogWrite on pins 9/10 now
}

void loop() {
  // Average a few readings so a noisy pot doesn't make the needle shiver.
  long sum = 0;
  for (int i = 0; i < 8; i++) sum += analogRead(POT_PIN);
  int raw = sum / 8;
  int angle = map(raw, 0, 1023, 0, 180);

  // Only move when the change is real (2° or more). Less buzzing, less current.
  if (lastAngle < 0 || abs(angle - lastAngle) >= 2) {
    pointer.write(angle);
    lastAngle = angle;
    Serial.print(F("raw="));
    Serial.print(raw);
    Serial.print(F("  angle="));
    Serial.println(angle);
  }
  delay(20);
}

↑↓ to move · Enter to open