Tutorial 12 – Using a Servo Motor with Arduino
Welcome back to the CraftedTech Engineering Arduino tutorial series! In this tutorial, we’ll show you how to control a servo motor with your Arduino. Servo motors are great for projects requiring precise control over rotational movement, such as robotic arms, camera gimbals, or automated doors.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 Servo Motor
- Breadboard
- Jumper wires
- USB cable for Arduino
You can order the required components here:
🧠 What Is a Servo Motor?
A servo motor is a type of motor that can be controlled to rotate to a specific angle within a limited range, typically 0 to 180 degrees. It is commonly used in robotics, RC vehicles, and automation projects because of its precision and ability to hold positions.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the Servo Motor
- Connect the red wire (power) of the servo to 5V on the Arduino.
- Connect the black/brown wire (ground) of the servo to GND on the Arduino.
- Connect the yellow/white wire (signal) of the servo to digital pin 9 on the Arduino.
💻 Step 2: Arduino Code to Control the Servo
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
int angle = 0; // Variable to hold the angle
void setup() {
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
for (angle = 0; angle <= 180; angle++) { // Sweep from 0 to 180 degrees
myServo.write(angle); // Set the servo to the specified angle
delay(15); // Wait for the servo to reach the position
}
for (angle = 180; angle >= 0; angle--) { // Sweep from 180 back to 0 degrees
myServo.write(angle); // Set the servo to the specified angle
delay(15); // Wait for the servo to reach the position
}
}
🔍 Code Breakdown
- Servo.h: This library simplifies controlling servo motors.
- Servo.attach(): Attaches the servo motor to a specific pin.
- Servo.write(): Sets the angle of the servo motor (0 to 180 degrees).
- delay(): Adds a delay to give the servo time to move to the new position.
🧪 Try This!
- Modify the code to set the servo to specific angles based on button presses.
- Use a potentiometer to control the servo angle dynamically.
🛠️ Troubleshooting Tips
- If the servo doesn't move, double-check the connections, especially the signal wire.
- Ensure the Arduino board is receiving enough power, as servos can draw significant current.
- Make sure your servo is compatible with 5V if using a standard Arduino.
🧭 What You Learned in This Tutorial
✔️ How to wire a servo motor to Arduino
✔️ How to use the Servo library to control a servo motor
✔️ How to move the servo to specific angles
✔️ How to create a sweep motion with the servo
📌 Next Up: Tutorial 13 – Using an Ultrasonic Sensor with Arduino
In the next tutorial, we’ll dive into using an ultrasonic sensor to measure distances. Ultrasonic sensors are great for projects like obstacle avoidance in robots, distance measuring, and more.
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering