How to Connect a Micro Servo Motor to Arduino Nano

How to Connect a Micro Servo Motor to Arduino / Visits:6

Micro servo motors are the unsung heroes of countless DIY robotics, animatronics, and automation projects. Their compact size, low power consumption, and precise angular control make them the go-to choice for everything from robotic arms to camera gimbals to tiny door locks. And when paired with the equally compact Arduino Nano, you have a combination that punches far above its weight class.

But if you’re new to the world of servos—or even if you’ve used them before with an Uno—the Nano’s smaller footprint and pin layout can throw you off. Fear not. This guide will walk you through the entire process, from understanding what makes a micro servo tick, to wiring it up correctly, to writing code that makes it dance. By the end, you’ll be able to integrate a micro servo into any Nano-based project with confidence.


What Exactly Is a Micro Servo Motor?

Before we plug anything in, let’s talk about the star of the show. A micro servo motor is a small, closed-loop actuator that can rotate its output shaft to a specific angular position—typically between 0° and 180°—and hold that position against moderate external forces.

The “micro” in its name refers to its size: most micro servos (like the ubiquitous SG90 or MG90S) measure about 23 x 12 x 29 mm and weigh less than 10 grams. Despite their tiny stature, they can deliver 1.5 to 2.5 kg·cm of torque, which is more than enough for lightweight mechanisms.

Inside the Servo: The Magic of Feedback

A micro servo isn’t just a motor with a gearbox. It’s a complete control system in a plastic case. Inside you’ll find:

  • A DC motor – The prime mover.
  • A gear train – Reduces speed and increases torque.
  • A potentiometer – Attached to the output shaft, it provides real-time position feedback.
  • A control circuit – Compares the desired position (from the signal wire) with the actual position (from the potentiometer) and drives the motor accordingly.

When you send a pulse-width modulation (PWM) signal down the signal wire, the control circuit continuously adjusts the motor until the potentiometer voltage matches the pulse width. This is why servos are so reliable: they actively correct for disturbances.

Standard vs. Continuous Rotation Servos

Most micro servos you’ll buy are standard (positional) servos. But be aware that continuous rotation servos exist too. A continuous rotation servo ignores the position feedback and instead uses the PWM signal to control speed and direction. For this guide, we’ll focus on standard positional servos, as they are far more common for beginners and most maker projects.


Why Pair a Micro Servo with an Arduino Nano?

The Arduino Nano is a perfect partner for a micro servo. Here’s why:

  • Size compatibility – Both are small. You can fit them into a tiny enclosure or mount them directly on a robot chassis.
  • 5V operating voltage – The Nano runs at 5V, and most micro servos are designed for 4.8V to 6V input. No level shifting needed.
  • Built-in PWM support – The Nano has multiple pins capable of generating the 50 Hz PWM signal that servos require.
  • Low current draw – A single micro servo draws around 100–200 mA when moving, and the Nano’s onboard regulator can handle that (with caution).

However, there’s a critical caveat: the Nano’s 5V regulator is not a powerhouse. If you plan to run two or more servos simultaneously, or if your servo will be under heavy load, you must use an external power supply. We’ll cover that in the wiring section.


What You’ll Need: The Shopping List

Here’s a complete list of components for this tutorial. Most are cheap and widely available.

  • Arduino Nano (the classic ATmega328P version, or a Nano Every)
  • Micro servo motor (SG90, MG90S, or similar)
  • Breadboard and jumper wires (male-to-female and male-to-male)
  • External power source (optional but recommended) – 4x AA battery holder (6V) or a 5V/2A wall adapter
  • 100 µF electrolytic capacitor (optional but highly recommended) – To smooth out power spikes
  • USB cable (Mini-B for classic Nano, Micro-USB for Nano v3, USB-C for Nano Every)
  • Computer with Arduino IDE installed

Understanding the Servo’s Three Wires

A micro servo has three wires. Their colors may vary depending on the manufacturer, but the most common standard is:

| Wire Color | Function | Connect To | |------------|----------|------------| | Brown (or Black) | Ground (GND) | Arduino GND | | Red (or Orange) | Power (VCC) | 5V (or external 5-6V) | | Yellow (or White) | Signal (PWM) | Arduino digital pin (e.g., D9) |

Important: Never connect the red wire directly to the Arduino’s 5V pin if you are powering the servo from an external source. You’ll create a ground loop or worse. We’ll separate the power paths in the wiring section.

The Signal Wire: How PWM Controls Position

The signal wire carries a 50 Hz PWM signal (period = 20 ms). The pulse width within that 20 ms window determines the angle:

  • 1 ms pulse → 0° (full left)
  • 1.5 ms pulse → 90° (center)
  • 2 ms pulse → 180° (full right)

The Arduino’s Servo library handles this timing automatically. You simply write an angle from 0 to 180, and the library generates the correct pulse width.


Wiring the Micro Servo to Arduino Nano

Now for the hands-on part. We’ll cover two scenarios: powering the servo from the Arduino (for light loads) and powering it externally (for reliable operation).

Scenario A: Powering the Servo from the Arduino’s 5V Pin

This is the simplest setup, but it’s only safe for a single micro servo that isn’t under heavy load. The Nano’s onboard regulator can supply about 500 mA total (including the board itself), and a typical micro servo draws 150–250 mA when moving. You’re fine as long as you don’t stall the servo.

Wiring Steps:

  1. Insert the Arduino Nano into the breadboard.
  2. Connect the servo’s brown wire to a GND pin on the Nano.
  3. Connect the servo’s red wire to the 5V pin on the Nano.
  4. Connect the servo’s yellow wire to digital pin D9 (or any PWM-capable pin: D3, D5, D6, D9, D10, D11).

Add a Capacitor (Highly Recommended):
Place a 100 µF electrolytic capacitor between the 5V and GND rails, as close to the servo connector as possible. The positive leg goes to 5V, the negative to GND. This absorbs voltage spikes when the servo starts or stops, preventing the Arduino from resetting unexpectedly.

Scenario B: Powering the Servo from an External Source

If you’re using two or more servos, or if your servo will be lifting something heavy, you must use an external power supply. The Nano will only provide the signal, not the current.

Wiring Steps:

  1. Connect the servo’s brown wire to the negative terminal of your external power supply.
  2. Connect the servo’s red wire to the positive terminal of the external power supply.
  3. Connect the servo’s yellow wire to digital pin D9 on the Nano.
  4. Crucially: Connect the GND of the external power supply to a GND pin on the Arduino Nano. This creates a common ground reference. Without it, the signal from the Nano will be meaningless to the servo.

Why the Common Ground is Mandatory:
The servo’s control circuit reads the signal voltage relative to its own ground. If the Arduino’s ground and the servo’s ground are not tied together, the signal will float, and the servo will behave erratically or not at all.

Wiring Diagram (Textual)

External Power (6V Battery Pack) + ----------------- Servo Red Wire - ----------------- Servo Brown Wire ---> Arduino GND (common)

Arduino Nano GND <--- Servo Brown (also connected to external GND) 5V (unused in this scenario) D9 <--- Servo Yellow (signal)


Writing the Arduino Code

With the hardware connected, it’s time to bring the servo to life. The Arduino ecosystem makes this ridiculously easy thanks to the built-in Servo library.

The Basic Sweep Sketch

This classic example moves the servo from 0° to 180° and back in a continuous loop.

cpp

include <Servo.h>

Servo myServo; // Create a servo object

int servoPin = 9; // Signal pin int pos = 0; // Position variable

void setup() { myServo.attach(servoPin); // Attach the servo to the pin }

void loop() { // Sweep from 0 to 180 degrees for (pos = 0; pos <= 180; pos += 1) { myServo.write(pos); delay(15); // Wait 15 ms for servo to reach position }

// Sweep back from 180 to 0 degrees for (pos = 180; pos >= 0; pos -= 1) { myServo.write(pos); delay(15); } }

What’s happening here?

  • #include <Servo.h> loads the library.
  • Servo myServo; creates an object that represents your physical servo.
  • myServo.attach(9); tells the library which pin to use.
  • myServo.write(pos); sets the target angle. The library handles the PWM generation.
  • The delay(15) gives the servo time to physically move to the new position. You can reduce this to 10 ms for faster motion, but too fast and the servo may stutter.

Controlling the Servo with a Potentiometer

A more interactive project is to control the servo angle with a potentiometer (pot). This teaches you how to map analog readings to angles.

Additional Hardware: - 10kΩ potentiometer - Jumper wires

Wiring for the Potentiometer: - Left pin → 5V - Center pin → A0 (analog input) - Right pin → GND

Code:

cpp

include <Servo.h>

Servo myServo; int potPin = A0; int servoPin = 9; int potValue = 0; int angle = 0;

void setup() { myServo.attach(servoPin); Serial.begin(9600); }

void loop() { potValue = analogRead(potPin); // Read 0–1023 angle = map(potValue, 0, 1023, 0, 180); // Map to 0–180 myServo.write(angle);

Serial.print("Pot: "); Serial.print(potValue); Serial.print(" | Angle: "); Serial.println(angle);

delay(15); }

The map() function is your best friend here. It scales the 10-bit ADC reading (0–1023) to the servo’s angular range (0–180). Now you can turn the knob and watch the servo follow your command in real time.


Advanced Techniques and Troubleshooting

Once you have the basics working, you’ll inevitably want to do more. Here are some advanced tips and common pitfalls.

Using Multiple Servos

The Servo library supports up to 12 servos on most Arduino boards. Simply create multiple objects and attach them to different pins.

cpp Servo servo1; Servo servo2;

void setup() { servo1.attach(9); servo2.attach(10); }

void loop() { servo1.write(0); servo2.write(180); delay(1000); servo1.write(180); servo2.write(0); delay(1000); }

Power Warning: With two servos, you almost certainly need an external power supply. Each servo can draw 200 mA under load, and the Nano’s regulator will overheat if you try to draw 400+ mA from its 5V pin.

Smooth Motion with for Loops vs. Direct write()

If you send myServo.write(90) directly, the servo will snap to that position as fast as it can. For smooth, robotic motion, use a for loop that increments the angle slowly. You can also use the writeMicroseconds() function for finer control (e.g., myServo.writeMicroseconds(1500) for center).

Dealing with Jitter or Stuttering

If your servo jitters or moves erratically, check these in order:

  1. Power supply – Is the voltage stable? A cheap USB wall adapter may sag under load. Use a battery pack or a regulated supply.
  2. Capacitor – Add a 100 µF or larger capacitor across the servo’s power pins.
  3. Ground loops – Ensure all grounds (Arduino, servo, external supply) are connected to a single point.
  4. Timer conflicts – The Servo library uses Timer1 on the Nano. If you also use libraries that hijack Timer1 (like Tone), you’ll get conflicts. Use a different pin or avoid those libraries.

Protecting Your Arduino from Back-EMF

When a servo stops abruptly, it can generate a voltage spike (back-EMF) that travels back through the power line. The 100 µF capacitor helps absorb this. For extra protection, add a flyback diode (1N4007) across the servo’s power terminals, with the cathode (striped end) connected to the positive wire.

What About 180° vs. 270° Servos?

Some micro servos (like the MG996R) can rotate 270° or even 360° (continuous). For these, the pulse width range is different. Check the datasheet. You can use myServo.writeMicroseconds(500) to writeMicroseconds(2500) to cover the full range. The standard write(angle) function assumes 0–180°.


Real-World Project Ideas

Now that you know how to connect and control a micro servo with an Arduino Nano, here are some project ideas to put your skills to use.

1. DIY Robot Arm

Use three micro servos to build a simple pick-and-place arm. The Nano can control each joint sequentially. Add a joystick or potentiometer for manual control.

2. Animatronic Eyes

Mount two micro servos behind a pair of plastic eyes. One servo controls left-right movement, the other controls up-down. Program random saccades for a lifelike effect.

3. Automatic Plant Waterer

Attach a micro servo to a small valve or a syringe. Use a soil moisture sensor (analog input) to trigger the servo when the soil is dry. The Nano runs on battery power for days.

4. Camera Pan-Tilt Mount

Use two servos to create a pan-tilt mechanism for a small camera (like an ESP32-CAM). The Nano sends angle commands based on a joystick or motion sensor input.

5. Tiny Door Lock

Mount a micro servo inside a 3D-printed box. The servo rotates a latch to lock or unlock a drawer. Use a keypad or RFID reader to trigger the servo.


Common Mistakes and How to Avoid Them

Even experienced makers make these errors. Here’s a quick checklist:

  • Forgetting the common ground – The number one cause of “servo not moving.” Always tie the external power supply’s ground to the Arduino’s ground.
  • Powering the servo from the Arduino’s 3.3V pin – Do not do this. The servo needs 5V. The 3.3V pin cannot supply enough current, and the servo will stall or behave unpredictably.
  • Using a non-PWM pin – The Servo library works on any pin, but for best results, use hardware PWM pins (D3, D5, D6, D9, D10, D11). On the Nano, D9 and D10 are the most reliable.
  • Overloading the servo – If the servo buzzes but doesn’t move, it’s likely stalled. Reduce the mechanical load or use a higher-torque servo.
  • Ignoring the datasheet – Not all micro servos are the same. The SG90 has plastic gears, the MG90S has metal gears. The pulse range may differ. Always check the specific model.

Final Code: A Full-Featured Test Sketch

Here’s a comprehensive sketch that tests your servo in multiple modes. Upload it to verify your wiring works perfectly.

cpp

include <Servo.h>

Servo myServo; int servoPin = 9;

void setup() { Serial.begin(9600); myServo.attach(servoPin); Serial.println("Servo Test Starting..."); }

void loop() { // Test 1: Snap to positions Serial.println("Snap to 0°"); myServo.write(0); delay(1000);

Serial.println("Snap to 90°"); myServo.write(90); delay(1000);

Serial.println("Snap to 180°"); myServo.write(180); delay(1000);

// Test 2: Sweep slowly Serial.println("Sweep from 0 to 180"); for (int i = 0; i <= 180; i++) { myServo.write(i); delay(10); }

Serial.println("Sweep from 180 to 0"); for (int i = 180; i >= 0; i--) { myServo.write(i); delay(10); }

// Test 3: Center and detach Serial.println("Center at 90° and detach"); myServo.write(90); delay(500); myServo.detach(); // Release the pin delay(2000); myServo.attach(servoPin); // Re-attach }

If your servo goes through all three tests without stuttering or freezing, your wiring and power supply are solid.


Next Steps: Beyond the Basics

You’ve now mastered the fundamental connection and control of a micro servo with an Arduino Nano. From here, you can branch out into:

  • I2C servo controllers (PCA9685) for controlling 16 servos with just two wires.
  • Feedback servos that report their actual position back to the Arduino.
  • Closed-loop PID control for precision tasks like camera stabilization.
  • Wireless control using Bluetooth (HC-05) or Wi-Fi (ESP8266) to command your servo from a smartphone.

The micro servo and Arduino Nano combination is a gateway to countless interactive projects. Whether you’re building a robotic pet, a mechanical clock, or an automatic cat feeder, the principles you’ve learned here will serve you well.

So go ahead—grab a servo, a Nano, and a breadboard. Wire it up, upload the code, and watch that tiny motor come to life. The only limit is your imagination.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/how-to-connect-a-micro-servo-motor-to-arduino/connect-micro-servo-arduino-nano.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags