Servo Motors

Servo Motors

Servo Motor

What is a Servo Motor?

A servo motor is a rotary actuator that allows precise control of angular position. Unlike regular motors, servos can be commanded to move to a specific angle.

Types of Servos

TypeDescriptionBest For
Standard (180°)Rotates 0-180 degreesRobotics, steering
Continuous (360°)Rotates continuouslyWheels, conveyors
MicroSmall size, low torqueSmall projects

SG90 Servo Specifications

ParameterValue
Voltage4.8V - 6V
Torque1.5-2.5 kg/cm
Speed0.1-0.2 sec/60°
Angle180 degrees
Weight9g

Connecting a Servo

Pin Connections

Servo WireArduino
Orange (Signal)Pin 9 (PWM)
Red (VCC)5V
Brown (GND)GND

Connection Diagram

Arduino          Servo
Pin 9    ------  Orange (Signal)
5V       ------  Red (VCC)
GND      ------  Brown (GND)

Using the Servo Library

Arduino has a built-in Servo library:

#include <Servo.h>

Servo myServo;

void setup() { myServo.attach(9); // Pin 9 }

void loop() { myServo.write(0); // Position 0° delay(1000); myServo.write(90); // Position 90° delay(1000); myServo.write(180); // Position 180° delay(1000); }

Key Servo Functions

FunctionDescription
attach(pin)Connect servo to pin
write(angle)Set position (0-180)
writeMicroseconds(us)Set position in μs
read()Get current angle
attached()Check if connected

Basic Example: Sweep

Make the servo sweep back and forth:

#include <Servo.h>

Servo myServo; int pos = 0;

void setup() { myServo.attach(9); }

void loop() { // Sweep from 0 to 180 for (pos = 0; pos <= 180; pos += 1) { myServo.write(pos); delay(15); } // Sweep from 180 to 0 for (pos = 180; pos >= 0; pos -= 1) { myServo.write(pos); delay(15); } }

Controlling Servo with Button

Add buttons to control servo position:

#include <Servo.h>

Servo myServo; int buttonUp = 2; int buttonDown = 3; int currentPos = 90;

void setup() { myServo.attach(9); pinMode(buttonUp, INPUT_PULLUP); pinMode(buttonDown, INPUT_PULLUP); }

void loop() { if (digitalRead(buttonUp) == LOW && currentPos < 180) { currentPos += 5; myServo.write(currentPos); delay(50); } if (digitalRead(buttonDown) == LOW && currentPos > 0) { currentPos -= 5; myServo.write(currentPos); delay(50); } }

Project: Distance-Controlled Servo

Control servo position based on distance sensor:

#include <Servo.h>

Servo myServo; const int trigPin = 9; const int echoPin = 10;

int getDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); return pulseIn(echoPin, HIGH) / 29 / 2; }

void setup() { myServo.attach(6); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

void loop() { int distance = getDistance(); // Map distance to angle (10cm = 0°, 100cm = 180°) int angle = map(distance, 10, 100, 0, 180); // Constrain to valid range angle = constrain(angle, 0, 180); myServo.write(angle); delay(100); }

Project: Servo Radar

Create a simple radar scanner:

#include <Servo.h>

Servo myServo; const int trigPin = 9; const int echoPin = 10;

int getDistance() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); return pulseIn(echoPin, HIGH) / 29 / 2; }

void setup() { myServo.attach(6); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); }

void loop() { // Scan left to right for (int angle = 0; angle <= 180; angle += 2) { myServo.write(angle); int distance = getDistance(); Serial.print(angle); Serial.print(","); Serial.println(distance); delay(50); } // Scan right to left for (int angle = 180; angle >= 0; angle -= 2) { myServo.write(angle); int distance = getDistance(); Serial.print(angle); Serial.print(","); Serial.println(distance); delay(50); } }

Power Considerations

Important Notes

  • Servos can draw significant current
  • Use external power for multiple servos
  • Don't power servos directly from Arduino for extended use
  • Connect all grounds together

External Power Connection

External 5-6V Supply
      |
    [Servo]
      |
     GND ---- Arduino GND
      |
    Signal ---- Arduino Pin

Summary

Servo motors provide precise angular control:

  • Use Servo library for easy control
  • write(angle) sets position 0-180 degrees
  • Attach to PWM pins (3,5,6,9,10,11)
  • Consider external power for multiple servos
Servos are perfect for robotics and automation projects!

Next Lesson

In the next lesson, you'll learn to control high-voltage devices using a relay module.

Quiz - Quiz - Servo Motors

1. How many degrees can a standard SG90 servo rotate?

2. Which library is used to control servo motors?

3. What does myServo.write(90) do?

4. Which Arduino pins can be used for servo control?

Reading Sensors - Distance Sensor