How to Control Servo Motor with Arduino: Simple Guide
To control a servo motor with
Arduino, use the Servo library to attach the servo to a pin and set its angle with write(). This lets you move the servo to any position between 0 and 180 degrees by sending the angle value.Syntax
The basic syntax to control a servo motor in Arduino involves including the Servo library, creating a servo object, attaching it to a pin, and then setting its position with write().
#include <Servo.h>: Adds the servo library.Servo servo;: Creates a servo object.servo.attach(pin);: Connects the servo to a digital pin.servo.write(angle);: Moves the servo to the specified angle (0-180).
arduino
#include <Servo.h> Servo servo; void setup() { servo.attach(9); // Attach servo to pin 9 } void loop() { servo.write(90); // Move servo to 90 degrees delay(1000); // Wait 1 second }
Example
This example moves the servo motor from 0 to 180 degrees in steps of 10 degrees, then back to 0. It shows how to sweep the servo smoothly.
arduino
#include <Servo.h> Servo servo; void setup() { servo.attach(9); // Attach servo to pin 9 } void loop() { for (int angle = 0; angle <= 180; angle += 10) { servo.write(angle); // Move servo to angle delay(500); // Wait half a second } for (int angle = 180; angle >= 0; angle -= 10) { servo.write(angle); // Move servo back delay(500); // Wait half a second } }
Output
The servo motor rotates smoothly from 0° to 180° in 10° steps, then back to 0° repeatedly.
Common Pitfalls
Common mistakes when controlling servo motors with Arduino include:
- Not including the
Servolibrary, which causes errors. - Forgetting to
attach()the servo to a pin before writing angles. - Using pins that do not support PWM or are reserved for other functions.
- Sending angle values outside the 0-180 range, which can damage the servo.
- Powering the servo directly from the Arduino 5V pin when it requires more current, causing resets.
Always use an external power supply for the servo if it draws more than 500mA.
arduino
// Wrong way (missing attach): #include <Servo.h> Servo servo; void setup() {} void loop() { servo.write(90); // ERROR: servo not attached delay(1000); } // Right way: #include <Servo.h> Servo servo; void setup() { servo.attach(9); } void loop() { servo.write(90); delay(1000); }
Quick Reference
Here is a quick cheat sheet for controlling a servo motor with Arduino:
| Command | Description |
|---|---|
| #include | Include the servo library |
| Servo servo; | Create a servo object |
| servo.attach(pin); | Attach servo to a digital pin |
| servo.write(angle); | Set servo position (0-180 degrees) |
| delay(ms); | Pause to allow servo to move |
Key Takeaways
Use the Servo library and attach the servo to a pin before writing angles.
Servo angles must be between 0 and 180 degrees to avoid damage.
Use an external power supply if the servo needs more current than Arduino can provide.
Avoid using pins reserved for other functions or without PWM support.
Test servo movement with small angle changes and delays for smooth control.