小编
Published2025-10-15
Introduction to Servo Motors and Arduino Nano
Servo motors have become an indispensable component in robotics, DIY electronics, and automation. Known for their precise control and ability to rotate to specific angles, servo motors are used in a variety of applications ranging from robotic arms and drones to camera gimbals and automated gates. The small yet powerful Arduino Nano is an excellent microcontroller for controlling these motors due to its compact size and rich capabilities. In this article, we will delve into how to control servo motors using the Arduino Nano, with a focus on the essential code, wiring, and practical tips.
A servo motor is a type of motor that is used to achieve precise control over angular position. Unlike regular motors that spin continuously, a servo motor can rotate to specific positions within a set range, typically 0 to 180 degrees. This makes them highly suitable for applications where precise positioning is required. Servo motors are widely used in robotics, RC vehicles, automation systems, and more.
The main components of a servo motor are the motor itself, a feedback mechanism (usually a potentiometer), and a controller circuit. The controller receives a pulse width modulation (PWM) signal, which dictates the position of the servo. By varying the length of these pulses, the servo can be moved to different positions.
Why Use the Arduino Nano for Servo Control?
The Arduino Nano is a small, breadboard-friendly microcontroller board that is part of the Arduino family. It is powered by the ATmega328P chip, offering sufficient I/O pins and a fast processing speed for a variety of tasks, including controlling servo motors.
One of the key benefits of using the Arduino Nano for servo motor control is its simplicity. With the Arduino IDE, you can easily write and upload the necessary code to control servos. Additionally, the Nano provides digital pins capable of producing the PWM signals that servo motors need to operate. Its small size and low cost make it an excellent choice for prototyping and compact projects.
Components Needed for Servo Motor Control
To get started with servo motor control using the Arduino Nano, you will need a few essential components:
Arduino Nano: The central control unit that processes your code and sends signals to the servo motor.
Servo Motor: A standard 3-wire servo motor. Popular models include the SG90, MG90S, or larger ones depending on your application.
Power Supply: Servo motors typically require more power than the Arduino Nano can supply. A 5V or 6V external power supply is ideal, especially for larger motors.
Jumper Wires: These will be used to make the necessary connections between the Arduino Nano, servo, and power supply.
Breadboard (optional): If you're prototyping, a breadboard can help organize your circuit connections.
Wiring the Servo Motor to the Arduino Nano
The wiring process is quite straightforward. Here’s how to wire your servo motor to the Arduino Nano:
Servo Motor Wiring: The typical servo motor has three wires: a power wire (usually red), a ground wire (black or brown), and a signal wire (yellow or white).
Connect the power wire to the 5V pin on the Arduino Nano.
Connect the ground wire to one of the GND pins on the Arduino Nano.
Connect the signal wire to one of the digital pins on the Arduino Nano (e.g., D9).
Power Supply: If your servo motor requires more power than the Arduino Nano can supply, use an external 5V power source to power the servo. Be sure to connect the ground of the external power supply to the ground of the Arduino Nano to create a common reference point.
Programming the Arduino Nano to Control the Servo Motor
Now that we’ve set up the hardware, it’s time to write the code to control the servo motor using the Arduino Nano. The Arduino IDE makes this task incredibly easy with its built-in Servo library.
Install the Servo Library: Before you start coding, you need to install the Servo library in the Arduino IDE. To do this:
Go to Sketch > Include Library > Manage Libraries.
Search for Servo in the Library Manager and click "Install."
Basic Servo Control Code: The following simple code will make your servo rotate between 0 and 180 degrees continuously.
Servo myServo; // Create a Servo object
myServo.attach(9); // Attach the servo signal wire to pin 9
myServo.write(0); // Rotate the servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Rotate the servo to 180 degrees
delay(1000); // Wait for 1 second
#include : This line includes the Servo library, which provides all the necessary functions for controlling servo motors.
Servo myServo;: This line creates a Servo object called myServo.
myServo.attach(9);: This attaches the signal pin (Pin 9) to the myServo object. You can change this to any digital pin on the Arduino Nano.
myServo.write(angle);: This function tells the servo to move to the specified angle (between 0 and 180 degrees).
delay(1000);: This function pauses the program for 1 second before executing the next command.
Once you've written the code, upload it to the Arduino Nano, and the servo should begin moving between 0 and 180 degrees, pausing for 1 second at each position.
Advanced Servo Control Techniques
Now that you've mastered the basics of controlling a servo motor, let’s explore some more advanced techniques to enhance your projects. These techniques will help you create more complex and dynamic motion patterns for your servo motors, whether you're building a robot, a gimbal, or another project that requires precision control.
Using Multiple Servo Motors
One of the key advantages of the Arduino Nano is that it allows you to control multiple servo motors simultaneously. You can control several servos by creating multiple Servo objects in your code.
Here’s an example of controlling two servos:
Servo servo1; // Create the first Servo object
Servo servo2; // Create the second Servo object
servo1.attach(9); // Attach servo1 to pin 9
servo2.attach(10); // Attach servo2 to pin 10
servo1.write(90); // Move servo1 to 90 degrees
servo2.write(45); // Move servo2 to 45 degrees
delay(1000); // Wait for 1 second
servo1.write(0); // Move servo1 to 0 degrees
servo2.write(135); // Move servo2 to 135 degrees
delay(1000); // Wait for 1 second
In this code, both servos are attached to different pins on the Arduino Nano, and each servo is controlled independently. You can add more servos as needed, simply by creating additional Servo objects and attaching them to the appropriate pins.
By default, the write() function immediately moves the servo to the target position. If you want to control the speed of the servo's movement, you can gradually increase or decrease the position over time.
Here’s an example that smoothly moves a servo from 0 to 180 degrees:
myServo.attach(9); // Attach the servo to pin 9
for (int pos = 0; pos <= 180; pos++) { // Move the servo from 0 to 180 degrees
delay(15); // Wait for the servo to reach the position
for (int pos = 180; pos >= 0; pos--) { // Move the servo back from 180 to 0 degrees
delay(15); // Wait for the servo to reach the position
This code gradually moves the servo from 0 to 180 degrees and back, with a small delay between each increment. The delay of 15 milliseconds controls the speed of movement. Adjusting this delay will change the speed of the servo.
PWM Control for Fine-Tuning
In some advanced projects, you may need to control the servo with more precision than just the standard 0 to 180 degrees range. The Arduino Nano allows you to generate custom PWM signals to achieve finer control over the servo motor’s position. However, for most applications, the Servo library’s standard write() function provides enough precision.
Controlling servo motors with the Arduino Nano opens up a wide range of possibilities for your projects, whether you are building a robotic arm, creating an automated camera system, or even designing a small-scale CNC machine. With the Arduino Nano’s ease of use and powerful Servo library, you can quickly implement precise control over servo
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.