Arduino Nano - Servo Motor

This tutorial instructs you how to use Arduino Nano to control servo motor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Servo Motor
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Servo Motor

A servo motor is a component that is capable of rotating its shaft, typically between 0° and 180°. It is commonly utilized to control the angular position of an object.

The Servo Motor Pinout

The servo motor has three pins:

  • VCC pin (usually red) must be connected to VCC (5V)
  • GND pin (generally black or brown) must be connected to GND (0V)
  • Signal pin (commonly yellow or orange) receives the PWM control signal from a pin of an Arduino Nano.
servo motor pinout

Arduino Nano - Servo Motor

Some of the Arduino Nano pins can be programmed to produce a PWM signal. We can connect the servo motor's signal pin to one of these pins and program it to generate a PWM output. This will allow us to control the servo motor.

Thanks to the Arduino Nano Servo library, controlling a servo motor is easy. We don't need to understand how servo motors work or how to generate PWM signals. All we need to do is learn how to use the library.

Wiring Diagram

The wiring diagram between Arduino Nano and Servo Motor

This image is created using Fritzing. Click to enlarge image

For the sake of simplicity, the above wiring diagram is used for testing or educational purposes, and for a servo motor with a small torque. We strongly suggest using an external power source for the servo motor in practice. The wiring diagram below illustrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and servo motor external power supply

This image is created using Fritzing. Click to enlarge image

Please do not forget to connect GND of the external power to GND of Arduino.

How To Program For Servo Motor

  • Include the library:
#include <Servo.h>
  • Create a Servo object:
Servo servo;
  • If you have more than one servo motor, simply declare additional Servo objects:
Servo servo1; Servo servo2;
  • Assign the Arduino Nano that connects to the signal pin of the servo motor. As an example, use pin 9:
servo.attach(9);
  • Finally, control the servo motor to the required angle. For instance, 90°
servo.write(90);

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-servo-motor */ #include <Servo.h> #define SERVO_PIN 9 // Arduino Nano pin D9 connected to the signal pin of servo motor Servo servo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // rotate from 0 degrees to 180 degrees // in steps of 1 degree servo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // rotate from 180 degrees to 0 degrees servo.write(pos); // tell servo to go to position in variable 'pos' delay(10); // waits 10ms for the servo to reach the position } }

Detailed Instructions

  • Attach the Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE, select the correct board and port.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
How to upload code to Arduino Nano
  • Check out the outcome: The servo motor rotates in both clockwise and counter-clockwise directions.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

How to Control Speed of Servo Motor

Utilizing the map() and millis() functions, we can adjust the speed of a servo motor in a steady manner while not impeding other code.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-servo-motor */ #include <Servo.h> #define SERVO_PIN 9 // Arduino Nano pin D9 connected to the signal pin of servo motor Servo servo; unsigned long MOVING_TIME = 3000; // moving time is 3 seconds unsigned long move_start_ms; int angle_start = 30; // 30° int angle_stop = 90; // 90° void setup() { servo.attach(SERVO_PIN); move_start_ms = millis(); // start moving // TODO: other code } void loop() { unsigned long progress = millis() - move_start_ms; if (progress <= MOVING_TIME) { long angle = map(progress, 0, MOVING_TIME, angle_start, angle_stop); servo.write(angle); } // TODO: other code }

Video Tutorial