Introduction
Servo motors are essential for robotics and automation. Unlike regular DC motors, a servo motor can be commanded to rotate to a precise angle — making it perfect for robot arms, camera gimbals, and door locks. In this tutorial, we cover three practical projects using a standard SG90 servo and an Arduino.
Components Needed
- Arduino Uno
- SG90 Servo Motor
- 10k-ohm potentiometer (for project 2)
- Pushbutton (for project 3)
Wiring
The SG90 has three wires: Red → 5V, Brown → GND, Orange (signal) → Pin 9.
Project 1: Sweep
#include <Servo.h>
Servo myServo;
void setup() { myServo.attach(9); }
void loop() {
for (int pos = 0; pos <= 180; pos++) { myServo.write(pos); delay(15); }
for (int pos = 180; pos >= 0; pos--) { myServo.write(pos); delay(15); }
}
Project 2: Potentiometer Control
#include <Servo.h>
Servo myServo;
#define POT_PIN A0
void setup() { myServo.attach(9); }
void loop() {
int val = analogRead(POT_PIN);
int angle = map(val, 0, 1023, 0, 180);
myServo.write(angle);
delay(15);
}
Project 3: Button-Controlled Lock
#include <Servo.h>
Servo myServo;
#define BTN_PIN 2
bool locked = true;
void setup() {
myServo.attach(9);
pinMode(BTN_PIN, INPUT_PULLUP);
myServo.write(0); // Locked position
}
void loop() {
if (digitalRead(BTN_PIN) == LOW) {
locked = !locked;
myServo.write(locked ? 0 : 90);
delay(300); // Debounce
}
}
Conclusion
Servo motors open up a world of mechanical possibilities. Combine them with sensors and you can build fully autonomous robotic systems!