[Solved] My SG90 is vibrating

So for a bit of context i'm working on a simple project for a school assignment and I encountered an issue where the SG90 Servo Motor starts vibrating when it rotates to at least in my case 180°. Now I tried multiple things to fix it, at first I thought it was my code repeatedly telling the servo to rotate, but after I modified the code so that it only told it to rotate once it kept vibrating, I then made sure my fix had actually worked by creating a simple sketch that just rotates the servo to 180° and stops, but it kept vibrating so I'm kind of at a loss here for what to do. I only am using one SG90 and just have the power wire connected as my classes wiring diagram says. My current guess is that the servo is just broken, I provide the code i'm using down below, but I don't think it's the issue as the serial prints I'm using for troubleshooting all are exactly as it should be. Any and all help will be greatly appreciated thank you in advance.

#include <Keypad.h>
#include <Servo.h>
Servo myservo;

// Define keypad size
const byte ROWS = 4;
const byte COLS = 4;
int combo = 0;
int combo1 = 0;
int combo2 = 1;
unsigned long
lastActionTime = 0;

// Define keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

// Connect rows and columns to these Arduino pins
byte rowPins[ROWS] = {9, 8, 7, 6};   // Adjust if needed
byte colPins[COLS] = {5, 4, 3, 2};

// Create keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Code buffer
char inputCode[7];  // 6 digits + null terminator
const char correctCode[] = "169420";
byte inputIndex = 0;

void setup() {
  Serial.begin(9600);
  resetInput();
  Serial.println("Enter 6-digit code followed by #");
    myservo.attach(10);
  myservo.write(90);// move servos to center position -> 90°

}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Serial.print("Key pressed: ");
    Serial.println(key);

    if (key == '#') {
      Serial.print("Code entered: ");
      Serial.println(inputCode);

      if (inputIndex == 6 && strcmp(inputCode, correctCode) == 0) {
        Serial.println("Access Granted");
        combo = 1;
        combo1 = 1;
        combo2 = 0;
      } else  {
       
        Serial.println("Access Denied");
        combo = 0;
       resetInput();
        
      }

      
    } 
    else if (inputIndex < 6 && key >= '0' && key <= '9') {
      inputCode[inputIndex] = key;
      inputIndex++;
      inputCode[inputIndex] = '\0'; // Keep string null-terminated
    }
  }

  if (key == '*') {
    combo = 0;
    combo1 = 0;
    combo2 = 1;
    resetInput();
       
  }

 if (combo == 1 && combo1 == 1) {
myservo.write(180);
    Serial.println("Servo Rotate");
    combo1 = 0;
   lastActionTime = millis();
  } 
 else if (combo == 0 && combo2 == 1) {

    myservo.write(90);
    Serial.println("Servo Return");
combo2 = 0;
   lastActionTime = millis();
 }
  }



void resetInput() {
    char key = keypad.getKey();
    memset(inputCode, 0, sizeof(inputCode));
  inputIndex = 0;
  Serial.println("Input reset");
     }


Yes this will happen. It happens because the translation between degrees and the servo position, leaves the servo on the edge of what it can resolve.

The way to stop this happening is to use the writeMicroseconds method, and then you can fine tweak the actual value until you find a point where this vibrating does not happen.

Hi, @kateykat411742069

Those servos are cheap and not the best quality.
If you replace 180 with 179 or even 178, does the vibration stop?

The problem could be that the feedback pot in the servo is not aligned properly.
180 could be passed the mechanical stop at 180 mechanical rotation and the servo electronics keeps trying to get to 180 of the feedback.
179 or 178 or 177 could find if this is the problem by stopping the vibration.

Tom.. :smiley: :+1: :coffee: :australia:

You could try to adjust the pulse timing little bit.
Full syntax for servo attach is:
servo.attach(pin, min, max)
where default min is 544 and max 2400.
Try for example:
myservo.attach(10, 520, 2350);

If that connection is to the Arduino 5V pin, that is a problem. Power servos separately (a 4xAA battery pack works well), and don't forget to connect the grounds.

Thank you this worked but I had to lower it quite a bite I set it to myservo.attach(10, 472, 2250);, every so often it seems to jitter a bit, but its only noticeable when you hold it barely at that though

No I tried that it didn't work, but I did fix it thanks anyway

Whatever works the best. Going too low with the min might start jittering on 0deg end. Too high on max similarly on 180deg end. Precision of these $1 servos is far from swiss watch...

This is because you are not doing it correctly.

You seem to be ignoring this advice!
Why?

Degrees is much easier to understand for a beginner.

Two common problems with servos:

  1. Power. Don't power a servo from the weak 5volt pin of the Arduino.
  2. Trying to swing a cheap servo 180 degrees. Keep it first between 30 and 150 degrees.
    Leo..

Yes but she never asked for further details. Which shows she is ignoring the best method of doing things.

What she has is a kludge at the best.

Since when has this forum been about hiding things from beginners? We don't do it with the many things at the same time, when that is the solution do we?

I'm not ignoring it I just don't know what you're talking about and I found a fix that works for my purposes again its just an assignment for my class and I didn't even need to fix the jittering really so finding a fix that just works at all is good enough

A small jitter in any position of the servo is usually a poor quality power source.
So how do you power the servo.
Leo..

So in other words you were not interested in learning, you just wanted some one to do your home work for you. Some would call that cheating. Being a retired UK university lecture, I would certainly say it was.

No one would not call it cheating as I put in the effort to find a fix myself and then decided to reach out to the dedicated forums made by Arduino to get help with an issue I was unable to fix myself, and was given a fix that worked for my purposes so I thanked the person for the help and moved on, I intend on learning more about Arduino coding and how it works, but still intend on following the dedicated curriculum. Your input is appreciated, but I don't currently need the fix you provided.

1 Like

If you don't want to receive more help/suggestions, you should mark your topic solved.

Ok I know you are not a native English speaker, but that is what is called a double negative. Now just like in maths when you multiply two negative numbers together you get a positive number.

So "No one would not call it cheating" is saying everyone would call it cheating.

Which is what I believe you were doing.

By the way we have the option on this forum for individual members to block other members, so that they do not see posts from them.

Welcome to my blocked list.

So for starters I am very much a native english speaker, I know no other languages unfortunately. Secondly that would not be a double negative, just because I forgot a single comma does not invalidate my statement "No, one would not call it cheating". You can block me all you want I don't care. TLDR; I am very much a native English speaker and speak no other languages and just because I forgot a single comma does not invalidate what I said.

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