How to Connect a Micro Servo Motor to Arduino Due

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

Micro servo motors are the unsung heroes of countless maker projects. From robotic arms to camera gimbals, from animatronic eyes to automatic pet feeders, these tiny but powerful actuators bring motion to life. The Arduino Due, with its blazing 84 MHz ARM Cortex-M3 processor and 12-bit PWM resolution, offers a unique platform for driving micro servos with exceptional precision—far beyond what typical 8-bit boards can achieve.

In this guide, I’ll walk you through everything you need to know about connecting a micro servo motor to an Arduino Due. We’ll cover hardware wiring, power considerations, software setup, and advanced techniques like smooth motion control and multi-servo synchronization. By the end, you’ll have a rock-solid foundation for building projects that require precise, repeatable angular positioning.

Why Micro Servo Motors and Arduino Due Are a Perfect Match

Before diving into the wiring, let’s understand why this combination is so compelling.

Micro servo motors (like the ubiquitous SG90 or MG90S) operate on a standard 50 Hz PWM signal with a pulse width between 1 ms and 2 ms. The Arduino Due’s native PWM outputs can generate these signals with 12-bit resolution (0–4095), compared to the 8-bit (0–255) resolution of Uno or Mega boards. This means you can command your servo to move to any of 4096 discrete positions instead of just 256—a 16x improvement in granularity.

Additionally, the Due’s high clock speed allows you to run multiple servos simultaneously without jitter, and its 3.3V logic level (versus the 5V logic on classic Arduinos) introduces some interesting considerations we’ll address later.

Key Specifications at a Glance

| Component | Key Specs | Impact on Servo Control | |-----------|-----------|-------------------------| | Arduino Due | 3.3V logic, 84 MHz, 12-bit PWM | Higher precision, faster updates, but needs voltage translation for 5V servos | | Micro Servo (e.g., SG90) | 4.8–6V operating, 50 Hz signal, 180° range | Standard hobby servo, draws 100–700 mA under load | | Power Source | 5V external supply recommended | Avoid powering servos from Due’s 3.3V rail |

Hardware Wiring: The Right Way to Connect

Connecting a micro servo to the Due is straightforward, but there are critical details that, if overlooked, can damage your board or cause erratic behavior.

Pin Identification on a Micro Servo

Most micro servos come with a three-wire cable:

  • Brown or Black wire → Ground (GND)
  • Red wire → Power (VCC, typically 5V)
  • Orange or Yellow wire → Signal (PWM input)

Always double-check the color coding on your specific servo. Some manufacturers use different conventions (e.g., white for signal, red for power, black for ground).

The Wiring Diagram

Here’s the correct connection for a single micro servo:

Servo Wire Arduino Due Pin ---------------- ----------------- Brown (GND) → GND (any) Red (VCC) → External 5V power supply (NOT from Due’s 5V pin) Orange (Signal)→ Digital Pin 9 (or any PWM-capable pin)

Critical Warning: Do not power the servo from the Arduino Due’s 5V pin. The Due’s on-board voltage regulator can only supply about 800 mA, and a single micro servo under load can draw 500–700 mA. Multiple servos will exceed this limit instantly, causing the Due to reset or, worse, damage the regulator. Always use an external 5V power supply (a 2A wall adapter works well).

Shared Ground Is Mandatory

Connect the negative terminal of your external power supply to the Due’s GND. This creates a common reference voltage, ensuring the PWM signal from the Due is interpreted correctly by the servo. Without this, the servo may twitch, jitter, or refuse to move.

Signal Level: A Special Note for the Due

The Arduino Due runs at 3.3V logic. Most micro servos are designed for 5V logic levels, but they typically accept 3.3V signals without issue. The threshold for a “high” signal on a standard servo is around 2.5V, so the Due’s 3.3V output is sufficient.

However, if you’re using servos with stricter logic requirements (some industrial or high-voltage servos), you may need a level shifter. For 99% of hobby micro servos (SG90, MG90S, MG996R), direct connection works fine.

Power Supply Considerations for Multiple Servos

If you’re driving more than one or two servos, power becomes the #1 source of problems. Here’s how to plan your power architecture.

Calculating Total Current Draw

  • Each micro servo at idle: ~10 mA
  • Each micro servo moving with no load: ~100–200 mA
  • Each micro servo under stall load: ~700–800 mA

For a 6-servo robot arm, worst-case current could be 4.8A. A 5V/5A power supply is a safe choice.

Power Distribution Options

  1. Breadboard with thick jumper wires – Fine for 1–2 servos
  2. Servo driver board (PCA9685) – Handles up to 16 servos with I2C control and separate power input
  3. Custom power distribution board – For permanent installations, use screw terminals and 16 AWG wire

Decoupling Capacitors

Place a 470 µF electrolytic capacitor between the servo power and ground, as close to the servo connector as possible. This smooths out current spikes when the servo starts moving, preventing voltage dips that can cause the Due to reset. A 100 nF ceramic capacitor in parallel helps with high-frequency noise.

Software Setup: From Basic Sweep to Precision Control

Now that the hardware is connected, let’s write the code. We’ll start with the classic “Sweep” example, then move to precise angle control with the Due’s 12-bit PWM.

Installing the Servo Library

The Arduino IDE includes a built-in Servo library. However, on the Due, this library has a limitation: it uses 8-bit resolution internally, which defeats the purpose of the Due’s 12-bit PWM. For true precision, we’ll write our own PWM control using direct register manipulation.

But first, let’s get something working quickly with the standard library.

Basic Sweep Example (Using Standard Servo Library)

cpp

include <Servo.h>

Servo myServo;

void setup() { myServo.attach(9); // Signal wire on pin 9 }

void loop() { for (int angle = 0; angle <= 180; angle++) { myServo.write(angle); delay(15); // Allow time to reach position } for (int angle = 180; angle >= 0; angle--) { myServo.write(angle); delay(15); } }

This works, but the Due’s Servo library uses a 1 ms to 2 ms pulse range mapped to 0–180 degrees with only 256 steps. For most applications, this is adequate. But if you need finer control, read on.

High-Resolution Servo Control with the Due’s PWM

The Arduino Due has dedicated PWM hardware on pins 2–13. We can configure the PWM controller directly to generate a 50 Hz signal with 12-bit duty cycle resolution.

Here’s a custom function that gives you 4096 steps for your servo:

cpp // High-resolution servo control for Arduino Due // Pin 9 is PWM channel 0 (PWM0)

void setup() { // Enable PWM clock and configure pin 9 PMC->PMCPCER1 |= PMCPCER1PID36; // Enable PWM peripheral clock PIOC->PIOABSR |= PIOABSRP9; // Set pin 9 to peripheral B (PWM0) PIOC->PIOPDR |= PIOPDR_P9; // Disable PIO control

// Configure PWM channel 0 PWM->PWMCLK = PWMCLKPREA(0) | PWMCLKDIVA(84); // MCK/84 = 1 MHz clock PWM->PWMCH0 = PWMCMRCPRECLKA; // Use clock A PWM->PWMCH0 = PWMCMRCPRE_MCK; // Actually use master clock (84 MHz)

// Set period for 50 Hz: 84 MHz / 20000 = 4200 PWM->PWMCH0PDR = 20000; // Period register (20 ms) PWM->PWMCH0CDTY = 1500; // Duty cycle (1.5 ms = 90°)

// Enable PWM channel 0 PWM->PWMENA = PWMENA_CHID0; }

void setServoAngle(int angle) { // Map 0–180° to pulse width 1000–2000 µs // With period = 20000, duty = (pulseus * 20000) / 20000 // Actually: duty = pulseus (since period is in microseconds) int pulseus = map(angle, 0, 180, 1000, 2000); PWM->PWMCH0CDTY = pulseus; // Set duty cycle }

void loop() { for (int i = 0; i <= 180; i++) { setServoAngle(i); delay(20); } for (int i = 180; i >= 0; i--) { setServoAngle(i); delay(20); } }

Note: The register-level code above is simplified. In practice, you’d want to calculate the clock divider and period more carefully. The key takeaway is that the Due can generate PWM with 12-bit precision, giving you 4096 possible positions instead of 256.

Smooth Motion: Easing Functions for Natural Movement

Abrupt servo movements look robotic and can cause mechanical stress. Adding easing (acceleration and deceleration) makes motions appear fluid and natural.

Simple Linear Interpolation

cpp void smoothMove(Servo &servo, int startAngle, int endAngle, int durationms) { int steps = 50; // Number of intermediate positions int delayms = duration_ms / steps;

for (int i = 0; i <= steps; i++) { float t = (float)i / steps; int angle = startAngle + (endAngle - startAngle) * t; servo.write(angle); delay(delay_ms); } }

Ease-In-Out Cubic

For even smoother motion, use a cubic easing function:

cpp float easeInOutCubic(float t) { if (t < 0.5) return 4 * t * t * t; else return 1 - pow(-2 * t + 2, 3) / 2; }

void smoothMoveEased(Servo &servo, int startAngle, int endAngle, int durationms) { int steps = 100; int delayms = duration_ms / steps;

for (int i = 0; i <= steps; i++) { float t = (float)i / steps; float easedT = easeInOutCubic(t); int angle = startAngle + (endAngle - startAngle) * easedT; servo.write(angle); delay(delay_ms); } }

This creates motion that accelerates at the start and decelerates at the end—much more pleasing to watch and gentler on the servo gears.

Multi-Servo Synchronization: Moving in Unison

When controlling multiple servos, you want them to start and stop moving at the same time, even if they have different distances to travel.

The Problem with Sequential Control

If you write servo1.write(180); delay(1000); servo2.write(90); delay(1000);, servo2 will only start moving after servo1 has completed its full travel. This creates a “wave” effect that looks uncoordinated.

Time-Based Synchronization

Instead of sequential delays, use a single timer that updates all servos simultaneously:

cpp

include <Servo.h>

Servo servo1, servo2, servo3; int target1 = 0, target2 = 0, target3 = 0; int current1 = 0, current2 = 0, current3 = 0; unsigned long lastUpdate = 0; const int speed = 1; // Degrees per update

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

void loop() { unsigned long now = millis(); if (now - lastUpdate >= 20) { // Update every 20 ms (50 Hz) lastUpdate = now;

// Move each servo toward its target if (current1 < target1) current1 += speed; if (current1 > target1) current1 -= speed; if (current2 < target2) current2 += speed; if (current2 > target2) current2 -= speed; if (current3 < target3) current3 += speed; if (current3 > target3) current3 -= speed;  servo1.write(current1); servo2.write(current2); servo3.write(current3); 

}

// Change targets periodically static unsigned long lastChange = 0; if (now - lastChange > 3000) { lastChange = now; target1 = random(0, 181); target2 = random(0, 181); target3 = random(0, 181); } }

All three servos now move simultaneously toward their targets at the same speed. For different speeds, use different speed values per servo.

Troubleshooting Common Issues

Even with correct wiring, you may encounter problems. Here’s how to diagnose and fix them.

Servo Twitches or Jitters

  • Cause: Power supply noise or insufficient current.
  • Fix: Add a 470 µF capacitor across servo power and ground. Use a regulated 5V supply rated for at least 2A.

Servo Does Not Move at All

  • Cause: Wrong pin, bad connection, or dead servo.
  • Fix: Check wiring. Use a multimeter to verify 5V between red and brown wires. Try a different PWM pin. Test the servo with a known-good Arduino Uno.

Servo Moves Only Partially

  • Cause: Incorrect pulse width range. Some servos expect 0.5–2.5 ms instead of 1–2 ms.
  • Fix: Adjust the map() function. Use servo.writeMicroseconds() for direct pulse control.

Due Resets When Servo Moves

  • Cause: Voltage drop due to servo current spike.
  • Fix: Use a separate power supply for the servo. Ensure the ground is shared. Add a large capacitor (1000 µF) near the servo power input.

Advanced: Using the Due’s DAC for Analog Servo Control

The Arduino Due has two true analog outputs (DAC0 and DAC1) that can output 0–3.3V with 12-bit resolution. Some analog servos accept a voltage signal instead of PWM. While rare in the micro servo world, this is worth knowing for specialized applications.

cpp void setup() { analogWriteResolution(12); // Set DAC to 12-bit }

void loop() { // Map angle to DAC output (0–4095, 0–3.3V) int dacValue = map(angle, 0, 180, 0, 4095); analogWrite(DAC0, dacValue); delay(20); }

Note: Most hobby micro servos are PWM-controlled, not analog voltage-controlled. Check your servo’s datasheet before attempting this.

Project Ideas to Get You Started

Now that you know how to connect and control micro servos with the Due, here are some projects that showcase their potential:

  1. Pan-Tilt Camera Mount – Use two servos to aim a camera. The Due’s high resolution allows smooth, precise tracking.
  2. Robotic Finger – A single servo with a linkage can mimic a human finger. Add multiple servos for a hand.
  3. Automatic Plant Waterer – A servo rotates a valve or moves a watering arm. Add a soil moisture sensor for automation.
  4. Servo-Based Clock – Arrange servos in a circle with hands. The Due’s real-time clock (RTC) keeps accurate time.
  5. Wave Simulator – Array of servos with fabric or paper to simulate ocean waves. Synchronized motion is key.

Final Wiring Checklist

Before powering on your system, verify each step:

  • [ ] Servo ground connected to Due GND
  • [ ] Servo power connected to external 5V supply (not Due’s 5V)
  • [ ] External supply negative connected to Due GND
  • [ ] 470 µF capacitor between servo power and ground
  • [ ] Signal wire connected to a PWM-capable Due pin (2–13)
  • [ ] Power supply rated for total servo current + margin
  • [ ] Code uploaded and tested with a simple sweep first

Parting Thoughts on Micro Servo Mastery

The combination of micro servo motors and the Arduino Due opens up possibilities that are simply not achievable with 8-bit boards. The 12-bit PWM resolution, combined with the Due’s processing power, allows you to create motion that is smooth, precise, and responsive. Whether you’re building a robotic sculpture, a precision instrument, or just experimenting with motion, this setup gives you the tools to do it right.

Remember: power is everything. The most sophisticated code in the world won’t help if your servo browns out on every move. Invest in a good power supply, add decoupling capacitors, and always share ground. With those fundamentals in place, you can focus on the creative part—bringing your projects to life, one degree at a time.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/how-to-connect-a-micro-servo-motor-to-arduino/connect-micro-servo-arduino-due.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