Arduino MKR WiFi 1010 - Servo Motor

Control precise angular motion with servo motors and your Arduino MKR WiFi 1010! Servo motors provide accurate position control from 0° to 180°, perfect for robotic arms, camera gimbals, steering mechanisms, and any project requiring controlled rotational movement. Learn to command exact angles with simple Arduino code.

What You'll Learn:

Real-World Applications:

Arduino MKR WiFi 1010 Servo Motor

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×Servo Motor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: For controlling multiple servo motors, use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify wiring.

Overview of Servo Motor

A servo motor is a precision rotary actuator that can accurately move to and hold a specific angular position. Unlike continuous rotation motors (DC motors), standard hobby servos rotate through a limited range—typically 0° to 180°—making them perfect for precise positioning tasks.

What Makes Servos Special:

  • Built-in feedback control: Internal potentiometer measures shaft position
  • Self-correcting: Automatically adjusts to maintain commanded angle
  • Precise positioning: Can hold a specific angle with high accuracy
  • Easy control: Single PWM signal controls position (no complex drivers needed)
  • Integrated gearbox: High torque at low speeds
  • Compact design: Power, ground, and signal in one package

Why Use Servo Motors?

  • Position control: Need exact angles, not just speed
  • Holding torque: Maintains position against external forces
  • Simple interface: Arduino Servo library handles PWM timing
  • Wide availability: Standard hobby servos are cheap and reliable
  • Immediate response: Moves to commanded position quickly

Common Servo Types:

  • Standard (180°): Most common, rotates 0-180° (this tutorial)
  • Continuous rotation: Modified servos that spin continuously (like DC motors)
  • Digital servos: Higher precision and faster response (more expensive)
  • High-torque servos: Stronger motors for heavy loads

Typical Specifications:

  • Rotation range: 0° to 180° (standard), some go to 270°
  • Torque: 1-10 kg·cm (larger = stronger)
  • Speed: 0.1-0.2 sec/60° (how fast it rotates)
  • Voltage: 4.8V-6V (5V is standard)
  • Current: 100mA-2A (depends on load)

Servo Motor Pinout

Servo motors have a standardized three-wire connection with color-coded wires:

Servo Motor Pinout

Pin 1 - GND (Ground):

  • Wire color: Brown or black
  • Connection: Connect to Arduino MKR WiFi 1010 GND pin
  • Purpose: Completes the electrical circuit (0V reference)

Pin 2 - VCC (Power Supply):

  • Wire color: Red
  • Connection: Connect to external 5V power supply (recommended for multiple/high-torque servos)
  • Alternative: Arduino 5V pin for small servos (with caution—see power notes below)
  • Voltage: Typically 4.8V-6V (5V is standard)
  • Current: 100mA-2A depending on servo size and load

Pin 3 - Signal (PWM Control):

  • Wire color: Yellow, orange, or white
  • Connection: Connect to Arduino MKR WiFi 1010 digital pin (any pin works, commonly D9)
  • Purpose: Receives PWM control signal to set servo angle
  • Signal type: 50Hz PWM with pulse width 1000-2000µs

Wire Color Variations:

Different manufacturers use slightly different color schemes:

Manufacturer GND VCC Signal
Standard Brown Red Orange/Yellow
Hitec Black Red Yellow
Futaba Black Red White

Important Note: While wire colors vary, the physical pin order on the connector is always standardized (GND, VCC, Signal from left to right when viewing the connector tab-side up).

How Servo Motor Works

Servo motors use a closed-loop feedback system to achieve precise position control. Understanding this mechanism helps you use them effectively.

Internal Components:

  1. DC Motor: Provides rotational power
  2. Gearbox: Reduces speed and increases torque (typically 50:1 to 300:1 ratio)
  3. Potentiometer: Measures actual shaft angle (feedback sensor)
  4. Control Circuit: Compares commanded position with actual position
  5. Housing: Protects internals and provides mounting points

PWM Control Signal:

Servos are controlled by a 50Hz PWM signal (pulse every 20ms). The pulse width determines the angle:

Pulse Width Servo Angle Typical Use
1000µs (1ms) Fully counterclockwise
1500µs (1.5ms) 90° Center position
2000µs (2ms) 180° Fully clockwise
1250µs 45° Quarter rotation
1750µs 135° Three-quarter rotation

How Position Control Works:

  1. Arduino sends PWM signal with specific pulse width
  2. Servo's control circuit decodes pulse width into target angle
  3. Internal potentiometer measures current shaft angle
  4. If current angle ≠ target angle, motor rotates
  5. As shaft moves, potentiometer value changes
  6. When current angle = target angle, motor stops
  7. Servo actively holds position (resists external forces)

Key Characteristics:

  • Proportional control: Pulse width directly maps to angle
  • Active holding: Servo applies torque to maintain position
  • Self-correcting: If you manually push the shaft, servo fights back to return to commanded angle
  • No calibration needed: Works immediately with standard pulse widths

Arduino Servo Library:

The Arduino Servo library handles all PWM timing automatically:

Servo myServo; myServo.attach(14); // Connect to pin 14 myServo.write(90); // Move to 90° (center)

For a deeper dive into servo mechanics, see: How servo motor works

Wiring Diagram between Servo Motor and Arduino MKR WiFi 1010

Configuration 1: USB Power (Small Servos Only)

The wiring diagram between Arduino MKR WiFi 1010 Servo Motor

This image is created using Fritzing. Click to enlarge image

Connections:

  • Servo GND (brown/black) → Arduino GND
  • Servo VCC (red) → External 5V power supply (+)
  • Servo Signal (yellow/orange) → Arduino digital pin (e.g., D9)
  • External power GND → Arduino GND (common ground essential!)

Use this when:

  • Testing with one small servo (SG90, 9g micro servo)
  • Servo draws <500mA current
  • No heavy loads on servo

Configuration 2: External Power Supply (Recommended)

The wiring diagram between Arduino MKR WiFi 1010 servo motor external power supply

This image is created using Fritzing. Click to enlarge image

Connections:

  • Servo GND → External 5V power supply (-)
  • Servo VCC → External 5V power supply (+)
  • Servo Signal → Arduino digital pin (e.g., D9)
  • External power GND → Arduino GND (critical: common ground!)
  • Arduino powered separately via USB or Vin

Use this when:

  • Using multiple servos (2 or more)
  • Using high-torque servos (MG996R, standard-size servos)
  • Servos under heavy load
  • Professional/reliable projects

※ NOTE THAT:

CRITICAL POWER SAFETY:

⚠ WARNING - Board Damage Risk:

  • NEVER power servos from Vin or VBUS pins when Arduino is USB-powered!
  • USB ports provide limited current (~500mA max)
  • Servos can draw 1-2A, especially under load or during rapid movements
  • Exceeding current limit can:
    • Damage your computer's USB port
    • Burn out Arduino voltage regulator
    • Cause Arduino to reset randomly
    • Permanently damage the board

    Safe Power Options:

    1. External 5V supply (battery pack, wall adapter) → Safest for servos
    2. Arduino 5V pin → Only for tiny servos (SG90), no load, testing only
    3. Common ground mandatory → Always connect power supply GND to Arduino GND

    Current Requirements:

    • SG90 micro servo: 100-250mA (can work from Arduino temporarily)
    • Standard servo (MG996R): 500mA-2A (needs external power)
    • Multiple servos: Add currents, use external supply

    Recommended External Supply:

    • 4× AA battery pack (6V) with 5V regulator
    • USB power bank (5V, 2A+)
    • 5V wall adapter (2A minimum)

Arduino MKR WiFi 1010 Code

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-servo-motor */ #include <Servo.h> #define SERVO_PIN 14 // Arduino MKR WiFi 1010 pin connected to servo motor Servo servoMotor; void setup() { servoMotor.attach(SERVO_PIN); // attaches the servo on Arduino Nano 33 IoT pin } void loop() { // rotates from 0 degrees to 180 degrees for (int pos = 0; pos <= 180; pos += 1) { // in steps of 1 degree servoMotor.write(pos); delay(15); // waits 15ms to reach the position } // rotates from 180 degrees to 0 degrees for (int pos = 180; pos >= 0; pos -= 1) { servoMotor.write(pos); delay(15); // waits 15ms to reach the position } }

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

Step 1: Wire the Hardware

  • Connect servo motor to Arduino MKR WiFi 1010 as shown in wiring diagram above
  • Use external 5V power supply for servo VCC (recommended)
  • Connect servo Signal wire to Arduino digital pin D9
  • Ensure common ground: Connect external power GND to Arduino GND

Step 2: Program the Board

  • Plug your Arduino MKR WiFi 1010 into your computer's USB port
  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and its COM port
  • Copy the code above and paste it into the Arduino IDE
  • Click the Upload button to compile and upload the code
How to upload Arduino MKR WiFi 1010 code on Arduino IDE

Step 3: Observe Servo Movement

  • Watch the servo motor sweep smoothly from 0° to 180° (right)
  • Then sweep back from 180° to 0° (left)
  • Movement repeats continuously
  • Notice the smooth incremental motion (not instant jumps)

Troubleshooting:

  • Servo jitters/vibrates: Check power supply, ensure sufficient current
  • Servo doesn't move: Verify signal wire connection, check power
  • Random resets: Servo drawing too much current—use external power
  • Servo moves wrong direction: Normal variation—adjust code angles

Code Explanation

Let's break down how the Arduino MKR WiFi 1010 controls the servo motor:

Library and Object Creation:

#include <Servo.h> Servo myServo;
  • Includes Arduino Servo library (handles PWM timing)
  • Creates servo object named myServo
  • Can create multiple servo objects for multiple servos

Setup - Attach Servo:

void setup() { myServo.attach(14); // Connect servo signal to pin 14 }
  • attach(pin) configures pin 14 to output servo PWM signals
  • Arduino now sends 50Hz PWM pulses to control servo position
  • Pin can be any digital pin on Arduino MKR WiFi 1010

Loop - Sweep Right (0° to 180°):

for (int angle = 0; angle <= 180; angle++) { myServo.write(angle); delay(15); }
  • Loop increments angle from 0 to 180 (1° per iteration)
  • write(angle) commands servo to move to specified angle
  • delay(15) creates 15ms pause between steps
  • Total sweep time: 180 steps × 15ms = 2.7 seconds
  • Creates smooth, slow motion

Loop - Sweep Left (180° to 0°):

for (int angle = 180; angle >= 0; angle--) { myServo.write(angle); delay(15); }
  • Loop decrements angle from 180 back to 0
  • Reverses direction smoothly
  • Same timing for symmetrical motion

Key Concepts:

  • write() method: Commands servo to move to angle (0-180)
  • Incremental movement: Small steps create smooth motion
  • Delay controls speed: Smaller delay = faster movement, larger delay = slower
  • Continuous loop: Servo sweeps back and forth endlessly

Servo Library Methods:

attach(pin) // Initialize servo on specified pin write(angle) // Move to angle (0-180°) writeMicroseconds(µs) // Move using pulse width (1000-2000µs) read() // Read last commanded angle attached() // Check if servo is attached detach() // Stop PWM signal (servo relaxes)

How to Control Speed of Servo Motor

Servo motors don't have direct speed control—they move as fast as possible to the commanded position. However, you can create the illusion of speed control by moving in small increments with delays:

Slower Movement (Larger Delay):

for (int angle = 0; angle <= 180; angle++) { myServo.write(angle); delay(50); // 50ms between steps = very slow }

Faster Movement (Smaller Delay):

for (int angle = 0; angle <= 180; angle++) { myServo.write(angle); delay(5); // 5ms between steps = fast }

Instant Movement (No Loop):

myServo.write(180); // Jumps to 180° at maximum speed

Advanced: Acceleration/Deceleration:

for (int angle = 0; angle <= 180; angle++) { myServo.write(angle); int dynamicDelay = map(angle, 0, 180, 50, 5); // Start slow, end fast delay(dynamicDelay); }

For more advanced speed control techniques, see: How to control speed of servo motor

Challenge Yourself

Ready to explore more with your Arduino MKR WiFi 1010 and servo motors? Try these creative experiments:

  1. Potentiometer Control: Connect a potentiometer to analog pin—map its value (0-1023) to servo angle (0-180°). Create a manual servo controller.
  2. Button Positions: Use 3 buttons to move servo to preset positions: Button1=0°, Button2=90°, Button3=180°.
  3. Smooth Acceleration: Modify the sweep code to accelerate at the start and decelerate at the end (ease-in/ease-out motion).
  4. Multi-Servo Control: Connect 3 servos to different pins. Create a coordinated dance routine with synchronized movements.
  5. Sensor-Triggered Servo: Use an ultrasonic distance sensor—servo angle represents distance (close=0°, far=180°). Create a distance indicator.
  6. Servo Sequencer: Create a sequence of 10 different positions with different speeds. Store in arrays and loop through them.
  7. Web-Controlled Servo: Combine with WiFi capabilities—control servo angle from a web browser (see video below for inspiration).
  8. Robotic Gripper: Build a simple gripper mechanism—use servo to open/close based on button press or sensor input.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!