Arduino; problems with controlling a servo motor

Dear Arduino forum,
I'm fairly new to programming Arduino, as I've only started to learn to program last year, but by now I should know all the basics and some more.

I've decided to try out a little project controlling a servo motor with a potentiometer and I ran into a weird issue. The servo motor I'm using is hs-311. It has a range of 90 degrees but I've seen on forums that it can go up to 180 but it doesn't really matter here.

I have a sample code to control the motor here:

#include <Servo.h>
float pot1;
Servo myservo;
int max_angle = 90;
void setup() {
Serial.begin(9600);
pinMode(A0,INPUT);
myservo.attach(9);
}
void loop() {
pot1 = analogRead(A0);
Serial.println(pot1);
pot1 = map(pot1, 0, 1023, 0, max_angle);
myservo.write(pot1);
delay(15);
}

Now the problem I'm facing is that the servo motor works perfectly until it reaches the set max_angle. That can be set to 90, 60, 40, 20 even 10. But as soon as I turn the potentiometer to the full angle the motor starts to shake/vibrate and begins drawing around 200-300 mA of current.

I've checked and the potentiometer works perfectly reading a constant 1023 when fully turned. The servo is powered from an external power supply so that doesn't seem to be a problem either.

I've tested this on two motors (i bought two) and on my friends Arduino as well and the result is always the same. The servo behaves perfectly but when you turn it to the max angle defined in the code it starts to vibrate and draw a lot of current.

Any suggestions on what might be wrong would be really appreciated. Thank you for all your time!

It does matter here. What is the physical limit of the servo YOU have right there? Other forums is irrelevant.
The symptoms are of the servo hitting the stop limit and trying to move beyond it.
Paul

1 Like

Not related to your problem, but analogRead does not return a float, and map does not accept one.

Also, no need to give pot1 (which is an int, remember) global scope, and no need to set the pinMode of an analogue input.

1 Like

hs-311 has a range limit of 90 degrees. But it doesn't matter if I set max_angle to 90, 60, 20, or 10 degrees. As soon as I turn the potentiometer to the set max angle the motor starts to vibrate and draw a lot of current.

I've just checked with Serial.print() and the map function works perfectly returning values from 0 to max_angle, but I will be more careful in the future.

So again, the motor has the range of 90 degrees, and even if I set it to turn to 20 or 10 degrees max, as soon as the 20 or 10 degrees are reached the motor starts to shake but it works perfectly up until that point.

And have you tried changing pot1 to the int it should always have been? You're using an incorrect type for the analogRead(), the map() and the write(), who knows what's going to happen.

What is the external power to the HS-311? Is the power supply -ve connected to the Arduino GND? And directly not through a breadboard? That's a fairly power hungry servo

You may wish to check that the corrected map produces the range of values you expect.
It isn't always obvious.

This is odd, the float/int thing doesn't explain it, map does work for me - you should print the value after the call to map to see if its doing something odd for you...

Which Arduino are you using?
Which version of the IDE?
Have you checked for compiler warnings?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.