Wednesday, March 28, 2012

How To Connect A Servo To Arduino


Servo problems seem to be one of the most commonly recurring topics in the Arduino world. This post will address some of the most common pitfalls in driving one or more servos from an Arduino.

   1) The Basics
   2) Code
   3) Power
   4) Multiple Servos - High Performance Battery Power

For driving multiple servos, as a first step to trouble shooting its still worth checking each servo individually with the procedure outlined in the basics below -

1) The Basics

Lets start with the real basics - does your servo work with the Sweep example ? If so skip to part 2), if not,  still skip to part 2), if your not sure because you haven't tried it, read on and try now.

The sweep example is in the Arduino IDE, check the 'examples' menu, under 'servo' you should see two examples, 'knob' and 'sweep'. Load sweep and upload it to your Arduino.

Disconnect your Arduino and build the following circuit before reconnecting it to power.

Using a bread board - 

1) Connect the red wire from the servo to the 5V pin of your Arduino.
2) Connect the black ground wire from the servo to one of the ground (GND) pins on your Arduino
3) Connect the signal (normally white or yellow) wire from the servo to pin 9 of your Arduino

Note that in the picture below I have used a 3-Pin male header as an easy way to connect the female servo to the female bread board.



Now add power and your servo should be sweeping back and forth. If not, check 1-3, double check 1-3 and try again. If your still not having any success, is your circuit stripped back so that it is just the Arduino and the servo as in the picture ? If it isn't try stripping it back and just test the Arduino and the Servo on thier own.

If it is still not working with just the Arduino and servo connected, it is possible that your servo is damaged or pin 9 is damaged.

Try another servo or edit the following line in the code to use another pin -

myservo.attach(9);  // attaches the servo on pin 9 to the servo object

Disconnect your servo from pin 9 and connect it to the new pin. If its still not working, the servo may be damaged.

To test multiple servos simultaneously use the multi sweep code here -

http://rcarduino.blogspot.com/2012/01/can-i-control-more-than-x-servos-with.html


One of the reasons for a lot of the confusion is that servos are quite happy working with the 5 Volts available from an Arduino, but what is most often overlooked is the Amps. Servos like Amps, lots of Amps.

Many Arduinos are powered through onboard regulators or USB sockets that are limited to one Amp or less. This is sufficient to drive an Arduino and one servo that is not acting against a load. When this same servo is installed in a project with other components or is acting against a load, the project as a whole may drawn several amps at which point the Arduino will at best brown out.

If your servo passes the test suggested in part 1) The Basics, but still causes problems in your project it could be down to 1) Your wiring, 2) Your code, 3) Your power supply.

2) Your Code


To determine if the cause is power or your code try the following -

Add A Test Servo To Your Code. 

This test servo will tell us whether we have sufficient power, or the problem is in the code.


To do this, copy the line of code that creates your original servo and edit it by adding the word test -

Servo myServo;
Servo myServoTest;

Now in your setup function copy the line that attaches your original servo and edit it to copy the name of our test servo and change the pin number to a free pin -

myServo.attach(9);

myServoTest.attach(10);

At this point we have created a new servo within our code and the servo library will be doing its job by instructing the test servo to maintain the center position. We will use this as a test.

Disconnect the servo signal wire from pin 9 (or whatever pin its connected to in your project) and reconnect it to the test servo pin (pin 10 in our case).

If you project still misbehaves, its is either your wiring or your power supply. If however your project remains stable and the servo resists pressure to turn -i.e. is on and centered then the problem is very likely in your code.


3) Power Supply

If you have reached this point the possibilities are likely 1) Wiring 2) Power Supply

At the start of part 2 we discussed how servos within a project can combine with the project as a whole to draw more current than the Arduino Regulator or USB socket can supply.

One common approach is to try and power the Servo directly from a battery, while the basic idea is correct, there are some common mistakes here -

Common Battery Pitfalls -

1) The servo power supply and the Arduino power supply must share the same ground connection. If the two power supplies are not connected to a common (shared) ground, your project will not work. This is a very common mistake, so check yours now.

Refer to the previous picture if you need to.

2) Servos are normally rated to work with 4.8 to 6 volts. A common mistake is to try and power a servo directly with a 9v battery, this can damage the servo as unlike the Arduino, the servo does not have an on board regulator to bring this down to a usable 5 Volts.

There are several low cost and low performance options here, one is to build a quick regulator circuit using a 7805 regulator or similar. This will give upto 1 Amp at 5 volts and can be driven from a 9V Square battery. 

Its okay for simple projects, but low cost, low performance. Don't forget to connect the (-) ground of the regulator with the ground of the Arduino.

Another option is to build a small dedicated battery pack from 4 or 5 AA rechargeable nimh batteries ( 4 * 1.2 = 4.8 Volts, 5 * 1.2 = 6 Volts) or 4 AA high power batteries (4 * 1.5 = 6 Vlts) , again this is low cost and low performance and can drive the servo directly provided it shares a common group with the Arduino.

Another option is a dedicated high performance battery, if your project includes several servos or shields you might have no choice but to take this option. If your project does not need to be portable an adjustable lab power supply is the best option and you can stop reading here.


High Performance Battery Power

If your planning a mobile or portable project that you know will require reasonable power you might want to consider this next option.


 8.4 Volt LIPO Battery from my RC Race cars and a corner store 9V Square Battery.


Servos can require more current (remember those Amps) than a corner store battery can deliver. In the world of radio controlled models where high currents are required for racing and flight, the preferred battery technologies are NIMH and LIPO. Unfortunately these batteries tend to be available in odd voltages like 7.2 and 8.4.

As we discussed under 'Common Battery Pitfalls' connecting a standard servo to voltages higher than 6 Volts can cause permanent damage. In fact when 8.4 volt LIPO batteries first become popular a lot of people who had been getting away with running their servos at lower voltages suddenly found themselves with burnt out servos.

There are readily available solutions for getting a stable 5 volts of high current from these performance batteries in the form of 'regulators'. These regulators differ from the one supplied on the Arduino board in that they are designed to supply enough current to drive multiple servos. A quick scan of an online hobby store shows one available for 12 dollars that supplies upto 6 Amps, enough for at least 6 Servos, many more if the loads will be light.

One Battery, One Regulator, Lots of Servos, Add Arduino and Go !


At this point, you may be thinking, yes but I don't want to invest in a regulator or battery until I know my project works. This is a chicken and egg situation, your project is unlikely to ever go anywhere without sufficient power. My recommendation is to get the power sorted.

Any other way ?

If your project doesn't need to be portable you can source a variable voltage power supply that will deliver 5 Volts and all the Amps that you need. Note that the 'wall wart' type power supplies which you probably already have around the house are not likely to supply the current you need at a stable voltage, they are easy to overheat and pose a fire risk if they do. Look at a purpose designed adjustable power supply.

That covers the most common mistakes, I hope it has helped however if you are still stuck and have tried each of the tests and suggestions all is not lost, you can still ask for help from the Arduino community in the knowledge that you have covered all the basics.

Duane B.






8 comments:

  1. My ambition at Filmstarlook is to shoot the best photos of every jacket, so that our customers can make their best decision to choose any leather jacket. We guarantee you to make every jacket exactly show in our product image with high quality resolution. Assignment Editing Services

    ReplyDelete
  2. This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understand. Thank you for giving information. Wesley Snipes Vest

    ReplyDelete
  3. Chances are that you’re planning on hosting a raclette party. One appliance that promises to give your food a healthy twist is the raclette grill. This kitchen appliance is quite different from your traditional ones. And as such choosing the best raclette grill will need some help.

    ReplyDelete
  4. Download Instagram video to your device.Instagram Reels Downloader Save Insta videos to enjoy watching when you are offline and when Wi-Fi connection is poor. FastHigh qualityFree.

    ReplyDelete
  5. See the latest UK49s Teatime predictions below.UK 49s Teatime Prediction These are based on teatime hot numbers and dream guides. UK49 tea time Number Generator.

    ReplyDelete
  6. want to know large size parrot macaw? Hyacinth Macaws is the large size parrot
    Hyacinth Macaws are the largest flying Parrot in the world with a length of 1m from the tip of its tail to the top of its head. Only the Kakapo, a flightless species of Parrot, is heavier than the Hyacinth Macaw by just over half a kilo. we have a good source read guide here

    ReplyDelete
  7. This information is incredibly helpful! Great job—everything is both fascinating to learn and easy to understand. Thank you for sharing such valuable insights!https://whomademeaprincess.net/



    ReplyDelete
  8. This information is incredibly helpful! Great job—everything is both fascinating to learn and easy to understand. Thank you for sharing such valuable insights!https://yandexapk.info/



    ReplyDelete