Controlling 9 micro servo motors with arduino & ultrasonic sensor module

Hello!

I am trying to control 9 micro servo motors using an arduino which is triggered by an ultrasonic sensor module. As soon as anything comes within 30cm of the sensor, all servos need to move 180 degrees in sync with each other. I have successfully managed to control 3 micro servos using this code:

#include <Servo.h>

#define trigPin 12
#define echoPin 13

Servo myservo;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(9);

}

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

if (distance >0 && distance < 30)
{
myservo.write(180);
}
else{
myservo.write(0);
}

}

However, I don’t think the circuit has enough voltage necessary to power all 9 motors. I was wondering if anybody had some advice as to how I should go about re-wiring and changing the code? Do I need to add an external power supply? I have so far only used it when connected via USB.

Thank you in advance!

However, I don’t think the circuit has enough voltage necessary to power all 9 motors.

Current, actually

I was wondering if anybody had some advice as to how I should go about re-wiring?

Difficult to say, seeing as you didn’t yet explain how it’s wired.

Do I need to add an external power supply?

Without question- you should do that even for one servo. See attached pic that I had for 3.

servo power.png

OK, I have attached a drawing of the wiring that I’ve been using so far for three servos and an ultrasonic sensor. Obviously, the power source is USB in this instance. Could you suggest what I may need to change in order to power 9 servos?

Thanks

That is not a "drawing" and it is much too large to view conveniently on the screen. Generally a photo of a pencil drawing is more informative than the sort of picture you posted because it can be hard to know what is connected to what inside a breadboard.

In any case... you should have a separate power supply for your servos - even if you only have one of them. Just make sure that the servo GND is connected to the Arduino GND. @JimboZA's diagram illustrates what is required.

...R

Firstly don't try and tell me what a "drawing" is http://en.wikipedia.org/wiki/Drawing here's a useful link just to remind you of the concept of electronic drawing in a 2d format.

Image not too big, you're wrong. It is a jpeg and will adjust to screen size. If you understand a breadboard it is not difficult to comprehend what is connected, you're wrong.

For a supposedly 'open source' community that prides itself on being welcome to newcomers to a young technology such as Arduino which is being introduced in educational facilities for young people to learn and try and understand this is a completely laughable response from two members of said community. If you're not going to help then why are you even replying? You are only creating an embarrassing elitist undertone in what has potential to be an information sharing network.

Finally, you actually haven't answered my question so obviously didn't bother reading my initial post. I am trying to understand how I need to adjust the coding to allow for 9 servos to be used. I apologise if my initial query wasn't clear enough to fit your self-made requirements. I have tried using fritzing which I thought would be useful so I am sorry that wasn't good enough

I answered the part of your post where you asked about wiring. I showed that the servos- no matter how many of them be that 1 or 20- should have their own power supply, and not be powered from the Arduino.

This isn't a help desk- we're all hobbyists like you- members answer what they can in the time available to them. Right now for me it's a 10min lunch stop in a hectic meeting with a colleague.

Your Fritzing shows that your current 3 are powered from the Arduino.... you're probably lucky that it's working and I'd guess that those servos are probably not loaded much and drawing little current. You are at risk of all sorts of servo "funnies" which experience shows, are usually due to inadequate current (not voltage, as I pointed out earlier.....)

So, take my pic of 3 servos- which I drew months ago and not for your case (coincidence that it's 3...) and extrapolate to 9. Point is, the power is external from the Arduino, with the grounds in common though as you can see- and you need a supply that provides enough current.

Forum wisdom is that a servo can draw about an amp, but if there's little mechanical load, well obviously less. If the servos do draw an amp each, you'll maybe need more than one supply. Use as many as you need, just have all the grounds hooked together.

PS, I've never actually measured the current in the signal line to a servo, but I suppose there must be a limit at which you can't control too many servos from the same signal pin on the Arduino. I have no idea what that limit (if any) might be.

Firstly don't try and tell me what a "drawing" is

It wasn't a drawing, it was a Fritzing diagram. Personally, I don't get Fritzing.

Do I need to add an external power supply?

Yes, always. If in doubt, use a multimeter to verify. If you can't find a meter on campus, there's a Maplin in Alderman Judge Mall on Eden Street.

Please, start here

I see. Thanks for the help.

Apologies if my drawing was confusing.

I understand what's being said about the power supply, but still a bit confused about the coding. Not sure what I need to add to what I was using for three servos to make it work for nine.

Cheers

I doubt you have to make any changes to the coding - the fan-out of the pin you're using should easily be sufficient to drive nine servos. If it isn't, just "attach" another servo object to a different pin, and write the same values to it.

je_1s:
Firstly don’t try and tell me what a “drawing” is
http://en.wikipedia.org/wiki/Drawing here’s a useful link just to remind you of the concept of electronic drawing in a 2d format.

Image not too big, you’re wrong.

Yes dear …

Finally, you actually haven’t answered my question

Perhaps you missed the last 2 lines of Reply #3 in addition to the advice in Reply #1

…R

Deary me, thank you for the help!

I have discovered that using a 9V battery doesn't supply the circuit with quite enough current which has confused me further. I have a 12V A/C Power Plug which is a Switching Power Supply. I would preferably like to use the Power Plug over Batteries as it is going to feature in a permanent wall-mounted sign. I read on letsmakerobots.com that using one of these

http://www.hobbyking.com/hobbyking/store/__16663__HobbyKing_8482_HKU5_5V_5A_UBEC.html

will do the job of reducing the Voltage to 5V which I am told is what is required to power the 9 servos. Would you recommend trying this? When I plugged the 12V plug into the Arduino five of the servos worked for a moment but then glitched and stopped working. They aren't broken as I've tested them since, but not sure what I need to do differently. Having real difficulty finding the adequate power supply

Thanks in advance for the help and apologies if I haven't explained clearly enough!

`

That HobbyKing device looks like a good option.

...R

http://www.maplin.co.uk/p/ts2940cz-co-5v-1a-low-dropout-fixed-positive-voltage-regulator-n67ca

this?

For 9 servos you need the the higher current capability of the HobbyKing device. You would probably need one of those Mapin regulators for every 2 or 3 motors. Plus they need other parts to make a functional circuit.

...R

If you are using a 12V supply and regulating it down to 5V, you don't need a low dropout regulator, what you need is a switching regulator.

A linear regulator will be 5/12 = 0.42 therefore 42% efficient. A switch mode regulator will be >90% efficiency.

If you need more current, power supplies like below are available. Servos work better with 6v instead of 5v, but 5v power supplies are common.

https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_323581_-1