Unable to position the rotor of LD-1501MG servo motor to an exact angle

It would have helped if you had mentioned that previously

Try this

//以15毫秒一步的速度转动
#include <Servo.h>

Servo servo2;

const int servo2Pin = 9;  // servo2 接 Pin 9


void setup() {

  servo2.attach(servo2Pin); // pin9
  Serial.begin(9600);
  servo2.write(0);
  delay(2000);
  servo2.write(100);
}
void loop( ) {
 
}

This doesn't work. As I mentioned have to increment/decrement from 'current position' by 1.
So servo2.write(0) won't work if the rotor is at some other angle say 30degrees.
If the rotor was at 0 degrees again servo2.write(100) won't work as it has to be incremented repeatedly by 1 till it reaches 100.

If it was possible to read the exact angle of the rotor then could have managed as I could increase/decrease by 1 till it reaches the desired angle.

@jim-p Yes. noticed that. Have ordered a 7.4v lipo battery. Let me see if that works.

That is a very unhelpful description. What does the servo do, if anything ?

You won't get anywhere powering the servo from the Arduino 5V, and you might damage the Arduino.

Power the servo like this. Don't forget to connect the grounds.

Tried that. Please see post #10 above.

@jremington Please see this post. Did try connecting power directly with the power supply and arduino grounds connected. Behaviour didn't change.

With a 7.4V battery?

I did. You did something wrong, but gave no details, so I can't be of any further help.

Good luck with your project.

The second paragraph refers to the code you sent. Was commenting on that.

@jim-p Yet to get the 7.4v battery. Have now placed order. Was responding to others who are trying to help.

Apologies.What I meant was I wired exactly as the diagram you posted but there was no change in behaviour.

Wired what, exactly? That silly 9V smoke alarm battery?

The data sheet for that servo shows 500mA run current, stall current of 2.5A at 6.0V power supply. A fresh set of 4 AA alkaline batteries should work for a short amount of time, no way will it run off a 9V battery, particularly when fed through the voltage regulator on the UNO.

@UKHeliBob @jremington I am posting on arduino forum for the first time. Wondering if all can see all the conversation - as I posted quite a bit in detail - and answering every post of anyone trying to help - but was surprised to see your comments that details are not there. Apologies for that , but frankly don't know what additional info can be given. May be because all the information is quite distributed and confusing. Thanks a lot for your comments , diagrams and for helping so far.

You've been told several times that a 9V battery won't work for this project. Is that confusing?

1 Like

It is not, no need to apologise

Start by describing exactly what happens when you run the sketch in post #41

Have you tried measuring the battery voltage whilst the sketch is running ?

Print out this graphic (may have to adjust size) and paste under servo horn for angle reference.

I use this simple test sketch for calibrating servos with Nano / UNO, type in angles in microseconds (500 ~ 2480) in Serial monitor entry box and press [ENTER] or click SEND button.

// servoCalTest.ino

#include <Servo.h>
Servo servo;
int
  zeroVal = 500,
  centerVal = 1490,
  maxVal = 2480;
  
byte cntr = 9;

void setup() 
{ 
  Serial.begin(9600); // set serial monitor line ending to Newline
  servo.write(centerVal);
  servo.attach(9,zeroVal,maxVal);
}
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(); 
    // look for the newline. That's the end of your sentence:
    if (Serial.read() == '\n') {} // skip 1 second wait
    //pos = constrain(pos,0,2500);
    servo.write(pos);
    if(++cntr >= 8) // print header
    
    {
      Serial.println("degrees      microseconds");
      cntr = 0;
    }  
    Serial.print(servo.read());Serial.print("\t\t");
    Serial.println(servo.readMicroseconds());

  }
}