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

  • Familiarize yourself with Arduino hardware reference.
  • Install the built-in Arduino Servo library.
  • Understand basic PWM concepts for servo control.

Hardware Connections

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

Servo specifications:

  • Standard miniature servo (SG90 or similar)
  • Voltage: Typically 5V
  • PWM frequency: 50 Hz
  • Rotation range: ~0° to 180°

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:

  • Check the servo connections (signal to pin 10, power, and ground).
  • Ensure power supply is stable (servo typically requires 5V).
  • Verify servo library inclusion and pin assignment.
en/iot-open/practical/hardware/taltech/arduino/scenarios/servo.txt · Last modified: 2025/07/18 11:04 by ingmar05
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0