Servo motors let you move things to specific angles easily. The Servo library helps you control these motors with simple commands.
You want to open or close a small door automatically.
You need to move a robot arm to certain positions.
You want to control the steering of a small car.
You want to make a dial or pointer move to show a value.
Syntax
Arduino
include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(pinNumber); // Connect servo to a pin
}
void loop() {
myServo.write(angle); // Move servo to angle (0-180)
delay(timeInMilliseconds); // Wait for servo to reach position
}
Use attach() to connect the servo to a pin.
write() moves the servo to an angle between 0 and 180 degrees.
Examples
This moves the servo to 90 degrees and waits 1 second.
Arduino
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Attach servo to pin 9
}
void loop() {
myServo.write(90); // Move servo to middle position
delay(1000); // Wait 1 second
}
This smoothly moves the servo from 0 to 180 degrees in steps of 10.
Arduino
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(10); // Attach servo to pin 10
}
void loop() {
for (int angle = 0; angle <= 180; angle += 10) {
myServo.write(angle); // Move servo from0 to 180in steps
delay(500); // Wait half a second
}
}
Sample Program
This program moves the servo motor to 0, then 90, then 180 degrees, pausing 1 second at each position.
Arduino
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Connect servo to pin 9
}
void loop() {
myServo.write(0); // Move to 0 degrees
delay(1000); // Wait 1 second
myServo.write(90); // Move to 90 degrees
delay(1000); // Wait 1 second
myServo.write(180); // Move to 180 degrees
delay(1000); // Wait 1 second
}
OutputSuccess
Important Notes
Make sure your servo power supply can provide enough current.
Do not force the servo arm by hand while powered.
Use delays to give the servo time to reach the position before moving again.
Summary
The Servo library makes controlling servo motors easy.
Attach the servo to a pin, then use write() to set the angle.
Use delays to allow the servo to move smoothly between positions.
Practice
(1/5)
1. What is the main purpose of the Servo library in Arduino?
easy
A. To control servo motors easily by setting angles
B. To read temperature sensors
C. To connect to Wi-Fi networks
D. To display text on an LCD screen
Solution
Step 1: Understand the Servo library role
The Servo library is designed to control servo motors by sending signals to set their position.
Step 2: Compare options with library purpose
Only To control servo motors easily by setting angles matches the library's function of controlling servo motors by setting angles.
Final Answer:
To control servo motors easily by setting angles -> Option A
Quick Check:
Servo library controls servo motors = D [OK]
Hint: Servo library controls motors by angle commands [OK]
Common Mistakes:
Confusing Servo library with sensor or display libraries
Thinking it manages Wi-Fi or other hardware
Assuming it reads data instead of controlling motors
2. Which of the following is the correct way to attach a servo to pin 9 using the Servo library?
easy
A. servo.attach(9);
B. servo.attachPin(9);
C. attachServo(9);
D. servo.connect(9);
Solution
Step 1: Recall the Servo attach syntax
The Servo library uses the method attach(pinNumber) to connect the servo to a pin.
Step 2: Match the correct method call
Only servo.attach(9); matches the correct syntax.
Final Answer:
servo.attach(9); -> Option A
Quick Check:
Attach servo with attach(pin) = C [OK]
Hint: Use attach(pin) to connect servo to pin [OK]
Common Mistakes:
Using incorrect method names like attachPin or connect
Forgetting the object name before attach
Using wrong syntax without parentheses
3. What will be the output behavior of this Arduino code snippet controlling a servo?
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(10);
myServo.write(90);
}
void loop() {
// no code here
}
medium
A. The servo continuously rotates back and forth
B. The servo moves to 90 degrees and stays there
C. The servo does not move at all
D. The code causes a compile error
Solution
Step 1: Analyze the setup function
The servo is attached to pin 10 and immediately set to 90 degrees using write(90).
Step 2: Check the loop function
The loop is empty, so no further commands change the servo position.
Final Answer:
The servo moves to 90 degrees and stays there -> Option B
Quick Check:
Servo set once in setup = stays at 90 [OK]
Hint: Servo holds last written angle if loop is empty [OK]
Common Mistakes:
Thinking servo moves continuously without commands
Assuming loop is needed to move servo once
Confusing servo rotation with continuous rotation motors
4. Identify the error in this code controlling a servo motor:
The servo object is declared as Servo servo1; and attached to pin 5, which is valid.
Step 2: Review write and delay usage
The write() commands set angles 0 and 180 with delays of 1000 milliseconds, all correctly terminated with semicolons.
Final Answer:
No error; the code will run correctly -> Option C
Quick Check:
Correct syntax and logic = A [OK]
Hint: Check semicolons and valid pin numbers [OK]
Common Mistakes:
Assuming pin 5 is invalid without checking board specs
Missing semicolons after delay calls
Incorrect servo object declaration syntax
5. You want to sweep a servo motor smoothly from 0 to 180 degrees and back repeatedly. Which code snippet correctly implements this using the Servo library?
Sweeping smoothly means moving the servo in small steps with short delays between each step.
Step 2: Analyze each option
for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); } uses two for-loops to increment and decrement the angle by 1 degree with 15 ms delay, creating a smooth sweep. Options A, C, and D do not create smooth back-and-forth movement.