Table of Contents

Controlling a Servo Motor

This scenario demonstrates how to control the position of a servo motor using Arduino. Servo motors are used for precise angular positioning in robotics and automation.

Prerequisites

Hardware Connections

Component Arduino Pin
————–————-
Servo Signal 10 (PWM)

Servo specifications:

Suggested Knowledge Resources

Task

Write a program that rotates the servo sequentially to 0°, 90°, and 180°, pausing for one second at each position.

Steps

Step 1: Include Servo Library

#include <Servo.h>

Step 2: Initialize Servo Object

Declare a servo object and assign it a pin:

Servo myServo;
const int servoPin = 10;

Step 3: Setup

Attach the servo object to the servo pin:

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

Step 4: Control Loop

Program the servo rotation sequence:

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
}

Validation

Observe the servo motor rotate precisely to the angles 0°, 90°, and 180°, pausing clearly at each position.

Troubleshooting

If the servo does not move or jittering occurs: