Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
Servo Motors

Servo Motors

Oct 06, 2021

Servo motors are actuators that allow the precise control of the angle of the axel. Typical servo motors are constructed from a regular DC motor, combined with a gear ratio, potentiometer to measure the final position of the axel some digital components to control the motor. Most servo motors are limited to a certain range of angles, often between 0 and 180 degrees. 

Common types of hobby servos read PWM signals and convert this into angles. The easiest way to generate the correct PWM signal for a given angle is to use the Servo library in Arduino. 

Basic Servo
#include <Servo.h>

Servo myservo;  // create servo object 
int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}


We can also add some easing, to smooth out the movements of the servo and achieve a more organic movement. The algorithm used here is the same seen in Signal Filtering.

Easing servo
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int angle = 160;  // goal angle of servo
float easedAngle;  // for storing an eased position between current and goal angle
int lastChange;    // Used for timing changes of angle

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
}

void loop() {
  // change the goal angle every 5000 miliseconds
  if (millis()>= lastChange+5000) {
    lastChange = millis();
    if (angle == 160) {
      angle = 20;
    } else {
      angle = 160;
    }
  }
  easedAngle = angle*0.02 + easedAngle*0.98;
  Serial.print(angle);
  Serial.print(",");
  Serial.println(int(easedAngle)); 
  myservo.write(easedAngle);   
  delay(10);
}
, multiple selections available,
For you

Electrical Engineering
Results will update as you type.
  • Arduino Grundlagen
  • Arduino Programmieren
  • Arduino und Processing
  • Shift Registers
  • Carvey
  • Capacitors
  • Bread Boards
  • Digitaler Input
  • Digitaler Output
  • Eagle
  • Energy Harvesting
  • H-Bridge
  • I2C (en)
  • Interrupt Service Routine
  • Mosfet Transistor
  • LED
  • PCBs Herstellen
  • Pololu Servo Controller
  • Relays as Switches
  • Pulse Width Modulation
  • RFiD
  • Schrittmotorentreiber
  • SD Karten
  • Signal Filtering
  • Transistors as Switches
  • Widerstand
  • PCB Manufacturing
  • PlatformIO
  • Lynx Smart Motion Servo
  • Electronics Basics (EN)
  • Arduino Basics (en)
  • Arduino Programming (EN)
  • Digital Outputs (en)
  • Digital Input (en)
  • Serial Communication (en)
  • Pulse Width Modulation (en)
  • Resistors (en)
  • Arduino and P5.js
  • Arduino and ml5.js
  • Project box 2021
  • Servo Motors
  • H-Bridge (DE)
  • Networking
  • Bit Shifting
  • C++ (Arduino) Variables
  • Stepper Motor Drivers
  • Interrupt Service Routine (en)
  • Common Arduino Problems
  • Finite State Machine
  • Voltage Regulators
  • Arduino USB HID (Human Interface Devices)

{"serverDuration": 38, "requestCorrelationId": "5fbc361cb1224e19acd279b887df4287"}