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.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerA 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.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| SG90 9g Micro Servo Motor | 4.8V–6.0V, 1.8 kg·cm torque, 180° range | 1 | $2.50 |
| 470µF 16V Electrolytic Capacitor | Suppresses motor startup voltage dips | 1 | $0.20 |
| Breadboard & Jumper Wires | 3× Male-to-Male jumpers | 1 | $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.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling SG90 9g Micro Servo (180° Positional) acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Identify SG90 Wire Color Code
Confirm signal, power, and ground wire colors before making breadboard connections.
- 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.
Wire SG90 & Bulk Decoupling Capacitor
Connect signal to D9 and place the 470µF cap across the breadboard 5V and GND rails.
- 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.
Verify Safe Mechanical Travel Range
Confirm the horn has clear space to rotate 180° without striking wires or the desk.
- 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.
Upload Sweep & Interactive Angle Controller
Flash the firmware to perform an initial calibration sweep, then accept live Serial angles.
- 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.
Verify Zero Jitter & Smooth Slew Rate
Listen for buzzing at endpoints and tune safe software limits.
- 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.
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 DiagnosticMinimal standalone sketch to verify pin wiring and signal response for SG90 9g Micro Servo (180° Positional) before running the full project firmware.
// Diagnostic: Servo 90-Degree Centering Check
#include <Servo.h>
Servo s;
void setup() { s.attach(9); s.write(90); }
void loop() {}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* 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
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Command 10° | Pulse ≈ 650µs | Left Mechanical Safe Limit | Slew CCW to 10° |
| Command 90° | Pulse ≈ 1500µs | Neutral Center | Slew to 90° Home |
| Command 170° | Pulse ≈ 2300µs | Right Mechanical Safe Limit | Slew CW to 170° |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Arduino Uno resets whenever servo starts moving | High startup surge dipping USB 5V rail | Observe Uno power LED flickering during motion | Add 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-stop | Command 15° and 165° to see if buzzing stops | Keep travel inside 10°–170° or calibrate attach(pin, minUs, maxUs). |
| Servo twitches erratically | Missing common ground between external supply and Uno | Verify continuity between external GND and Uno GND | Connect external PSU negative rail directly to Arduino GND. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Mount Project 7 (HC-SR04) on the SG90 horn to scan 15°–165° and log angle:distance pairs.
Map analogRead(A0) directly to servo angle with exponential smoothing.
Pair with RFID (Project 9) to lift a miniature toll gate for 4 seconds on valid tag scan.