Before you start
Arduino UNO R3, a documented positional servo, suitable separate low-voltage supply; upload code with the mechanism detached.
You will leave with
A three-wire power plan, a midpoint test, and an optional knob-controlled movement test.
Before a servo moves a pencil-case hatch, prove that its power and signal work with the mechanism disconnected. This guide separates those jobs into a connection diagram, a single-position sketch and an optional manual control.
The pencil-case design behind this exercise specifies an MG996R-size servo. A tiny SG90 is not a drop-in replacement for that mechanical design. Neither the reference wiring nor the code below establishes how much load your actual servo can safely move.
1. Identify the servo and its supply
Start with the label and supplier documentation, not the shape of the case. The manufacturer's MG996R reference lists a 4.8–6.6 V operating range and a 1.4 A stall current. These are reference specifications, not measurements of a clone or of the pencil-case mechanism. Choose a regulated supply within your unit's range, with current capacity and connectors appropriate to its startup and load. Do not use the no-load current as the supply's required peak capacity.
For a first unloaded trial, a documented regulated 5 V supply is an option only when the exact servo accepts 5 V. A higher current-capacity rating does not increase the voltage or force the servo to draw that current. Stop for overheating, repeated resets, sustained buzzing or binding; deliberately stalling the output is not a sizing method.
| Part | What to verify |
|---|---|
| Arduino UNO R3 and data cable | This guide targets the classic 5 V ATmega328P board. |
| One positional servo, original horn removed | Voltage, signal requirements, pulse range and lead functions. |
| Separate regulated DC supply | Voltage and peak-current capability for that servo; enclosed, ready-made adapter. |
| Rated terminal block or servo breakout and leads | Wire/connector ratings, secure joints, and no exposed positive contacts. |
| Optional 10 kΩ linear potentiometer | Three terminals: two ends and a centre wiper. |

A smaller SG90-labelled servo illustrates the three-wire lead, output shaft and supplied horns. This is not the MG996R-size servo specified by the pencil-case design, and is not a drop-in replacement. Identify your actual servo before using its voltage, pulse or torque ratings. This is a third-party component photo, not a Workbench test.
Photo: Luiz Augusto Souza de Mesquita · CC0 1.0. Wikimedia preview; no further edits. Image credits and scope.2. Wire one signal and a shared reference
| From | To | Job |
|---|---|---|
| UNO D9 | Servo signal | Control pulses only |
| Servo supply positive | Servo positive | Motor power |
| Servo supply negative | Servo ground and UNO GND | Motor return and shared signal reference |
Build those connections with both supplies off. Run the motor return directly to its supply junction, with a separate reference lead to the UNO. Keep motor current out of the UNO, breadboard power rails and thin signal jumpers. Leave the servo supply's positive separate from UNO 5V, VIN and USB. A shared ground is not a shared positive rail. This follows the separate-supply principle in Adafruit's servo wiring guide.
A ready-made servo breakout and rated screw terminals can avoid soldering for a bench trial. They still need correct polarity and strain relief. A wire twisted around another wire and wrapped in tape is not an equivalent connector.
3. Ask for one position first
Install the Servo library through the Arduino IDE Library Manager if it is unavailable. Select Arduino Uno and the correct port. Upload with servo power off and the horn/linkage removed. The sketch requests 1500 microseconds, a nominal centre command for the documented class of servo—not a measured 90-degree shaft angle. Verify that this command is supported by your servo before applying power.
Read the complete sketch
// Workbench — one unloaded positional servo; Arduino UNO R3.
// External regulated supply appropriate for the exact servo; shared ground.
#include <Servo.h>
Servo joint;
const byte SERVO_PIN = 9;
const int CENTRE_US = 1500; // Command only; verify your servo's accepted pulse range.
void setup() {
joint.attach(SERVO_PIN);
joint.writeMicroseconds(CENTRE_US);
}
void loop() {
// Hold a single nominal midpoint. No automatic sweep or lid movement.
}
Once the UNO is running, energise the servo supply with the shaft unobstructed. Record whether it settles, continues rotating, twitches or causes a reset. A continuous-rotation servo interprets the same command differently; stop and identify the unit if behaviour contradicts the intended positional device. Before powering the UNO down, switch off servo power too.
4. Add a knob only after the midpoint works
With power off, connect the potentiometer's outer terminals to UNO 5V and GND, and its wiper to A0. Reversing the outer connections reverses the knob direction. Use the following sketch only with the servo still detached from the mechanism. It samples every 20 ms and maps the input into a deliberately narrow 1400–1600 microsecond window. This is a starting command range, not a guarantee of safe travel for an unidentified servo.
Read the complete sketch
// Workbench — detached positional servo controlled by a 10k potentiometer.
// UNO R3: A0 = wiper, D9 = servo signal. See tutorial for separate power.
#include <Servo.h>
Servo joint;
const byte SERVO_PIN = 9;
const byte KNOB_PIN = A0;
const int MIN_US = 1400; // Deliberately narrow starting window; check exact servo.
const int MAX_US = 1600;
const unsigned long SAMPLE_MS = 20UL;
unsigned long lastSampleAt = 0;
int commandUs = 1500;
void setup() {
joint.attach(SERVO_PIN);
joint.writeMicroseconds(commandUs);
}
void loop() {
const unsigned long now = millis();
if (now - lastSampleAt < SAMPLE_MS) return;
lastSampleAt = now;
const int raw = constrain(analogRead(KNOB_PIN), 0, 1023);
const int targetUs = map(raw, 0, 1023, MIN_US, MAX_US);
// A small deadband avoids repeatedly changing almost identical commands.
if (abs(targetUs - commandUs) >= 4) {
commandUs = targetUs;
joint.writeMicroseconds(commandUs);
}
}
| Code idea | Plain-English meaning |
|---|---|
| analogRead(A0) | Read the knob as a number from 0 to 1023 on the UNO R3. |
| map(..., 1400, 1600) | Translate that reading into the chosen pulse range. |
| now − lastSampleAt | Run the update when enough time has passed, including across timer rollover. |
| abs(target − command) ≥ 4 | Ignore very small command changes. This does not fix a weak supply. |
The Servo library uses Timer1 on UNO R3. Avoid analogWrite on D9/D10 while Servo is attached, even if its signal is moved elsewhere. Servo.read reports the requested command, not a position sensor reading. See the Servo API and timer definitions.
5. Troubleshoot the observation, not the whole project
| Observation | First isolation step |
|---|---|
| UNO restarts when motion begins | Return to midpoint only; inspect the separate power path and connections. Measure supply voltage during movement if you have suitable equipment. |
| No movement | Verify pin functions and the common ground with power off. Confirm successful upload and supply output. |
| Buzzing or heating | Disconnect power. Remove load and check mechanical travel before another command. |
| Knob makes the servo jump | Test the potentiometer reading alone; a disconnected wiper may float. |
6. Record a result before adding the hatch
Use the servo bench record to note the exact model, supply, pulse range and observations. Only after a repeatable unloaded result should you assess the mechanism's torque, end positions and pinch points. A successful free-running servo test does not validate an automatic closing lid. Continue with the pencil-case power and control plan.
Reference exercise · Read the evidence and checking record.
Where to go next
Something unclear? Send a correction or a question.