MEGA Sensor Shield V2.0
Please provide us a link to the product and/or datasheet. Specifically, we need to know what pins the power input uses on the Arduino to power it.
Everything works fine when connected to AC adapter 5V 2A. (separate power supply for the shield).
Then I tried 4xAA battery holder with Pololu rechargeable batteries (1.2 V, 2200 mAh, 1 cell). With fully charged batteries I got 5.3V, a bit later - 5.0V. When project is using this external power source I noticed 3 things:
How and where are you measuring the voltage -and- current draw? Are you doing so under load? Are you doing so with the power source disconnected or connected to the Arduino and shield? Are the servos and sensor connected as well? What kind of current do you measure when you have the AC adaptor connected vs the battery pack?
Have you tried using a separate sources for the Arduino vs the shield (the wording of your post is ambiguous in this regard)?
I want to note that four 1.2 volt cells in series -do not- equal 5 volts (4 x 1.2 = ?); virtually every kind of battery cell type has a higher voltage at full capacity than it's chemistry suggests - when it is not under a load. With a load, the voltage can and does change, moving closer to the actual voltage.
Do you know what the "C" rating of the battery cells is? This can be important.
With a heavy load (high current draw), the voltage can and will sag (ie, drop lower than the rated voltage). It is very likely that your servos are causing such sags, depending on the current they draw (loaded or unloaded).
- wrong UltraSonic sensor values (sometimes 0, sometimes just inappropriate values)
Note that when an ultrasonic sensor "pings" - it draws quite a bit of current - which again can cause a voltage sag.
- sometimes looks like servos are not enough powered
Most likely they aren't - have you measured how much current a servo needs when under load (or have you seen the datasheet or specs that says how much current it should need)?
- sometimes device is rebooted when it shouldn't.
When a voltage sag occurs due to excess current consumption - this can occur (it's called a "brownout reset").
So everything works perfect when powered from the power outlet with adapter, and became weird with autonomous power. Looks like in both cases voltage is fine, so what I'm doing wrong? What should I look at?
It's very likely that the adapter is a somewhat regulated source that is able to keep the voltage steady at 5 volts - even when you pull 2 amps, maybe a bit more from it. While the battery cells - depending on their "C" rating (even that may be suspect, depending on if they are a name brand or not) - may be only "1 C" (meaning that they can only supply the amount of current they are rated for - or 2.2 amps in this case - if -that- rating is true as well) - but if you exceed that, voltage sag will occur, and you'll see problems like you are seeing.
So - to figure this out:
Gather datasheets for all of the parts your are using - note the amount of maximum current needed for each part.
Find out the true ratings for the battery vs the power supply. Note that information down. Also learn what a "C" rating means. Also make sure your battery calcs or such are done for 4.8 volts (the answer to the multiplication question above, of course) - not 5 volts (if the cells are alkaline though - each cell will supply 1.5 volts, in which case the pack will be at 6 volts).
For those parts you cannot find specs for - measure the current using a single part, under a particular load. Be careful with the servos - you don't want to break them - instead, find out what their max torque is from the datasheet, and simulate it by:
A. Suspend a weight with string or something using a pulley over the edge of a table, with the other end of the string connected to the control arm mounted on the servo.
B. The weight and distance should be equal to what the torque rating for the servo reads - for instance, if the specs read something like "1 kg/cm torque" - that means you need to connect the string to the control arm on the servo approximately 1 cm from the center of the output shaft, then add 1 kg of mass to the other end of the string (hanging over the edge of the table with a pulley). Note that the purpose of the pulley is to keep the mass pulled somewhat perpendicular to the lever - it won't be perfect, but it will do. Also - if @MarkT comes into this thread and starts talking about how "1 kg/cm torque" is wrong and how to do it right - listen to him, but ignore him for the purposes of this test. You'll probably just get lost otherwise. His advice is necessary and good (and well intentioned) when it comes to other torque measurements and calcs, but he also seems to think that hobbyist grade hardware (and most common gearmotors) are measured and reported in SI values, when the truth is they usually aren't, unless you are buying your motors from a supplier for FIRST robotics competitions, or the motors are from a major well-known supplier (even then, it isn't guaranteed they will be spec'd in this manner).
C. Once this is done, use the sweep example or something similar to move the servo under load while measuring the current consumed for the given voltage in the spec sheet.
Then connect the battery pack and other parts together - but only the sensor; test the sensor, note the current, ping the sensor and note the current (two separate measurements). You may need to ping the sensor repeatedly as fast as possible to get a usable reading (you don't care about the return value - you are measuring current use here, not anything else). If possible, verify your reading with the datasheet - regardless, though, use your reading as the actual value.
Disconnect the sensor - then connect a single servo and measure it's current consumption under load (it should equal what you got in step 3).
Disconnect the servo - measure the current consumption of the Arduino itself when it is running the code.
Connect one servo (with a load) and the sensor - measure the current consumption of the Arduino when it is running the code.
Connect a second servo (with a load) - again, measure the current consumption of the Arduino when it is running the code.
For steps 7 and 8 - you might prepare some test code that will exercise all servos and the sensor at the same time - to simulate "worst case scenario".
With all of this data, you should be able to see how much current overall and max your system is using for the given voltage, and whether (knowing the voltage of the battery pack and its "C" rating) it can supply the current properly.
Repeat step 8 enough times (adding on more and more servos with loads) - and at some point (likely far below the 12 servos you are using) you should start seeing your problems occur. Note that each servo may require up to an amp of current while under load; even if it only require 500 mA under load, with 12 servos all operating at the same time, that would be 6 amps of load (not counting the ultrasonic sensor or the needs of the Arduino itself!). It's a wonder that you haven't burned out the AC adaptor, but it doesn't surprise me that the battery pack may not be able to supply the load (especially if the cells are only 1 C rated cells).
Also - don't connect two of the packs in series - but you might try two of the packs in parallel (to give you approximately 4 amps or more at 4.8 volts).
thanks so much for your patience with this newbie question and I hope to solve this issue.
Again - it really sounds like you are experiencing voltage sags and other issues due to excess current consumption from all of the servos, the sensor, and the Arduino, which you aren't supplying adequately. But this is only a guess on my part, without having the actual measured data of course.
Good luck, and I hope this helps...