Tutorial 20 – Controlling a Servo Motor with Arduino
Welcome back to CraftedTech Engineering's Arduino tutorial series! In this tutorial, we'll show you how to control a servo motor using an Arduino. Servo motors are commonly used in robotics and other mechanical projects to achieve precise positioning. By the end of this tutorial, you'll be able to control the angle of a servo motor based on the input from a potentiometer.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 Servo Motor
- 1 Potentiometer (10kΩ)
- 1 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 allows for precise control over its position, making it ideal for applications like robotics, camera positioning, and moving parts in machines. Servo motors typically have a built-in feedback loop that allows the Arduino to control their position by sending PWM (Pulse Width Modulation) signals.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the Servo Motor and Potentiometer
- Connect the servo motor's power (red) wire to the 5V pin on the Arduino.
- Connect the servo motor's ground (black) wire to the GND pin on the Arduino.
- Connect the servo motor's control (yellow) wire to digital pin 9 on the Arduino.
- For the potentiometer:
-
- Connect one outer leg of the potentiometer to 5V.
- Connect the other outer leg of the potentiometer to GND.
- Connect the middle leg (wiper) of the potentiometer to analog pin A0 on the Arduino.
💻 Step 2: Arduino Code to Control the Servo Motor
#include <Servo.h> // Include the Servo library
int potPin = A0; // Pin connected to the potentiometer
int potValue = 0; // Variable to store potentiometer value
int servoPin = 9; // Pin connected to the servo motor
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(servoPin); // Attach the servo motor to the pin
Serial.begin(9600); // Start serial communication
}
void loop() {
potValue = analogRead(potPin); // Read potentiometer value (0 to 1023)
int angle = map(potValue, 0, 1023, 0, 180); // Map potentiometer value to servo angle (0 to 180)
myServo.write(angle); // Set the servo position
Serial.println(angle); // Output the angle to the Serial Monitor
delay(15); // Wait for the servo to reach the position
}
🔍 Code Breakdown
- Servo.h: This library makes it easy to control servo motors.
- analogRead(): Reads the analog value from the potentiometer (0-1023).
- map(): Maps the potentiometer value to a range that is suitable for the servo (0-180 degrees).
- myServo.write(): Moves the servo to the desired angle.
- delay(15): Ensures the servo motor has time to reach its position before the next update.
🧪 Try This!
- Experiment with the potentiometer: Turn it and watch the servo motor's angle change in real-time.
- Use multiple servos: Modify the code to control multiple servo motors for more advanced projects like robotic arms or vehicles.
- Adjust speed: Add more delays between servo position changes for smoother movements.
🛠️ Troubleshooting Tips
- Make sure the servo is receiving enough power (it needs a 5V supply).
- If the servo is not moving, check the wiring and ensure the control pin is connected to the correct pin (pin 9 in this case).
- Use the Serial Monitor to debug and view the potentiometer value and the mapped servo angle.
🧭 What You Learned in This Tutorial
✔️ How to use the Servo library to control a servo motor
✔️ How to read values from a potentiometer and map them to control servo position
✔️ How to adjust the servo motor's angle based on analog input
✔️ Basic understanding of PWM (Pulse Width Modulation) in controlling servo motors
📌 Next Up: Tutorial 21 – Using a Push Button to Control an LED
In the next tutorial, we'll learn how to use a push button to control an LED, introducing digital input/output in your Arduino projects.
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering