MyRoboPathOpen Robotics Lab
Arduino Uno #8 of 35
Est. 0.75 Hours
Budget: $3 - $6
Beginner

SG90 Micro Servo Motor Precision Controller

Command exact angular positions from 0° to 180° using 50Hz PWM control pulses and smooth ease-in motion sweeps.

Control mechanical angle accurately with the SG90 9g micro servo motor. Learn how 1ms–2ms pulse widths inside a 20ms frame dictate closed-loop internal potentiometer position, wire proper power decoupling, and drive smooth sweeps and Serial angle commands.

Arduino Uno R3SG90 Micro Servo50Hz PWM ServoServo.h LibraryAngular Motion
SG90 Micro Servo Motor Precision Controller Blueprint
SG90 Micro Servo Motor Precision ControllerCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Understand 50Hz servo control frames (20ms period, ~544µs for 0°, ~1500µs for 90°, ~2400µs for 180°).
2Recognize Servo.h Timer1 usage (disables analogWrite PWM on pins D9 and D10 for non-servo LEDs).
3Prevent microcontroller brownouts using bulk decoupling capacitance and external power rails.
4Parse Serial integer commands to move a servo interactively to any target angle.

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

A single unloaded SG90 can run during bench tests from Arduino 5V with a 470µF decoupling capacitor across 5V/GND, while loaded or multi-servo setups must use an external 5V 2A supply with common GND tied to the Uno. Never force servo horn past mechanical end-stops; add 470µF capacitor across 5V/GND (or external 5V PSU) to prevent Uno brownout resets.

SG90 Micro Servo Motor Precision Controller Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for SG90 Micro Servo Motor Precision Controller
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P @ 16MHz1$8.00
SG90 9g Micro Servo Motor4.8V–6.0V, 1.8 kg·cm torque, 180° range1$2.50
470µF 16V Electrolytic CapacitorSuppresses motor startup voltage dips1$0.20
Breadboard & Jumper Wires3× Male-to-Male jumpers1$2.00

Electrical & System Architecture

Connect SG90 Orange/Yellow signal wire to D9, Red wire to 5V (with 470µF electrolytic decoupling cap to GND), and Brown/Black wire to Arduino GND.

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for SG90 Micro Servo Motor Precision Controller.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for SG90 Micro Servo Motor Precision Controller.Wiring Schematic

Autonomous Behavior & Finite State Machine (FSM)

Deterministic Arduino loop handling SG90 9g Micro Servo (180° Positional) acquisition, filtering, and real-time output updates.

Figure: Firmware execution flow and state transitions for SG90 Micro Servo Motor Precision Controller.
Figure: Firmware execution flow and state transitions for SG90 Micro Servo Motor Precision Controller.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Identify SG90 Wire Color Code

Duration: 5 Mins

Confirm signal, power, and ground wire colors before making breadboard connections.

SG90 Micro Servo Motor Precision Controller hardware layout diagram
Stage 1: SG90 9g Micro Servo (180° Positional) hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • Brown (or Black) = GND, Red = +5V VCC, Orange (or Yellow) = Signal.
  • Press a plastic servo horn lightly onto the output spline (do not over-force the gears).
  • Prepare a 470µF electrolytic capacitor noting its white negative stripe.
Engineering & Tuning Tip: For more than one servo—or any servo carrying a mechanical load—always power Red/Brown from an external 5V supply sharing GND with Uno.
2

Wire SG90 & Bulk Decoupling Capacitor

Duration: 10 Mins

Connect signal to D9 and place the 470µF cap across the breadboard 5V and GND rails.

SG90 Micro Servo Motor Precision Controller breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Connect Arduino 5V and GND to the breadboard power rails.
  • Insert the 470µF capacitor with negative stripe to GND and positive lead to 5V.
  • Connect SG90 Brown→GND, Red→5V rail, and Orange→Arduino D9.
Engineering & Tuning Tip: Respect electrolytic capacitor polarity—installing it backwards can damage the capacitor!
3

Verify Safe Mechanical Travel Range

Duration: 5 Mins

Confirm the horn has clear space to rotate 180° without striking wires or the desk.

SG90 Micro Servo Motor Precision Controller power rail protection diagram
Stage 3: Supply rail check (5.0V Rail + 470µF Decoupling Cap (External 5V for multi-servo)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • Double-check all three servo leads and capacitor orientation.
  • Hold the servo body gently on the workbench so torque does not flip it.
  • Plug in USB power.
Engineering & Tuning Tip: Many budget SG90 clones hit hard mechanical stops near 5° and 175°; limiting sweeps to 10°–170° prevents buzzing stall current.
4

Upload Sweep & Interactive Angle Controller

Duration: 10 Mins

Flash the firmware to perform an initial calibration sweep, then accept live Serial angles.

SG90 Micro Servo Motor Precision Controller serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • Upload the production sketch below.
  • Watch the horn perform one smooth calibration sweep (15° → 165° → 90°).
  • Open Serial Monitor (115200 baud, Newline) and type angles like 30, 90, or 150.
Engineering & Tuning Tip: Typing angles in Serial Monitor lets you calibrate robot gripper open/close endpoints precisely.
5

Verify Zero Jitter & Smooth Slew Rate

Duration: 5 Mins

Listen for buzzing at endpoints and tune safe software limits.

SG90 Micro Servo Motor Precision Controller live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • Command 10° and 170° and confirm the servo rests quietly once positioned.
  • If buzzing persists at 0° or 180°, tighten MIN_ANGLE / MAX_ANGLE slightly.
  • Observe smooth ramped motion versus abrupt full-speed snaps.
Engineering & Tuning Tip: For 4–16 servos (like robotic arms or hexapods), offload PWM generation to an I2C PCA9685 16-channel driver.

Subsystem Benchtop Verification Scripts

Test each hardware subsystem independently before deploying the full autonomous firmware.

1Bench Diagnostic: SG90 9g Micro Servo (180° Positional) Hardware Check

Subsystem Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for SG90 9g Micro Servo (180° Positional) before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// Diagnostic: Servo 90-Degree Centering Check
#include <Servo.h>
Servo s;
void setup() { s.attach(9); s.write(90); }
void loop() {}
Expected Output: Servo moves immediately to exact center (90°) and holds steadily.

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_sg90_servo_controller.ino
arduino_uno_sg90_servo_controller.inoArduino C++
/**
 * Project 8: SG90 Micro Servo Smooth Sweep & Serial Positioner
 * Board: Arduino Uno R3 | Pin: D9 (Signal)
 */
#include <Servo.h>

const uint8_t SERVO_PIN = 9;
const int MIN_ANGLE = 10;
const int MAX_ANGLE = 170;

Servo myServo;
int currentAngle = 90;

void moveSmooth(int targetAngle, int stepDelayMs = 15) {
  targetAngle = constrain(targetAngle, MIN_ANGLE, MAX_ANGLE);
  int step = (targetAngle >= currentAngle) ? 1 : -1;
  while (currentAngle != targetAngle) {
    currentAngle += step;
    myServo.write(currentAngle);
    delay(stepDelayMs);
  }
}

void setup() {
  Serial.begin(115200);
  myServo.attach(SERVO_PIN);
  myServo.write(currentAngle);
  Serial.println(F("SG90 Controller Ready. Running calibration sweep..."));
  moveSmooth(15, 12);
  moveSmooth(165, 12);
  moveSmooth(90, 12);
  Serial.println(F("Enter angle (10-170) in Serial Monitor:"));
}

void loop() {
  if (Serial.available() > 0) {
    int target = Serial.parseInt();
    while (Serial.available() > 0) Serial.read(); // flush newline
    if (target >= 0 && target <= 180) {
      Serial.print(F("Slewing to ")); Serial.print(target); Serial.println(F(" deg"));
      moveSmooth(target, 12);
    }
  }
}

Firmware Architecture Explanation:

  • Uses Servo.h Timer1 interrupts to emit jitter-free 50Hz control pulses on D9.
  • moveSmooth() ramps 1° at a time to reduce gear wear and current spikes during large swings.
  • Constrains angles to 10°–170° to prevent hitting plastic end-stops on clone SG90 units.

Operational Verification & Pin State Matrix

1Double-check all 3 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: Servo moves immediately to exact center (90°) and holds steadily.
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for SG90 Micro Servo Motor Precision Controller
Input / StatePin / SignalDetected ConditionExpected Output
Command 10°Pulse ≈ 650µsLeft Mechanical Safe LimitSlew CCW to 10°
Command 90°Pulse ≈ 1500µsNeutral CenterSlew to 90° Home
Command 170°Pulse ≈ 2300µsRight Mechanical Safe LimitSlew CW to 170°

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for SG90 Micro Servo Motor Precision Controller
ProblemLikely CauseDiagnostic CheckEngineering Solution
Arduino Uno resets whenever servo starts movingHigh startup surge dipping USB 5V railObserve Uno power LED flickering during motionAdd a 470µF capacitor across 5V/GND or power servo from an external 5V supply with common GND.
Servo buzzes continuously at 0° or 180°Horn pushing against internal physical end-stopCommand 15° and 165° to see if buzzing stopsKeep travel inside 10°–170° or calibrate attach(pin, minUs, maxUs).
Servo twitches erraticallyMissing common ground between external supply and UnoVerify continuity between external GND and Uno GNDConnect external PSU negative rail directly to Arduino GND.

Engineering Challenges & Upgrades

Take your build to the next level with these progressive engineering upgrades:

Level 1Ultrasonic Sonar Radar Scanner

Mount Project 7 (HC-SR04) on the SG90 horn to scan 15°–165° and log angle:distance pairs.

Level 2Potentiometer Follow-Me Arm

Map analogRead(A0) directly to servo angle with exponential smoothing.

Level 3Automated Gate Barrier

Pair with RFID (Project 9) to lift a miniature toll gate for 4 seconds on valid tag scan.