In this tutorial, I demonstrate how to connect and program literally any servo motor using an Arduino. Whether you are using a tiny, popular SG90 or a powerful motor with 30 kg of torque, this guide covers both. We will look at safe powering methods, wiring configurations, how servos work under the hood using Pulse Width Modulation (PWM), and two essential code scripts: a basic sweep script and an analog potentiometer knob control.
In the accompanying video, I walk you through:
- Safe Powering: Why direct connection is risky and how to wire an external power supply.
- Wire Color Codes: SG90 vs. other manufacturers.
- PWM Theory: Understanding pulse width modulation.
- Sweep Script: Moving the servo up and down.
- Knob Control: Mapping a potentiometer dial to servo angles.
- Capacitor Trick: Reducing noise and stabilizing power.
The most important topic when working with motors is power:
⚠️ I always recommend using an external power supply for your servos. Motors (especially in stall/load mode) can draw a very large amount of current.Even tiny servos like the SG90 can draw 0.5A to 0.8A in stall mode. If you power them directly from the Arduino board, this high current can cause voltage drops, causing the Arduino to twitch and suddenly reset. It can even restart your computer’s USB port or laptop, especially with older USB 2.0 ports that are limited to 0.5A.
For larger servos, or to make your setup safe and stable, connect the servo’s power directly to an external power supply (typically 5V or 6V depending on the motor specs) and only share the ground (GND) pin with the Arduino.
Wiring Guide1. The Risky Direct Method (Prototyping Only)Use at your own risk for a single small, unloaded SG90:
- VCC → Arduino 5V
- GND → Arduino GND
- Signal → Arduino D9 (or any digital pin)
Connects the servo to an external power supply:
- Servo Power → External Power 5V
- Servo GND → External Power GND and Arduino GND (Common Ground)
- Servo Signal → Arduino D9 (or any digital pin)
💡 To stabilize the power supply and reduce electrical noise that causes twitching, place a capacitor (e.g. 100µF or larger) between the servo’s ground and power rails.How it works: Pulse Width Modulation (PWM)
You might think that the Arduino sends a command like “go to 90 degrees” over the wire. It doesn’t. It sends a heartbeat signal called PWM (Pulse Width Modulation).
Microcontrollers can only output digital signals: either high (3.3V/5V) or low (0V). PWM is a technique that fakes an analog result by switching the switch on and off extremely fast. In the servo domain, the width of the pulse tells the servo’s internal gears and controller where to point.
Fortunately, the Arduino Servo library hides all these timing details.
More details, code snippets, and hardware demonstrations in the video!









Comments