Author Options:

Controlling 9 Micro Servos with Arduino & Ultrasonic Sensor Answered


I'm new to Arduino and trying to build a series of moving cogs attached to micro servos, which are triggered by an Arduino controlled Ultrasonic Sensor. My code so far looks like this

#include <Servo.h>

#define trigPin 12
#define echoPin 13


// macro just adds two - the first servo is attached to digital pin 2, this gives us upto 12 servos - digital 2 to 13
#define SERVO_TO_PIN(x) (x+2)


Servo myservo;

void setup() {
  Serial.begin (9600);  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // attach the servos
  for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)

void loop() {
int duration, distance,pos=0,i;
  digitalWrite(trigPin, LOW); 
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
   Serial.println(" cm");

  for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)
    if (distance >0 && distance < 30)
But it isn't really the code that I am having a problem with. I know that I need an external power supply for the 9 servos but I am having difficulty figuring out what is best. I have tried attaching a 9V battery to the power and ground lines of the servos and it hasn't quite supplied enough, only making them move a little when i get closer to the distance sensor, then jittering about a bit before completely stopping. I have got a Switching Power Supply plug that has an output of 12V which I can use and plug directly into the Arduino? I also have voltage regulators that have an output of 5V available. Could somebody recommend how to wire these supplies up for the servos?

Any help would be much appreciated! 


6 Replies

j_olo (author)2015-01-27

Thanks Honus and Downunder35m that's really helpful. I have now got a Switched mode AC/DC adaptor to use for the servos and a separate 5v power source for the arduino. That video was helpful for knowing the rough layout of a circuit but I guess I'm just confused as to whether I need to use a transistor or not or if I can send the 5v straight to the servos?


Select as Best AnswerUndo Best Answer

Honus (author)j_olo2015-01-27

You don't need a transistor. Just send power directly to the servos and make sure the Arduino and servos share a common ground.

Select as Best AnswerUndo Best Answer

Honus (author)2015-01-26

What you want is to power your servos with a separate clean power supply. Powering an Arduino and servos from the same source can cause a lot of problems due to the electrical noise the servos generate.

The first method is to power your servos from a separate battery (or wall power supply) with the proper voltage (4.8v to 6v) and make sure the power source can deliver enough current. 9V transistor batteries have very high internal resistance and cannot deliver more than a couple hundred milliamps- they are a poor choice for powering servos but they are fine for powering most Arduino boards.

The second method is to power everything from a single power source but use a voltage regulator to provide power to the Arduino while the servos are powered straight from the power source. Using a voltage regulator will isolate the Ardunio's power supply. The trick with this is that the Arduino has to operate at a lower voltage than the servos. You can do the opposite and have the servo power supplied by the regulator but the regulator must be capable of delivering enough current for all of your servos, especially if they are all moving at the same time.

No matter which method you choose, the servos and the Arduino must share a common ground.

Here is a video that may help you with your problem-

Select as Best AnswerUndo Best Answer

j_olo (author)2015-01-26

hmm OK. Would it be easier if I used only 5 servos? As that would be possible with my mechanism

Select as Best AnswerUndo Best Answer

Downunder35m (author)j_olo2015-01-26

It depends on the power for the servos.
And with under 20 bucks a dedicated shield for the job is always a good option.
In case you want to without a shield I strongly suggest to a good 5V power supply to run the Arduino AND to connect the 5V supply of the servos to.

If you use the 5V of the Arduino alone it will either shut down due to overload or even fry the 5V regulator.
Just to be clear with the connections:

5V power supply positive goes to the Arduino AND the servos, negative goes to the Arduino AND the servos, PWN of the servos goes directly to the Arduino.

This way you can drive as many servos as you can find outputs on the Arduino for.
Just check the datasheet for your servos to get the right power rating and a power supply to fit.
Since you are talking about micro servos a 5V, 3A power supply should be enough, don't use cheap phone chargers though as most won't handle sudden increases in the output current and also have the problem of harmonics over the DC voltage, which can cause problems on the Arduino.
If in doubt and short on money you can add a 4700µf electrolytic capacitor and a 10nF ceramic capacitor parallel to the power supply, add a nice inductor in series on the positive between power supply and first cap, any toroid core inductor from an old power supply or filter should work fine.

Select as Best AnswerUndo Best Answer

Downunder35m (author)2015-01-26

The best option is to use a servo shield as it can supply the currents you need from a suitably supply, the Arduino itself should not be forced to supply that much current directly and unprotected

It also helps to know the power levels of the servos as you need a supply that is strong enough, for example:

Each servo uses a max of 220mA under full load.

Times 9 this equals to 1.98amps, or 2amps to have a round number.

9V batteries won't supply that much current.

You also need a supply voltage that is suited for the servos, usually they run on 5-6 volts.

Check something like this


for your servos and I think it might work much better, your 12V supply is suitable if it produces enough current for the servos.

Select as Best AnswerUndo Best Answer