Question about servos

Hello all, I just received a kit in the mail and I started to tinker with it.

My end goal was to to make the servo rotate and the LED light up when the button was pushed. Then on release, the servo will reset to where it started.

From the code I have here, I assumed that would work, but it does not. When loaded the servo spins full rotations.

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    myservo.write(180); 
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    myservo.write(90); 
  }
}

Now from what little I know about servos, servos should not even rotate a full circle.

The servo I am using is a super cheap, if I break it, it's okay TowerPro SG90 9G Mini Servo. All the information I can find on this servo is that you need to "hack" it to make it spin full rotations, so I am confused.

This all may be irrelevant because I have a Futaba at home that I will try next, but I didn't want to break it testing code. XD

Sorry for what is probably such a basic answer. I did search, and all the answers pointed to the servo being a full rotation "servo", but this is apparently not the case with this one? Maybe..

Thanks! :slight_smile:

Does the servo rotate continuously through 360 degrees ?

If so then it is not a servo at all.

Yea, I gathered that. That is why I was very confused with all the tutorials online to "hack it" into spinning 360 degrees. I will check with my real servo in a few hours, but other than that, is the logic in my code sound for what I am trying to do?

is the logic in my code sound for what I am trying to do?

Can you please post the complete program ? How is the button pin wired ? Do you have a pulldown resistor on the input pin to keep it in a known state ?

One note of caution. How is the servo powered ?
It should not be powered from the 5V pin on the Arduino as the voltage regulator can only provide a limited amount of current and could be damaged if the servo draws too much.

Turns out a common ground between the Arduino and the power source helps...

Sorry :slight_smile: