Arduino Servo Motor Control (Code & Circuit Diagram Included)
- Get link
- X
- Other Apps
Arduino Servo Motor Control (Code & Circuit Diagram Included)
Meta description: How to control a servo motor using Arduino, including code and circuit diagram. Complete guide with code and troubleshoot tips included.
Introduction
Servo motors are one of the most versatile types of motor used in robotics- they can rotate to a specific angle and hold that position. This tutorial shows how to wire and control a standard servo motor using an Arduino Uno.
What you'll build
In this tutorial you'll learn how to connect and control a standard SG90 servo motor. We'll show you how to make a simple program in Arduino to sweep it continuously back and forth, and how to manually control the angle using a potentiometer. This is a great beginner project that introduces robotics concepts and is useful for many robotics applications, such as automated doors, camera gimbals, robotic joints, etc.
Parts needed
- • Arduino Uno development board (any type will do)
- • SG90 Micro Servo Motor (generic, common hobbyist servo)
- • Potentiometer (10k ohm)
- • Breadboard and jumpers
- • USB data cable
- • External 5V power supply (optional but recommended)
Servos are extremely useful for many robotics applications. Compared to regular motors they allow much finer control over rotational position (up to 180 degrees) and can hold a position under power.
How Servos Work
Servo motors are controlled using a form of Pulse Width Modulation (PWM) - by varying the width of a PWM signal sent to the servo, we can control the angle of rotation. Fortunately, the Arduino Servo library handles all of the PWM details for us - all we have to do is specify what angle we want the servo to rotate to. This library is included with the Arduino IDE by default.
Wiring / Circuit Diagram
The wiring for the basic servo test is shown below.
The signal wire for the servo (usually yellow or orange) should be connected to pin 9 on the Arduino. The red power wire goes to 5V, and the black or brown ground wire goes to GND.
For the potentiometer circuit, the connections are as follows:
Notes on power: A single small SG90 servo can be powered directly from the 5V pin on the Arduino. However, make sure to use an external power supply if you are using multiple servos, larger servos, or if you notice that the Arduino reboots when the servo is under load. In that case, connect the positive terminal of the power supply to the VCC pin on the servo, and connect the negative terminal to the GND on the Arduino (the two grounds should be connected together).
Step-by-step instructions
Wiring up the circuit
Use the circuit diagram above to connect the servo motor to the Arduino. Make sure the signal wire is connected to pin 9, and the power and ground wires are connected to 5V and GND respectively.
Basic Servo Sweep Example
Open the Arduino IDE. Create a new sketch and copy the following code into it:
void setup() {
myServo.attach(9); // Attach signal pin
}
void loop() {
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(15);
}
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
delay(15);
}
}
Upload the sketch to your Arduino Uno. Once uploaded, you should see the servo motor moving from 0 degrees to 180 degrees and back again.
Controlling with Potentiometer
Add the potentiometer to the circuit as shown in the circuit diagram. Upload the following sketch:
int potPin = A0;
void setup() {
myServo.attach(9);
}
void loop() {
int potValue = analogRead(potPin);
int angle = map(potValue, 0, 1023, 0, 180);
myServo.write(angle);
delay(15);
}
Now, when you turn the potentiometer, the angle of the servo should change.
Code Explanation
Let's go over some of the important parts of the code:
`myServo.attach(9);`
This line specifies that the signal for the servo is sent from pin 9 on the Arduino.
`myServo.write(angle);`
This line specifies that the servo should rotate to a specific angle (between 0 and 180 degrees).
`analogRead(potPin);`
This reads the voltage at the wiper pin of the potentiometer. Since we've connected the two outer pins to 5V and GND, this value will be between 0 and 1023.
`map(potValue, 0, 1023, 0, 180);`
This maps the 0-1023 range to a 0-180 range so that it corresponds to degrees of rotation for the servo.
`delay(15);`
This pauses the program for 15 milliseconds so that the servo has enough time to rotate before the next command. This delay is needed to get smooth rotation.
Testing
For the first test, simply try rotating the servo from 0 degrees to 180 degrees and back. Verify that there are no unusual noises or jerky motions. For the potentiometer test, slowly rotate the potentiometer from one end to the other and verify that the servo rotates smoothly as well.
Troubleshooting
Servo is jittering/twitching instead of rotating smoothly
Make sure you're using the external power supply (not powering the servo from the Arduino). Also check that the grounds are connected together (Arduino ground and power supply ground).
Servo is not rotating at all
Double-check that the signal pin is connected to pin 9 on the Arduino, and that the ground and positive pins are connected properly.
Arduino reboots when servo is rotating
This is a sign that the servo is not getting enough power. This can happen when the servo is under load (rotating against resistance). To fix this, connect the servo to an external power supply instead of the Arduino.
Potentiometer causes servo to rotate jerkily or in the wrong direction
If the rotation direction is wrong, swap the two outer pins of the potentiometer. If the rotation is still jerky, add a delay or smoothing to the potentiometer reading.
Common Problems and Solutions
Problems Likely Cause Solution
Servo is jittering/twitching instead of rotating smoothly Servo is not getting enough power Make sure the servo is powered by an external power supply
Servo is not rotating at all Servo signal not connected properly Check connections
Arduino reboots when servo is rotating Servo is not getting enough power Connect servo to external power supply
Upgrades / Modifications
- Control multiple servos at the same time
- Add a bluetooth module to control the servo wirelessly (great project for the ESP32 version of this circuit)
- Combine with HC-SR04 ultrasonic sensor for a radar-like scanning application
- Add acceleration curves to the servo motion for smoother movement
FAQ
What is the difference between a servo and a regular motor?
A servo can rotate to specific angles and hold that position under power, while a regular motor spins continuously in one direction when powered. Servos are much more convenient to use in robotics applications where precise positioning is needed.
Can I power multiple servos from the Arduino?
It's not recommended to power multiple servos from the Arduino, since large servos require a lot of current. For multiple servos, it's better to use an external power supply.
Why is my servo making a buzzing noise?
Many servos make a buzzing noise when positioned at certain angles or when under load. This is normal and nothing to worry about - the noise should disappear once the servo is positioned at a new angle.
Can I use this code with an ESP32?
The code should work with any ESP32 dev board, since it uses standard PWM output for the servo. However, make sure to consult the pinout diagram for your particular board to select the right pin for the signal wire.
Conclusion
With this knowledge you can now control a servo motor using both programmed angles and potentiometer input. This forms the basis of many robotics applications, such as robotic limbs or camera gimbals. This is a great foundation for other robotics projects, including combining with sensors or building a full robotic arm.
- Get link
- X
- Other Apps
Comments
Post a Comment