3

I have this setup where I have and Arduino Uno, a mini servo motor, and a pushbutton. I use a single pushbutton to control the servo motor to go to one position, then push it again to go back. It first goes to 0 degrees, then if you push it it goes to 180 degrees then vice versa. The problem is that in its initial state (which is 0 degrees), the servo motor makes noise. Like it's running or something then it gets hot.

I've seen other servo motors that never do this. Here is my code by the way:

#include <Servo.h>

const int buttonPin = 8;
const int servoPin = 9;
int buttonState = 0;
int directionState = 0;
Servo servoOne;
int pos = 0;

void setup() {
  servoOne.attach(9);
  servoOne.write(directionState);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (directionState == 0) {
    if (buttonState == HIGH) {
      directionState = 1;
      for (pos = 0; pos < 180; pos = pos + 1) {
        servoOne.write(pos);
        delay(5);
      }
    }
  } else if (directionState == 1) {
    if (buttonState == HIGH) {
      directionState = 0;
      for (pos = 180; pos > 1; pos = pos - 1) {
        servoOne.write(pos);
        delay(5);
      }
    }
  }
}

I really need all the help I can get.

  • 1
    Can you try setting the initial state to a middle value like 20 or 30? I have these really cheap servos I bought on ebay (no datasheet or anything). Empirically, I've found that my range is less than advertised. I see something similar to what you described when I try writing a degree beyond what my servo is capable of. – krol Nov 6 '15 at 16:49
1

You might need to add more time to the delay. Also, check how your powering the servo. If you're powering from the Arduino, it might be drawing to much current and causing issues. If it's a bigger servo, try to hook it up to an external power source and just connect the PWM line and it's ground.

  • 1
    also be sure to join the ground of the arduino with the ground of your external power supply (+5vdc) – fred_dot_u Jun 4 '16 at 17:43
1

Looks like your delay is too short. If the delay gets over before the servo can reach the set postion, it will skip that command and execute the next one.

Also I agree with @julio about how are you powering your servo. By "mini" servo I assume that you are using a micro servo. I have a Tower Pro 9G micro servo (which I use as steering servo btw, small but pretty decent), and it works fine if I power it with the Arduino UNO, but torque is a bit less. Anyways, check your servo's datasheet, your servo might need more power. Just hook it up to a power supply and make sure the grounds are common. The jitter might just be the result of inappropriate power.

0

The code was absolutely ok but position 180 was a problem sometimes. Those servos simply can't reach position 180 because the end is on 178 or nearby. But the servo was trying to go on that position. The solution is simple: try a different position (179 or less).

0

The most basic issue with servo motors is wrong cabling and then insufficient motor driver in terms of amperage. And then, PWM duty cycle must be set correctly. If you don't treat those things properly, you may hear noises such as humming, vibration instead of turning the motor.

-1

I think you need to do some debouncing on the button.

You can modify the code to suit your project:

/*
 Debounce

 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.


 created 21 November 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 modified 28 Dec 2012
 by Mike Walters

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}
  • 2
    Your assessment is incorrect. The nearly second (180 * 5ms) of delay already present in the code already accomplises substantial debouncing. – Chris Stratton May 25 '17 at 5:13
  • The delay(5) is after servo.write() the debouncing is for the buttons!! I'm not sure what you are referring to. As an electronics and communication engineer, I believe I have a good idea of what I'm talking about! – Amir May 25 '17 at 5:21
  • 2
    You are fundamentally and grossly mistaken. Because this is a serial execution language, once the button is detected high the entire blocking delay, 180 times 5 ms, must elapse before the state of the button will again be evaluated, which is an order of magnitude beyond typical debounce time. When it is seen as low, no action is taken so no debounce is required. Facts are facts, regardless of who you claim to be. – Chris Stratton May 25 '17 at 5:23
  • 2
    No, rather obviously, once it decides to execute the for loop, it will ignore the button until the for loop is complete. Ignoring an input after acting on it, until such time as any contact bounces must have died away, is the simplest and most basic form of debouncing. – Chris Stratton May 25 '17 at 5:30
  • 1
    Nothing happens when the button is set low. But the key is that after the button is set high, the state of the button isn't even considered again for almost a second. If you hold the button down, you can get it to slowly toggle modes, but that may not be undesired, and regardless it has nothing to do with the problem reported in the question. – Chris Stratton May 25 '17 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.