Could anyone identify why my servo is janky please?

Hello!
This a small project, dersigned to help identify servo limits so they can be claibrated in a more complex sketch.
It uses a Pot/map/servo code to allow you to move the servo, but also outputs the servo position (0-180) onto a 7 segment display module. In short, you move the position, read the number, write that value into the min/max ints in your setup. It was suppossed to be a simple job, but when I run it, the servo jumps about.
The screen works fine, and the values in the serial monitor are all as expected, but the servo just cant sit still. It shows signs of moving to the right place but pulses and flicks around that point.
The code for the screen came from the sparkfun page, and this works fine, and if I //it out then the servo works fine. Im guessing something in the borrowed code is upsetting it?>

Ive had it before where serial (9600) makes servos slow, but never seen this before.
Any ideas please?


#include <Servo.h> 
#include <SoftwareSerial.h>
 
Servo myservo;  // create servo object to control a servo 
const int softwareTx = 12;
const int softwareRx = 7;

const int ServoPotPin = A3;
int ServoVal;
int ServoPotVal;
SoftwareSerial s7s(softwareRx, softwareTx);
char tempString[10];  // Will be used with sprintf to create strings
 
void setup() 
{ 
 // Serial.begin(9600);
  s7s.begin(9600);
  clearDisplay();
  setBrightness(255);
  myservo.attach(11); 
  
} 
 
void loop() 
{ 
  ServoPotVal = analogRead(ServoPotPin);  
  Serial.print ("Servo Pot = ");
  Serial.println (ServoPotVal);
  ServoVal = map(ServoPotVal, 0, 1023, 0, 180);
  Serial.print ("Servo Value = ");
  Serial.println (ServoVal); 
  myservo.write(ServoVal);
  sprintf(tempString, "%4d", ServoVal);
  s7s.print(tempString);
  delay(5); 
} 

void setBrightness(byte value)
{
  s7s.write(0x7A);  // Set brightness command byte
  s7s.write(value);  // brightness data byte
}

void clearDisplay()
{
  s7s.write(0x76);  // Clear display command
}
1 Like

what if you change

into delay(100); ?

which arduino are you using an how is the Servo powered ?

1 Like

Post an annotated schematic showing exactly how you have wired it. Include all power sources.

I don't understand how that code can help to identify the limits.
If you want to hit the limits, I believe you have to play with this:
servo.attach(pin, min, max)

Some Cheap servos start jittering before reaching 0° or 180°. I assume that’s what OP wanted to see.

You are right that min and max play a role too

1 Like

And some go to 270.
But the movement is related to min,max. So maybe a code that is attaching with variable min. max values could be usable here. Never tried, but I expect you can "re-attach" servo in loop.

1 Like

Im using a mini pro. Which works fine with just the servo sketch. There is 6V battery into the breadboard, shared ground between Arduino and servo, and 6V going into raw of the Arduino.

It helps limits in two ways. Some servos dont travel the full 180, sometimes under about 20 and over 160 dont register.
The other reason is physical limits. Im not trying to sweep a servo on a bench, im trying to find the point at which the servo clashes against the frame, or fully opens the box or closes the eye etc.
So its more for knowing what arbitrary number i want to go from and to.

1 Like

The servo cannot be powered from the 5V Arduino output. Use a separate power supply providing at least 1 Ampere at 4.8 to 6V for small (SG-90) servos, or 2.5 A for large ones (MG996R). Don't forget to connect the grounds.

1 Like

Sorry for clarity, both red and black of the servo are powered from the battery, connected to the breadboard which also feeds raw and gnd of the Arduino

The Arduino "raw" input is generally 7V minimum. If the battery voltage drops, the Arduino may malfunction.

You will have far less trouble with projects if you power motors and servos separately from the Arduino, and obey the supply voltage and current recommendations.

1 Like

hey mate, I recently had an issue with jittery/twitching/hiccups servos using flysky transmitter receiver ibus. I added two statements to my sketch and it solved the issue. here is link,

I have not seen anything like this, not with the real UART.

But it does seem that SoftwareSerial is not your friend here. I built your project in the wokwi simulator. Only by commenting out the software serial functions did the servo behave.

I can't instantly find any reason, it feels like a conflicting use of some microprocessor peripheral.

The easiest fix would be to use an Arduino with an additional real UART, or use the real one you have and suffer the inconvenience of changing the wiring between programming and testing and doing without serial printing for testing and debugging.

I see there is a library NewSoftwareSerial, perhaps that works differently and with any luck better.

a7