Different behavior with different power sources

Hello, rookie here in need of some assistance.

I am building a little robot for demonstration purposes, which is nothing more than five microservos (TS90A and MS18) following a repetitive set of commands to pick up a ball and place it at the top of a ramp, repeat. I have it running this cycle each time I press a button.

When connected to the computer via USB, the program runs just fine and the robot moves exactly how I want it to. When I connect it to a 9Vdc 300mA power supply plugged into the wall, it behaves completely differently, and without consistency. It jerks around and moves very slowly through motions that are not a part of the code.

I ultimately want to be able to plug this thing into the wall and let it run this simple demonstration on loop over and over. Why the different behavior from the USB to the DC power supply? Any advice would be appreciated.

#include<VarSpeedServo.h>
int switchState = 0;

//define variables and constants
VarSpeedServo robotBase;
VarSpeedServo robotClaw;
VarSpeedServo bigArm;
VarSpeedServo littleArm;
VarSpeedServo gate;

void setup() {
  robotBase.attach(10);
  robotClaw.attach(9);
  bigArm.attach(6);
  littleArm.attach(11);
  gate.attach(5);
  pinMode(2, INPUT);
} //end of setup()

//loop function
void loop() {
  switchState = digitalRead(2);

  if (switchState == HIGH) {
     robotBase.write(155,60,true);
     bigArm.write(60,100,true);
    delay(1000);
     littleArm.write(90,60,true);
     bigArm.write(108,60,true);
    delay(1000);
    gate.write(180,100,true);
    delay(2000);
     robotClaw.write(0,60,true);
    delay(1000);
    bigArm.write(60,60,true);
    littleArm.write(120,60,true);
     robotBase.write(112,60,true);
     littleArm.write(180,60,true);
     bigArm.write(120,60,true);
    delay(1000);
    gate.write(90,100,true);
     robotClaw.write(50,60,true);
    delay(1000);
     bigArm.write(60,60,true);
    delay(1000);
  }
} //end of loop()

Ppl are generally advised to supply the servos with adequate power.

I think this is a classic example of not doing.

You could measure the current drawn and see. You might also see the voltage on the servo power leads sag. Take a look.

Solution: a good 5 volt power brick or wall wart with "enough amps" to more than supply the maximum current required.

Not the code prolly.

a7

Guaranteed…
I can see your servo 5V is taken from the Arduino regulator.

That’s a no-no.
I’m surprised the Arduino isn’t resetting itself.

Once you get this working properly with a separate servo supply, if you want to move on with more complex interactive projects, look into millis() timing to sequence your servos.

You’ll be able to do a lot more interesting overlapping motion, and other effects while the servos are running.

And an all-too-common "rookie" mistake. :grin:

For any "real" project - one which uses parts other than the UNO itself, just forget "Vin" and the "barrel jack". The on-board regulator is basically quite useless.

You need a 5 V power supply - 5 servos suggest something approaching 5 Amps. Connect that to your servos, and also to he "5V" pin on the Arduino except when you plug it into a PC via USB. Always have the grounds connected together. :+1: