Voltage - arthurbenemann/ardupilot GitHub Wiki
APM has built in battery voltage-sensing circuitry, and the instructions on using it are below. However, you may find it more useful to measure the current used by your battery, which will give you a better indication of how much power you have left than just voltage, which can vary with the load. Instructions for using the optional current sensor are at the bottom of this page.
The APM shield has four built-in voltage sensors, in the form of voltage divider circuits connected to analog input pins on the processor. The APM software (2.X) is configurable for four different battery monitoring setups. These are:
- Measuring individual cell voltages and total battery voltage for a 3 cell li-po battery.
- Measuring individual cell voltages and total battery voltage for a 4 cell li-po battery.
- Measuring the total battery voltage only. This can be used with high voltage set-ups.
- Measuring the current consumption and total battery voltage. This option requires an external current sensor, but gives the most accurate information on amount of battery power remaining. It is also the most compatible with Ground Station displays.
You can also use these inputs for measuring analog voltages from a variety of other sources. For example, you can use one to measure your (total) battery voltage and use the other to read the voltage output from some other external sensor.
Each of the 4 voltage inputs can be used to measure voltages up to the supply voltage of APM (5 volts). Pads have been provided to add a resistor on each input creating a voltage divider so that higher voltages can be measured. The default configuration is to use 3900 Ohm resistors here for voltage measurements up to 17.9 volts. Information on using different values is included later on this page. The four are shown in the red box below:
http://ardupilot-mega.googlecode.com/svn/ArduPilotMegaImages/voltagemeters.jpg
Setting up battery monitoring
Battery monitoring can be enabled either through the Command Line Interpreter, or using the parameter interface through a Ground Control Station.
In the Command Line interpreter go to the Setup mode/menu and then type "battery X", where X is the number above corresponding to the type of monitoring you want. If you want to disable battery monitoring use 0.
Set-up for 3 or 4 cell li-po with individual cell voltages
The best way to measure battery voltage is to connect a cable with a male JST-XH connector (4 pin for a 3C/11.1v battery, 5 pin for a 4C/14.8v battery) to the APM shield pins. You must also solder in a 3.9k ohm resistor (included with the shield kit) in the area marked on the board for each cell you want to measure. (Labels are on the bottom of the shield.) Note that there is a ground pad next to the AN0 pad, and you should solder a header pin there so that your cable fits as shown.
**3S Wire color** | **Pin** |
black | GND |
yellow | AN0 |
blue | AN1 |
red | AN2 |
Here's a typical setup for a 3c battery:
http://ardupilot-mega.googlecode.com/svn/ArduPilotMegaImages/IMG_0963.JPG
Set-up for just measuring total battery voltage
For just measuring the total battery voltage you will use a setup similar to the setup for measuring individual cell voltages, except the total battery voltage should be applied to AN0. For batteries up to a 4 cell lipo you should use a 3.9k ohm resistor and the default voltage divider ratio. For higher voltage batteries you will need to use an appropriate resistor and change the voltage divider ratio in the configuration (see below).
Setting the Voltage Divider Ratio
The Voltage Divider Ratio is the ratio of the built-in 10k ohm resistor and the through hole resistor(s) you've added over the through hole resistor(s) . So you if you add a 3.9k resistor, it's (10+3.9)/3.9 = 3.56. Note that if you do not add a resistor, then the voltage divider ratio is 1 (infinity/infinity = 1)
For non-standard battery voltages
You can use batteries of higher voltage than 4 cell lip, but you need to use a different resistor value in the voltage divider, and you need to put a different voltage divider value in the configuration file.
To use the voltage divider, you need to add a regular (through-hole) resistor to scale the signal for the voltage you're measuring. To calculate the right resistor value, use the following equation. R 1 is the built-in 10,000 ohm resistor and R 2 is the resistor you'll be adding. Vout should be around 5V, and Vin is the highest voltage of the device you want to measure.
- {{{R 2 = R 1 ** Vout /((Vin - Vout)}}}
For a standard 3-cell (11.1v) or 4-cell (14.8v) !LiPo battery, you'd calculate the necessary resistor as follows:
We know that R 1 = 10k or 10000 ohms, Vout should be around 5volts and we'll give Vin as 17.94V (to give us a little headroom for overvoltage situations).
{{{R 2 = 10000ohms ** 5v /(17.94V - 5v); }}}
R 2 = 3.9K
So that's why we include a spare 3.9k resistor with APM shields!
The data can be read on analog pins 0,1,2 and 3 (Atmega1280 pins PI6, PI7, PE6, PE7).
Connect the voltages sources you want to measure to the following pins. (Connect the positive wire; the ground is assumed to already be connected to the board):
http://ardupilot-mega.googlecode.com/svn/ArduPilotMegaImages/voltage.jpg
When configured for current and voltage battery monitoring APM will provide both the current voltage and current, and a cumulative current consumption in milli-amp hours. This is the most accurate measurement of battery capacity remaining. An external current sensor is required. http://ardupilot-mega.googlecode.com/svn/ArduPilotMegaImages/currentsensor.png
An external current sensor is required. The default is the !AttoPilot 90A/50V Voltage/Current Sensor, which is available from SparkFun (shown above).
The sensor provides a scaled output for battery voltage and current. The outputs are designed for a 12 bit 3.3V ADC. Full range is 51.8 Volts and 89.4 Amps. If you are using a 4 cell lipo or smaller we recommend that you DO NOT use the voltage divider on this sensor board. Rather, we recommend you use the APM onboard voltage divider as this will give better resolution. If you are using the APM onboard voltage divider then install a 3.9k ohm resistor for AN0. If you are using the current sensor board voltage divider do not put a resistor in for the APM onboard voltage divider AND you will have to change the voltage divider ratio in the firmware.
Connect the !AttoPilot sensor to APM as shown in the photo. Use the AN0 and AN1 voltage sensor pins and the adjacent ground pin.
Solder on a 3 pin header. The 4th pin from the bottom of the row is ground (the black lead). The next pin up is battery voltage (the red lead). The 3rd pin is the battery current (white lead).
http://ardupilot-mega.googlecode.com/svn/ArduPilotMegaImages/AttoPilot_current.jpg
Instructions for setting up the APM code to use the !AttoPilot sensor:
- Remove all voltage divider resistors from APM board
- In the APM Arduino code's APM_config.h add these lines: For 4 cell LiPo add this line: "#define VOLT_DIV_RATIO 16.6" For 3 cell LiPo add this line: "#define VOLT_DIV_RATIO 12.6"
- If you have trouble compiling for 1280 board, write additional line to APM_config.h #define LOGGING_ENABLED DISABLED It will prevent error (sketch too big). New board APM2560 does have more memory.
- Upload firmware and connect external sensor