Hello, I am trying to control the servo "hs-5645mg" with arduino but it doesn't works in all degree.
I have used the next code. With it, the servo changes it position
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
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
}
}
However, if I use any position like it doesn't move:
It happened it because it only had given it no time to get into position and if it was at position 0 when you started that line it was already there. Hence no movement.
I think that the problem is the library.
I have modified the schematic. I supply 5v since powerbank(3A output) to the servo and I wire the GND and the yellow pin to arduino (PIN 3).
I will try to explain better:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(3); // 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
}
}
If I use the code above, the servo moves from left to right and right to left, but not to all degrees. I saw that the servo does not move to 0. It stops at about 20 degrees. At the other end the servo stops at 160 and does not go to 180.
On the other hand, if I use the following code, the servo does not move no matter what degree it starts at and what degree is chosen.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(3); // attaches the servo on pin 9 to the servo object
myservo.write(90); //servo is in the 0 degree
delay(15); // waits 15ms for the servo to reach the position
}
void loop() {}
In conclusion, the servo only move if I use a "for" and It doesn't move in all degree
When you attach() the servo it moves to its default start position, which is 90 degrees. Then you write(90) to it which tells it to move to 90 degrees, but it is already there. Why do you expect to see it move ?
Try this
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup()
{
myservo.attach(3); // attaches the servo on pin 3 to the servo object
delay(1000);
myservo.write(150);
}
void loop() {}