Go Down

Topic: Can Arduino write the decimal to control a servo motor MG90s? (Read 1 time) previous topic - next topic

mymelo

็Hi,
I'm doing a project using a servo motor MG90s for control a laser projection to indicate the distance. To cover a distance of 70 cm on the ground and  i need the resolution of the distance every each 1 cm..When the motor rotates to the laser projection distance farther,the motor turns to a decimal degrees for each 1 cm..The code myservo.write in the libraries receive only the int. how do i code the program to read the decimal.
Thankyou

groundFungus

You can use the Servo.writeMicroseconds to get finer resolution.  The Servo.write function will not take floats (decimals).

https://www.arduino.cc/en/Reference/Servo


mymelo

I tried this code Servo.writeMicroseconds but it didn't move.

groundFungus

What numbers did you use?  The numbers need to be between about 1000 and 2000 microseconds.  The servo will center (90 degrees) with an input (servo.writeMicroseconds) of near 1500.  Google the protocol of a servo signal for more information.

756E6C

You can try this test sketch, start at 1600 and try moving 1 micro at a time but I bet you have to move 3 - 5 micros before it moves and then it will jump past your intended position, it's backlash in the gear train that you can't do anything about, the "cost" of cheap. :)
Code: [Select]
/*
 Try this test sketch with the Servo library to see how your
 servo responds to different settings, type a position
 (0 to 180) or if you type a number greater than 200 it will be
 interpreted as microseconds(544 to 2400), in the top of serial
 monitor and hit [ENTER], start at 90 (or 1472) and work your
 way toward zero (544) 5 degrees (or 50 micros) at a time, then
 toward 180 (2400).
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(9600); //set serial monitor baud rate to match
  servo.write(90);
  servo.attach(9);
  prntIt();
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int pos = Serial.parseInt();
    pos = constrain(pos, 0, 2400);
    servo.write(pos);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("  degrees = ");
  Serial.print(servo.read());
  Serial.print("\t");
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());
}

mymelo

 At first, I take a degree But after I switched to the pulse.The first three degree motor does not rotate and start rotate at 4 degree (pulse 1025).

756E6C

Well a degree is about 10.3 micros, does the servo move if you keep stepping 1 micro after 1025? Try reversing and counting down and see how many micros it takes to move the servo, that will be the amount of "backlash" or "free play" that you will have to just "work around". If you know how much backlash you have you might get closer to desired position by adding micros when you reverse direction.

Go Up