Servo Motor is behaving weird

I recently bought the Arduino starter kit and followed all the projects, till the project 5. In that project I struggled to get everything working correctly, but after a while everything was working fine. Except the Servo Motor.

In the Serial Monitor, the code prints out something like (potVal: value, angle: angle)
Here is the code:

#include <Servo.h>

Servo myServo;

int const potPin = A0;
int potVal;
int angle;

void setup() {
  myServo.attach(9);
  Serial.begin(9600);
}

void loop() {
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  angle = map(potVal, 0, 1023, 0, 179);
  Serial.print(", angle: ");
  Serial.println(angle);

  myServo.write(angle);
  delay(15);
}

As you can see angle = map(potVal, 0, 1023, 0, 179); tells the motor to work around those numbers.
The problem is that I don’t get near those numbers, and get into the 300s (potVal) and 50s (angle).
Also the actual Server Motor is not working well, it’s behaving oddly (ex: jumping around and randomly moving the pin).

Im very new to Arduino and would love some help. Thanks in advance!

alexholstv:
I recently bought the Arduino starter kit and followed all the projects, till the project 5. In that project I struggled to get everything working correctly, but after a while everything was working fine. Except the Servo Motor.

In the Serial Monitor, the code prints out something like (potVal: value, angle: angle)
Here is the code:

#include <Servo.h>

Servo myServo;

int const potPin = A0;
int potVal;
int angle;

void setup() {
  myServo.attach(9);
  Serial.begin(9600);
}

void loop() {
  potVal = analogRead(potPin);
  Serial.print("potVal: “);
  Serial.print(potVal);
  angle = map(potVal, 0, 1023, 0, 179);
  Serial.print(”, angle: ");
  Serial.println(angle);

myServo.write(angle);
  delay(15);
}




As you can see *angle = map(potVal, 0, 1023, 0, 179);* tells the motor to work around those numbers.
The problem is that I don't get near those numbers, and get into the 300s (potVal) and 50s (angle).
Also the actual Server Motor is not working well, it's behaving oddly (ex: jumping around and randomly moving the pin).

Im very new to Arduino and would love some help. Thanks in advance!

Welcome to the Arduino forum and a “karma” for putting the code in the proper brackets so it looks like code.

I suspect the 15 milliseconds you have at the end of the “loop” is not near long enough. try 1500 or more.

Also please explain how you are powering the project.

Paul

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:

Many of those kits make the terrible recommendation to power the servo from the Arduino 5V pin.

If that is the case, stop now -- it will cause program malfunction and possibly even damage the Arduino. Use a 4xAA battery pack to power the servo and connect the grounds.

jremington:
Many of those kits make the terrible recommendation to power the servo from the Arduino 5V pin.

Can hardly blame them, when this very site does exactly the same. The below is from the knob example:

Thanks everyone for answering!

Thanks larryd for showing me how to post images!
Anyways, wow, i had no clue that Arduino would make this kind of bad recommendation in their own starter kit.

After changing the delay to be 1500 it sadly doesn't fix the issue. The pin is now moving very little at a time but still not as expected.

Disconnect the servo for now and just check that “potval” ranges all the way from 0 to 1023. If it doesn’t the potentiometer is wired wrong. That needs fixing first.

When that works “angle” will definitely go from 0 to 179. If that doesn’t work then you aren’t running the code that you have posted here.

Now connect the servo again and report what happens. I’d change the delay back down to something like 100 which will give the servo time to move provided you turn the pot fairly slowly. 1500 will make it too unresponsive.

BTW although it is definitely not a good idea for any real usage, the small servo in the kits will generally work powered from the 5V pin provided that it has no load on it. If it’s just sitting on the bench swinging its arm round it should be fine.

Steve

Can hardly blame them, when this very site does exactly the same.

That is one example among many of the sorts of misinformation to be found on the official Arduino web site.

Forum members have tried, but in many cases have not been able to get the site owners to correct their mistakes.