Sound & motion · Lesson
10 min read
Servo motors
Move to exact angles with the Servo library.
What You Will Learn
- Attach a servo
- Write angles 0–180°
- Power servos safely
A hobby servo contains a motor, gears and a position sensor. It reads a pulse every 20 ms: about 1 ms means 0°, 2 ms means 180°. The Servo library makes these pulses for you.
Arduino C++
#include <Servo.h>
Servo arm;
void setup() { arm.attach(9); }
void loop() { arm.write(0); delay(1000); arm.write(180); delay(1000); }Servo follows a knobarduino-uno
Note: Turn the knob to steer the servo.
Servos draw current spikes. One small SG90 on the Uno's 5V pin is fine; several or larger servos need a separate 5 V supply with GND shared with the board.
Check Your Understanding
Test what you learned in this lesson