How to Control Precise Positioning of Servo?

Hey everyone.

I’m using an Arduino Mega to control a HiTec HS-785HB servo motor. The motor is powered by a 5V ATX power supply. I need the motor to turn a specific angle and that’s the problem I’m encountering. I looked at different tutorials but so far I haven’t found one that helped me get the motor to do the specific task.

I’ve created a code to test how the motor works. From it, I noticed the var in Servo.write(var) affects the angle it turns and whether it turns counter clockwise or clockwise. I deduced it down to var tells the servo what position it turns to. So I got an idea of the relationship between var and the servo position but I don’t know it precisely enough to make the servo turn at a specific angle. Anyone know how to make this servo motor turn precisely?

I have the test code here:

#include <Servo.h>

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

float motorDelay;

void setup() {
  // attaches the servo on pin 4 to the servo object
  myServo.attach(4);
  Serial.begin(9600);
  // set reference position for servo
  myServo.write(90);
}

void loop() {
  bool validInput = 0;
  Serial.println();
  Serial.println("Enter Value For motorDelay");
  while (validInput == 0) {
    //wait for user input
    while (Serial.available() == 0);
    motorDelay = Serial.parseFloat();
    Serial.print("motorDelay: ");
    Serial.println(motorDelay);
    myServo.write(motorDelay);
    validInput = 1;
  }
}

There is no substitute for experimenting. The actual value in Servo.write() is meaningless without reference to your actual project because, among other things, it depends on how you attach the servo arm.

You can use Servo.writeMicroseconds() for finer control.

The full range of movement can vary from servo to servo. Some will physically move 180 degrees, some a bit less, some a bit more. You will also need to experiment to discover this.

...R

I don't see what validInput is good for. It does nothing, so it shouldn't be in a test sketch.

lg, couka

Thanks for the advice Robin. It was tedious but I did an experiment with the servo and found a relationship that is useful for my project.

As for the validInput, I used it for the while loop after I put a limit on how much the servo can rotate. That way, it will force the void loop to restart (without the servo turning) if the input is out of range.

#include <Servo.h>

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

float motorDelay;

void setup() {
  // attaches the servo on pin 4 to the servo object
  myServo.attach(4);
  Serial.begin(9600);
  // set reference position for servo
  myServo.writeMicroseconds(1100);
}

void loop() {
  bool validInput = 0;
  float servoAngle;
  Serial.println();
  Serial.println("Enter Value For motorDelay");
  while (validInput == 0) {
    //wait for user input
    while (Serial.available() == 0);
    motorDelay = Serial.parseFloat();
    //preventing the servo going out of range
    if (motorDelay < 1100 || motorDelay > 1500) {
      Serial.println("Entry Not Valid");
      //restart loop without moving the servo
      break;
    }
    Serial.println("servo moving");
    Serial.print("motorDelay: ");
    Serial.println(motorDelay);
    myServo.writeMicroseconds(motorDelay);
    validInput = 1;
    Serial.println("reached the absolute end");
  }
}

Have a look at the examples in Serial Input Basics

...R