Tutorial 15 – Controlling 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 Arduino. Servo motors are perfect for precise angular movements, making them ideal for robotics, automated doors, or even a pan-and-tilt camera system.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 Servo Motor
- Jumper wires
- Breadboard
- USB cable for Arduino
You can order the required components here:
🧠 What Is a Servo Motor?
A servo motor is a small motor that can rotate to a specific position based on an input signal. It can rotate to precise angles (typically from 0° to 180°) and is commonly used in robotics for tasks like steering, opening/closing, or tilting.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the Servo Motor
- Connect the VCC pin of the servo to the 5V pin on the Arduino.
- Connect the GND pin of the servo to the GND pin on the Arduino.
- Connect the Signal pin 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
void setup() {
myServo.attach(9); // Attach the servo to digital pin 9
}
void loop() {
myServo.write(0); // Move the servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move the servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move the servo to 180 degrees
delay(1000); // Wait for 1 second
}
🔍 Code Breakdown
- #include <Servo.h>: Includes the Servo library, making it easy to control servos.
- Servo myServo; Creates a servo object to control the servo motor.
- myServo.attach(9); Attaches the servo to digital pin 9 on the Arduino.
- myServo.write(): Moves the servo to the specified angle (0, 90, or 180 degrees in this case).
- delay(): Pauses the program for a specified time (1000 milliseconds = 1 second).
🧪 Try This!
- Experiment with different angles in myServo.write() (e.g., 45, 135, etc.).
- Try adding a PWM input (using a potentiometer) to control the servo’s position.
🛠️ Troubleshooting Tips
- Ensure the servo is properly powered; some servos require external power sources.
- Check the Signal pin connection to ensure it's firmly attached to digital pin 9.
- If the servo doesn’t respond, test with a different digital pin and update your code accordingly.
🧭 What You Learned in This Tutorial
✔️ How to wire and control a servo motor with Arduino
✔️ How to use the Servo library for precise angle control
✔️ How to programmatically move a servo to specific positions
✔️ How to control the servo’s movement with myServo.write()
📌 Next Up: Tutorial 16 – Reading Temperature with a Thermistor
In the next tutorial, we’ll teach you how to measure temperature using a thermistor and Arduino. You’ll be able to build a simple temperature sensing system!
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering