2 servos 1 pin

Inexperienced user here. I usually use a prop specific controller but decided this was the year I learned more about Arduino. I'm animating some skeletons, and for the heads I've mounted 2 servos being randomly turned to give the effect they are looking around while opening/closing their mouths. Hardware is working 100%, but when I try to connect to servos to the one output pin they both kind of freak out and eventually just twitch. If they are plugged in independently they function perfectly (with the exception of turning a bit too far each way). I've tried to install a 10k resistor between each signal wire with no change. I either need a second output pin doing the same thing as pin 10, or to find what is happening with these servos "seeing" each other.

If I've made some rookie mistakes, don't be afraid to let me have it. I've been searching for answers for about 2 hours now, just can't crack this.

Here's the current sketch:

#include <Servo.h>
Servo myservo;
unsigned int duration = 0;
int lasttime1 = 0;
int lasttime2 = 0;
int randstart = 0;
int randend = 0;
int pos = 0;
void setup()
{
myservo.attach(10);
Serial.begin(9600);

}
void loop()
{
for(pos = randstart; pos < randend; pos += 1)
{
myservo.writeMicroseconds(pos);
delay(1);
}
for(pos = randend; pos>=(randstart+1); pos-=1)
{
myservo.write(pos);
delay(1);
}
randstart = random(200, 800);
randend = random(800, 2400);
}

Hi FridayThe13th,
I have never tried running two servos off of one signal, but I think I can give you some pointers as to where to go next:

  1. Format your code like this using code tags
  2. Have you tried running the servos with one of the included Servo library examples like knob or sweep? This would help eliminate any code errors.
  3. Double-check your wiring, it has tripped me up before.
    Good luck,
    John Wolf
    Progetto Company

Thanks for the reply, I fixed the code.

I tried the servos with sweep to make sure they were working properly, but not wired together. They each work perfectly when connected alone, the second I attach the second to the breadboard it acts strange. I don't know if it's possible to duplicate the sketch and have it work on pin 9 as well.

Have you got all the grounds connected?

Are you powering the servos from the Arduino ? The Arduino cannot supply enough power for two servos - it’s very marginal and not good practice even with one.
Also ... why not drive them from seperate pins?

I have an external power supply. 5v 1a. I want both to do the exact same thing, I figured connecting them to the same pin would accomplish this. If I can modify the code to have both pins doing the same thing, that's fine too.

And yes, all grounds are tied.

Your external supply may be too weak, or you may have wired ground loops, or bad breadboard contacts.

DrDiettrich:
Your external supply may be too weak, or you may have wired ground loops, or bad breadboard contacts.

You nailed it. The power supply, although it should be large enough, wasn't up to the task. Swapped to a 5v 3a and it works perfectly. Thanks for that!

Is there a way to slow the speed down slightly, like 10%?

You can try to hack the servo for lower speed, but that's not very practical. Better implement slow moves in code, but consider the servo signal rate of typically 20ms. You cannot make small steps faster than this.

FridayThe13th:
Is there a way to slow the speed down slightly, like 10%?

What speed?

Change delay(1) to a different number.

Better still, remove the delays entirely and use millis() for timing.

Thanks for the replies. I changed the delay and that helped, but after assembling the whole project I am curious if it's possible to have pin 9 running it's own servo on a second random sweep? The way it runs now the skeleton opens his mouth according to head position. Was hoping to have more of an eerie opening and closing mouth effect at all times while the head does it's own thing.

I've tried to modify the code and made a total mess of it. I just don't have to time to learn to code this before Halloween. I've been using pre-made programmers on all my previous projects with a totally different interface and it's become a crutch. :slight_smile: The kids in our neighborhood thank you in advance.

Yes, entirely possible.
Take a look at Robin2's useful tutorial