How to Use Servo Motors with Arduino: Beginner’s Guide
Introduction
Many robotics and automation projects use servo motors because they provide precision and control over movement. Unlike standard motors, servo motors allow you to position them at specific angles, making them ideal for applications that require fine control. In this lesson, we’ll guide you through the basics of using servo motors with Arduino, focusing on the popular SG90 model.
What is a Servo Motor?
A servo motor is a small, efficient motor that can be controlled to move to specific positions or angles. It consists of a motor coupled with a feedback mechanism (usually a potentiometer) and a control circuit. This setup allows it to achieve precise control over angular motion, typically between 0 and 180 degrees for standard servos.
Types of Servo Motors
Servo motors come in various types, but the most common ones for Arduino projects are:
SG90 Servo Motor
The SG90 is a popular, small, and inexpensive servo motor that can rotate between 0 and 180 degrees. It’s widely used in beginner-level projects because it’s easy to control with Arduino and operates on 5V, making it ideal for hobbyists.
MG90 Servo Motor
The MG90 is similar to the SG90 but features metal gears instead of plastic ones, making it more durable for higher torque applications. It is typically used in projects that require more robust performance.
How the SG90 Servo Motor Works
The SG90 servo motor uses Pulse Width Modulation (PWM) to control its position. Arduino sends a PWM signal to the servo’s control pin, which determines the angle of the motor’s shaft. The pulse width (measured in milliseconds) correlates to a specific angle, with shorter pulses (around 1 ms) corresponding to 0 degrees and longer pulses (around 2 ms) corresponding to 180 degrees.
Wiring Servo Motor with Arduino
Wiring Diagram
The SG90 motor has three wires:
- VCC (red wire): Connects to 5V on the Arduino.
- GND (brown wire): Connects to the ground pin on Arduino.
- Signal (orange wire): Connects to a PWM pin on the Arduino.
Arduino Code
Below is a simple Arduino code example to control the position of the SG90 servo motor:
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a servo object
void setup() {
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
myServo.write(0); // Move servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move servo to 180 degrees
delay(1000); // Wait for 1 second
}
Explanation of code:
- Servo.h Library: This library provides functions to control the servo motor.
- myServo.attach(9): This line tells the Arduino that the servo motor is connected to Pin 9.
- myServo.write(angle): The write() function is used to set the position of the servo motor, where the angle can range between 0 to 180 degrees.
- delay(1000): This pauses the code for 1 second, allowing the motor to reach the desired position.
Conclusion
Servo motors, especially the SG90, provide a simple and precise way to control angular movement in Arduino projects. Whether you’re building a robotic arm, creating an automatic door, or developing a pan-tilt camera system, understanding how to wire and code a servo motor is essential for beginners. As you gain more experience, you can explore more advanced applications like multiple servos for complex robotics or integrating sensors for automated control systems. Experimenting with servos is a great way to learn about control mechanisms and opens up endless possibilities for creative projects.