Troubles with my servos

Hey!
I’m new to Arduino and I’m trying to run 4 servos (soon to be 5)

Each servo has 2 push buttons - One for clockwise and the other for counterclockwise.

I’m getting a lot of ‘jittering, vibrating’ and also the push buttons seems to only work sometimes.

I wanted to know if its a problem with the power supply, layout or code?

Any help will be much appreciated

#include <Servo.h>

// servo1

const int buttonPin = 14;
const int buttonPin2 = 15;
int buttonState = 0;
int buttonState2 = 0;
Servo servo1;


// SERVO2

const int buttonPin3 = 16;
const int buttonPin4 = 17;
int buttonState3 = 0;
int buttonState4 = 0;
Servo servo2;



// SERVO3

const int buttonPin5 = 7;
const int buttonPin6 = 6;
int buttonState5 = 0;
int buttonState6 = 0;
Servo servo3;


// SERVO4

const int buttonPin7 = 8;
const int buttonPin8 = 9;
int buttonState7 = 0;
int buttonState8 = 0;
Servo servo4;


// SERVO 5

const int buttonPin9 = 11;
const int buttonPin10 = 13;
int buttonState9 = 0;
int buttonState10 = 0;
Servo servo5;



int pos = 90;
int pos2 = 90;
int pos3 = 90;
int pos4 = 90;
int pos5 = 90;



void setup() {

  Serial.begin(9600);
  servo1.attach(2);
  servo2.attach(3);
  servo3.attach(5);
  servo4.attach(10);
  servo5.attach(12);


  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  pinMode(buttonPin9, INPUT);
  pinMode(buttonPin10, INPUT);


  digitalWrite(buttonPin, HIGH);
  digitalWrite(buttonPin2, HIGH);
  digitalWrite(buttonPin3, HIGH);
  digitalWrite(buttonPin4, HIGH);
  digitalWrite(buttonPin5, HIGH);
  digitalWrite(buttonPin6, HIGH);
  digitalWrite(buttonPin7, HIGH);
  digitalWrite(buttonPin8, HIGH);
  digitalWrite(buttonPin9, HIGH);
  digitalWrite(buttonPin10, HIGH);

  servo1.write (pos);
  servo2.write (pos2);
  servo3.write (pos3);
  servo4.write (pos4);
  servo5.write (pos5);

  delay(1000);
}



void loop() {

  // serial //

  Serial.print(buttonState);
  Serial.print(buttonState2);
  Serial.print(buttonState3);
  Serial.print(buttonState4);
  Serial.print(buttonState5);
  Serial.print(buttonState6);
  Serial.print(buttonState7);
  Serial.print(buttonState8);
  Serial.print(buttonState9);
  Serial.print(buttonState10);



  // buttonstate //

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
  buttonState7 = digitalRead(buttonPin7);
  buttonState8 = digitalRead(buttonPin8);
  buttonState9 = digitalRead(buttonPin9);
  buttonState10 = digitalRead(buttonPin10);


  /////// MOVEMENT ////////

  // servo1 //

  if (buttonState == LOW) {
    pos = (pos + 1);
    servo1.write(pos);
    Serial.println(buttonState);
    delay(15);

  }

  if (buttonState2 == LOW) {
    pos = (pos - 1);
    servo1.write(pos);
    Serial.println(buttonState2);

    delay(15);

  }

  // servo2 //

  if (buttonState3 == LOW) {
    pos2 = (pos2 + 1);
    servo2.write(pos2);
    Serial.println(buttonState3);
    delay(15);

  }

  if (buttonState4 == LOW) {
    pos2 = (pos2 - 1);
    servo2.write(pos2);
    Serial.println(buttonState4);

    delay(15);

  }

  // servo3 //

  if (buttonState5 == LOW) {
    pos3 = (pos3 + 1);
    servo3.write(pos3);
    Serial.println(buttonState5);
    delay(15);

  }

  if (buttonState6 == LOW) {
    pos3 = (pos3 - 1);
    servo3.write(pos3);
    Serial.println(buttonState6);

    delay(15);

  }


  // servo4 //

  if (buttonState7 == LOW) {
    pos4 = (pos4 + 1);
    servo4.write(pos4);
    Serial.println(buttonState7);
    delay(15);

  }

  if (buttonState8 == LOW) {
    pos4 = (pos4 - 1);
    servo4.write(pos4);
    Serial.println(buttonState8);

    delay(15);

  }

  // servo5 //

  if (buttonState9 == LOW) {
    pos5 = (pos5 + 1);
    servo5.write(pos5);
    Serial.println(buttonState9);
    delay(15);

  }

  if (buttonState10 == LOW) {
    pos5 = (pos5 - 5);
    servo5.write(pos5);
    Serial.println(buttonState10);

    delay(15);

  }

}

How are the inputs wired ?
Have you got pullup or pulldown resistors in place or are the inputs floating at an unknown voltage ?

How are the servos powered ?

The code would be neater and much shorter if you used arrays but get it working as it is first.


How to post an image.

Is the battery powering the whole setup really a 9V smoke alarm battery? Are you really powering the servos from the Uno?

9V smoke alarm batteries do not have the current capability to power a servo. The Uno regulator will be overloaded by a servo and may shut down. Feeding 9V to the 5V input is a sure way to kill an Uno. Connect the 9Vbattery to the Vin input* and provide a separate power supply for the servos. A 4 AA or AAA pack will provide enough current for the servos if they are not all powered at once.

*The 9V battery won't last more than a few hours.

*The 9V battery won't last more than a few hours.

The 9V battery probably won't last more than a few minutes.

groundFungus:
Is the battery powering the whole setup really a 9V smoke alarm battery? Are you really powering the servos from the Uno?

9V smoke alarm batteries do not have the current capability to power a servo. The Uno regulator will be overloaded by a servo and may shut down. Feeding 9V to the 5V input is a sure way to kill an Uno. Connect the 9Vbattery to the Vin input* and provide a separate power supply for the servos. A 4 AA or AAA pack will provide enough current for the servos if they are not all powered at once.

I'm using 6X AA Batteries (1.5V ea)

So I plug the positive battery wire to the Vin? and then the negative to GND on the Arduino?
Sorry, I'm still learning, so I don't know the best way to do things yet.

My resistors are 10K

iiSeeStarlight:
I’m using 6X AA Batteries (1.5V ea)

So I plug the positive battery wire to the Vin? and then the negative to GND on the Arduino?
Sorry, I’m still learning, so I don’t know the best way to do things yet.

My resistors are 10K

Please reduce this to 4 batteries. As others have stated 9V (6 x 1.5V) will damage your Uno and probably your servos (if they are as shown SG90 micro servos, the spec for which states a nominal supply voltage of 4.8V).

The best solution would be to disconnect the battery pack positive lead from the Arduino and power it from the USB or it’s 2.1mm power jack (if you use the power jack you must supply at least 7V). This will isolate any electrical noise generated on the power rail by the servos from the Uno.

You must retain a common GND connection. That is the GND pin on the Uno should be connected to the battery pack negative.

You might want to consider switching to rechargeable NiMH AA batteries rather than alkaline as their slightly lower terminal voltage (1.2 - 1.4V rather than 1.5V) will bring the supply for the servos closer to their suggested 4.8V (this is assuming you are using SG90 micro servos).

Ian

9V to Vin is fine. But 9V is too much for the servos directly and you can't rely on the tiny onboard regulator to supply 5V to 4 or 5 servos so you can't use the Arduino 5V pin for them. You need a separate supply for the servos, 3 or 4 x AA batteries would probably be o.k.

Steve

IanCrowe:
Please reduce this to 4 batteries. As others have stated 9V (6 x 1.5V) will damage your Uno and probably your servos (if they are as shown SG90 micro servos, the spec for which states a nominal supply voltage of 4.8V).

The best solution would be to disconnect the battery pack positive lead from the Arduino and power it from the USB or it's 2.1mm power jack (if you use the power jack you must supply at least 7V). This will isolate any electrical noise generated on the power rail by the servos from the Uno.

You must retain a common GND connection. That is the GND pin on the Uno should be connected to the battery pack negative.

You might want to consider switching to rechargeable NiMH AA batteries rather than alkaline as their slightly lower terminal voltage (1.2 - 1.4V rather than 1.5V) will bring the supply for the servos closer to their suggested 4.8V (this is assuming you are using SG90 micro servos).

Ian

Sorry, I'm using the MG996R

iiSeeStarlight:
Sorry, I'm using the MG996R

A quick look at Tower's website here:-

Shows an operating voltage range for the MG966R as 4.8 ~ 6.6V. Nine volts is substantially over that and will not do the servos any good. It might also explain the jitter you are seeing.

Also you use the switches to pull the inputs of the Arduino up to your battery voltage. These pins on your Uno board connect directly to ATmega328P. If you read the spec sheet for the 328P here:-

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf

you will see the maximum supply voltage (Vcc) is 6V and absolute maximum voltage on any pin except Reset is Vcc+0.5V. (Page 365 of the datasheet).

Overall it would be a very good idea to switch to using 4 x AA NiMH batteries.

Ian

IanCrowe:
A quick look at Tower's website here:-

http://www.towerpro.com.tw/product/mg995-robot-servo-180-rotation/

Shows an operating voltage range for the MG966R as 4.8 ~ 6.6V. Nine volts is substantially over that and will not do the servos any good. It might also explain the jitter you are seeing.

Also you use the switches to pull the inputs of the Arduino up to your battery voltage. These pins on your Uno board connect directly to ATmega328P. If you read the spec sheet for the 328P here:-

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf

you will see the maximum supply voltage (Vcc) is 6V and absolute maximum voltage on any pin except Reset is Vcc+0.5V. (Page 365 of the datasheet).

Overall it would be a very good idea to switch to using 4 x AA NiMH batteries.

Ian

Thanks Ian!
I will definitely purchase some new batteries tomorrow.
Quick question, Did I wire the servos pos and neg to the breadboard correctly or is there another way to do it?

Chantelle

iiSeeStarlight:
Thanks Ian!
I will definitely purchase some new batteries tomorrow.
Quick question, Did I wire the servos pos and neg to the breadboard correctly or is there another way to do it?

Chantelle

Hi Chantelle

Yep they are wired correctly.

A quick check here:-

https://www.servocity.com/servo-connector-types

shows the following colour code for a JR connector -

Brown = 0V/-ve/Gnd
Red = Supply (4.8-6.6V).
Orange = Control (Standard servo signal).

All servos should have their supply(Red) and ground(Brown) wires in parallel as you show in your diagram.

With a 6V (4 x AA) battery pack you can get away with the switches pulling up to the battery rail. However it would be better to wire them to the 5V pin on the Arduino.

An even easier solution which uses less components is to wire the switches between the Arduino input pins and ground then change your Setup() code from this:-

  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  pinMode(buttonPin9, INPUT);
  pinMode(buttonPin10, INPUT);

To this:-

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  pinMode(buttonPin5, INPUT_PULLUP);
  pinMode(buttonPin6, INPUT_PULLUP);
  pinMode(buttonPin7, INPUT_PULLUP);
  pinMode(buttonPin8, INPUT_PULLUP);
  pinMode(buttonPin9, INPUT_PULLUP);
  pinMode(buttonPin10, INPUT_PULLUP);

This connects internal 10k pull-up resistors between the input pins and the Arduino's +5V supply.

You now don't need your pull-down resistors. However you will need to change your loop() code to look for LOWs rather than HIGHs for an activated switch.

Hope this helps.

Ian

iiSeeStarlight:
Quick question, Did I wire the servos pos and neg to the breadboard correctly or is there another way to do it?

It is not a good idea to power servos, particularly high-powered ones, via a breadboard. Many breadboards have relatively high resistance and are not capable of handling the high currents involved.

If you can wire the servo pos and neg directly to the battery you will have a much better chance of avoiding jitter and other power problems.

Steve

I wanted to know if its a problem with the power supply, layout or code?

i think the problems is the supply, if you use more than 3 servo, the supply from arduino isn't enough. you need external power supply.

generally, servo motor needs +9V power supply.

connect the 9V external powersupply to +- pin on the breadboard, and then connect GND pin from the arduino to the - breadboard.

nielyay:
generally, servo motor needs +9V power supply.

NO! That is terrible advice.

Most servos, including the MG996R, need around 5-6V supply. Powering a normal hobby servo from 9V is almost guaranteed to damage it.

Steve