CONTROLLING SERVO WITH BUTTON

Hi. I am trying to control a servo with a button and have no luck. Below is my code. The result, while plugged into the USB which goes to the computer, is a humming of the servo at a constant pitch as the button is read as LOW(nothing is supposed to happen, especially in the code), but when I do click, then hold down the button, I can hear the hum of the beat to the servo going from 180 degrees to 75 degrees with delay. When I use my 9V cord and plug it into the wall, nothing happens, also no hum. Is the error i the code or is it possible I blew the servo :slightly_frowning_face: ::slight_smile: :cry: . I have heard that you should not use servos in a circuit while the arduino is plugged into the USB to the computer.

#include <Servo.h>
Servo eqo;
void setup() {
  
  pinMode(4, INPUT_PULLUP);
  eqo.attach(5);
}
void startServo(int ethan) {
  eqo.write(180);
  delay(ethan);
  eqo.write(75);
  delay(ethan);
}
void loop() {
  while (digitalRead(4) == HIGH) {
    startServo(500);
  }
}

Try this example:

I would use an external power supply (make sure you connect the power supply ground to the Arduino ground).

You do not power servos from the arduino. They need a separate power supply with proper grounding.

Simple servo button code.

//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Ok. Thank you guys.

LarryD:
I would use an external power supply (make sure you connect the power supply ground to the Arduino ground).

!!

Duh! That's why the little servo that came with my "fun way to learn Arduino" kit never worked right! Maybe. I should try it out again, get the sweep demo happening.

The servo examples drive most servos past their physical limits. You should include appropriate limits when you attach the servos.

You'll get better control from servos if you use writeMicroseconds rather than using degrees. The "degrees" used in code very rarely corresponds to actual angular position of the servo.

The conversion from microseconds to angles varies between servo models. You will be able to predict a servo's position with much greater accurately if using your own conversion algorithm rather letting the Arduino pretend it's making this conversion.

DuaneDegn:
The servo examples drive most servos past their physical limits. You should include appropriate limits when you attach the servos.

You'll get better control from servos if you use writeMicroseconds rather than using degrees. The "degrees" used in code very rarely corresponds to actual angular position of the servo.

The conversion from microseconds to angles varies between servo models. You will be able to predict a servo's position with much greater accurately if using your own conversion algorithm rather letting the Arduino pretend it's making this conversion.

Or you can set the "0 degrees" and "180 degrees" limits in microseconds in the Servo.attach method, and still let the Servo objects do the scaling for you.