Servo Motor Direction Problem

Hello,

I have a single servo hooked up to an UNO. I have 2 buttons connected to the board and I want the different buttons to spin the servo 90 degrees in opposite directions.

Button 1 → 90 degrees clockwise & return to center
Button 2 → 90 degrees counter-clockwise & return to center

I have the code written/compiling/functioning but both buttons trigger the same behavior (always clockwise). I have debug code that confirms I am getting into the correct loops per button click.

Any tips? Thanks!

/* Basic Digital Read
 * ------------------ 
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */

#include <Servo.h>
Servo servoMain;      // Define Servo

int inPinLeft = 2;    // choose the input pin for LEFT button
int inPinRight = 3;   // choose the input pin for RIGHT button
int valLeft = 0;      // variable for reading the LEFT BUTTON pin status
int valRight = 0;     // variable for reading the RIGHT BUTTON pin status

void setup() {
  Serial.begin(9600);
  pinMode(inPinLeft, INPUT_PULLUP);    // declare LEFT pushbutton as input
  pinMode(inPinRight, INPUT_PULLUP);   // declare RIGHT pushbutton as input
  servoMain.attach(10);                // servo on digital pin 10
}

void loop(){
  valLeft = digitalRead(inPinLeft);    // read LEFT button input value
  valRight = digitalRead(inPinRight);  // read RIGHT button input value
  
  servoMain.write(90);                 // Set Servo to center position

  if (valRight == LOW) {              // check if the RIGHT button is pressed
   Serial.println("RIGHT BUTTON");    // Write to debug window
   servoMain.write(0);                // Turn Servo CCW to 0 degrees
   delay(1000);                       // Wait 1 second
   servoMain.write(90);               // Return Servo to center
   delay(1000);                       // Wait 1 second
  
 } else if (valLeft == LOW) {         // check if the LEFT button is pressed
   Serial.println("LEFT BUTTON");     // Write to debug window
   servoMain.write(180);              // Turn Servo CW to 180 degrees
   delay(1000);                       // Wait 1 second
   servoMain.write(90);               // Return Servo to center
   delay(1000);                       // Wait 1 second
    
  } else {
   delay(1000);                      // Wait 1 second   
   
  }
}

I have the code written/compiling/functioning but both buttons trigger the same behavior (always clockwise).

How are you powering the servos? From the arduino is not a good answer. Below is some servo two button test code.

//zoomkat servo button test 12-29-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;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

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

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

How are you powering the servos? From the arduino is not a good answer. Below is some servo two button test code.

Good or not, "from the Arduino" is the correct answer. I followed the tutorial to hook up the servo...you're suggesting I should power it externally?

I know I didn't provide a schematic but I'm not sure what other [u]physical[/u] changes may be required for this to work. For instance, I see you are using INPUT while I am using INPUT_PULLUP so I doubt the code will drop right in.

I'll poke around with this when I have a minute. Thanks!

Motors should NEVER be powered from the Arduino. They create all sorts of problems.

It is also not a good idea to believe everything you read on the internet.

Motors should NEVER be powered from the Arduino. They create all sorts of problems.

https://www.arduino.cc/en/Tutorial/Sweep

So, the tutorial about Arduino on the Arduino site is unreliable?

It is also not a good idea to believe everything you read on the internet.

I guess I'll go elsewhere for help if that's the caliber of assistance I can expect here.

https://www.arduino.cc/en/Tutorial/Sweep So, the tutorial about Arduino on the Arduino site is unreliable?

Yes. That is an unfortunate tutorial, because it suggests powering a servo from the Arduino. The author clearly does not understand the problems this can cause.

A glance through this forum will reveal the hundreds, if not thousands of times people have posted your very complaint.

Good luck!

yugffuts: https://www.arduino.cc/en/Tutorial/Sweep

So, the tutorial about Arduino on the Arduino site is unreliable?

I guess I'll go elsewhere for help if that's the caliber of assistance I can expect here.

Sounds like a good plan for you. Bye! ;)

:)

The reason I'm posting here in the first place is because there is a ton of contradictory information out there. My original question was not about external power; it was about servo spin direction. However the conversation has drifted to external power & I'm trying to understand it all.

I'm reading the replies so far as "Servo behavior [u]could[/u] be adversely impacted by using the arduino power directly. It would be best for you to power the servo externally and see if the problem persists before making any coding changes." That sound about right?

Thanks.

yugffuts: :)

The reason I'm posting here in the first place is because there is a ton of contradictory information out there.

Thanks.

Correct , the information you get here is much better.

A bad answer will be quickly shot down.

Instructables can be very bad , just because someone has made something that works does not make it good practice or suitable for all cases.

yugffuts: :)

I'm reading the replies so far as "Servo behavior [u]could[/u] be adversely impacted by using the arduino power directly. It would be best for you to power the servo externally and see if the problem persists before making any coding changes." That sound about right?

Thanks.

Yes , any inductive load can impress spikes on the power rail causing any number of glitches

yugffuts: :)

"Servo behavior [u]could[/u] be adversely impacted by using the arduino power directly.

Thanks.

I would change could for would generally.

jremington: Yes. That is an unfortunate tutorial, because it suggests powering a servo from the Arduino. The author clearly does not understand the problems this can cause.

A glance through this forum will reveal the hundreds, if not thousands of times people have posted your very complaint.

Good luck!

I reckon 95% of problems with servos here are inadequate power or sharing power with the Arduino itself. That tutorial is wrong, and none of us can get it fixed which is frustrating.

Thanks for the feedback. However, now I am wary of instructables in general and I have incredibly limited knowledge of how to wire up the servo to external power.

I have a single 4.8~6.0V servo that I need to power. Can anyone point me to a reliable lesson/walkthrough?

My original question was not about external power; it was about servo spin direction. However the conversation has drifted to external power & I'm trying to understand it all.

It is not obvious to me why your program doesn't work correctly.

However, just about any nonsense can happen, including destruction of the Arduino board itself, if you have a motor being powered by the Arduino. For that reason, I and other forum members usually recommend to fix the power problem first.

Power the servo from the + pole of a 4.8 - 6V battery pack, and connect the - battery pack pole to the servo ground AND to the Arduino ground.

Seems simple enough. Thanks.

What you describe will power the servo…the Arduino itself still gets power over USB?

The Arduino itself still gets power over USB?

If that is how you are powering it, yes. There are other options.

Thank you sir!

I hooked up the 4 AA batteries to the servos as you described & powered the Arduino itself over USB. The servo is considerably faster/stronger on battery power & the servo is triggering & behaving properly. No code changes were required.

I’ll be back with more questions (hopefully soon) but this solves the current challenge.