@jim-p Have connected to Vin. Please see the image I posted and the note below that.
Have tried to power it directly with 6V also as mentioned in another note above.
Maybe not ideal but 4 AA batteries would be a good starting point
So what exactly happens when you run that sketch ? Does the servo not run to an angle and stay there ?
@jim-p See note above on direct connection of power to the motor.
Are you trying to build a car like on the Hiwonder site
If yes then you will need two batteries, 7.4V and 11.1V
Have tried to power directly with 4 AAs. Same thing sweep works other exact positioning doesn't work.
When it doesn't work can hear some vibration or very light sound like the motor is trying to move when the pulse is coming from Arduino. Saw other posts in this forum and users reporting similar issue.
The 11.1 v is really for the rear wheel motors. That works without issues. So as not to complicate - didn't mention that here and focussed on the servo motor used for steering - since that is not working.
Well it need a high current 6V-8.4V power source. A 2S Li-ion battery would be perfect
Thanks. Will try to get that.
The servo will be constantly adjusting its position to maintain the requested angle. How good it is depends on the quality of the servo. It may help if you don’t keep writing the angle to the servo as in your sketch. What happens if you write the required angle in setup() and have nothing in loop() ?
Another approach would be to detach() the servo once it has reached its target position but that has the disadvantage of allowing the servo output arm to be moved by external forces. Depending on the application this may or may not be a problem
Your description of the problem is very misleading as it seems that the servo can be positioned at an angle after all
Have tried that i.e. just the positioning command in the setup(). Didn't work. Tried again to post the code again -->
//以15毫秒一步的速度转动
#include <Servo.h>
Servo servo2;
const int servo2Pin = 9; // servo2 接 Pin 9
void setup() {
servo2.attach(servo2Pin); // pin9
Serial.begin(9600);
servo2.write(100);
}
void loop( ) {
delay(1000);
//checkServo2();
//(posOfServo2 >90 ) &&
}
On your comment on misleading. Not able to position exactly. As mentioned I can do a sweep i.e. from 0 to 180. You may think then I can use this to position to say 100degrees angle. But it doesn't move if the rotor is not already at 0. If at 0 then I can move it step by step to another angle. But if the rotor is at some other position - say at 90 and I want to move it to 120 degrees - I can't. It doesn't work.
You might also consider using two 18650 batteries and a battery holder. Might be cheaper than a 2S battery especially if you consider you also need to buy a charger.
Actually if i can read the position of the angle then I may be able to increment from 90 to 120 step by step - adding 1 degree at a time. But as I understood ( and tried ) there is no way to read the exact angle the rotor is at. the servo.read() gives previously written value - as per documentation. In practice when I try it is giving some numbers not exactly what was written previously.
Will buy these or the 2S battery.
A few problems with this function.
You should initialize the value of Timer, local variables are not initialized to zero.
If servo2Dir is greater than 1, the test for (servo2Target == posOfServo2) will not work properly, and you cannot simply add/subtract the value of servo2Dir from posOfServo2. You need to test for the condition where adding/subtracting servo2Dir goes past servo2Target (either above or below).
< edit >
To illustrate the above, assume the following:
posOfServo2 = 90
servo2Target = 85
servo2Dir = 2
Calling checkServo2 will move the servo to the following positions
88
86
84 << at this point, you have passed servo2Target
82
80.......
Going the other directions:
posOfServo2 = 90
servo2Target = 95
servo2Dir = 2
Calling checkServo2 will move the servo to the following positions
92
94
96
94
96
94 ... continues forever
If you see at the top of the script servo2Dir = 1 is defined.
This script is working fine.
I was addressing the following comment that you made:
Ah ok.Understood. I used plenty of different scripts. Didn't increment more than 1 on the script you referred to.
This issue in this forum looks exactly the same I am facing although the servo motor is a different model.
https://forum.arduino.cc/t/servo-motor-not-rotating-to-specified-angle/628283
Yes it is.
He also tried to use 4 AA rather than a 7.4V battery