Motors causing Servo to act erratically

Hi!

I’m having an issue with my servo that I use as a gripper being influenced by the motors when I come to a stop. I’m powering everything through the Arduino. I’ve tried putting diodes and capacitors across the motors but still nothing changes.

The servo works fine when I just use the buttons to open and close, but when I drive it forward and stop then it moves the servo.

I’ll attach the schematic I’m using. Please let me know if you have any ideas or ways to change it so it works!

#include <Servo.h>

Servo myservo;  

void setup()
{
  myservo.attach(9);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);
  pinMode(11, INPUT);
  digitalWrite(11, HIGH);
}

void loop() 
{ 
  if (digitalRead(10) == LOW){
  myservo.write(10);
    }                
  if (digitalRead(11) == LOW)
    {myservo.write(120);}
}

I've tried putting diodes and capacitors across the motors but still nothing changes.

Then you have not put enough and in the right places.

The simplest way is to have a separate power supply for the servos.

However that physical layout diagram ( that is not a schematic ) shows no drivers on the motors. You can not drive a motor directly off an Arduino pin.

Thanks for the reply! Would it work if I ran the motors through the Vin pin instead of sharing the 5V with the servo?

So I tried running the motors from a 4 battery pack and running the servo and buttons from the Arduino supply so they are separately powered and the same thing is happening.

I twisted the motor wires around each other to see if that would help, but still same erratic servo movement.

Could it be Electromagnetic interference being created by all the wires?

Thanks!

Well I figured it out! Thanks!

whitegumby:
Well I figured it out! Thanks!

And the solution was …

It could help others in future.

I’m just messing I hate when people post that.

So having the capacitor, a 104, across the motor helped with the feedback or whatever but the biggest issue was with the code. So my original physical layout diagram is fine my code just needs to look like this: (Added delay(5); before I did the digitalReads)

#include <Servo.h>

Servo myservo;  

void setup()
{
  myservo.attach(9);
  pinMode(10, INPUT);
  digitalWrite(10, HIGH);
  pinMode(11, INPUT);
  digitalWrite(11, HIGH);
}

void loop() 
{ 
  delay(5);
  if (digitalRead(10) == LOW){
  myservo.write(10);
    }
  delay(5);    
  if (digitalRead(11) == LOW)
    {myservo.write(120);}
}