• Post Comments:2 Comments
A lithium quad-copter battery, powering a Galileo Gen2 Board

How to connect a battery to the Arduino

When working with sensitive circuits and micro-controllers, batteries can be a tricky thing to get right. It is not as simple as just buying a battery with the same voltage as what the micro-controller requires; the issue with batteries is that their voltage does not remain constant. The voltage stated on the battery usually tells you the approximate voltage when it is at 50% charge. The voltage tends to be slightly higher when fully charged, and drops below this value as it is drained. Battery power level indicators work by analysing this voltage and then calculating how much power is left.

Most Arduino compatible micro-controllers have several different ways in which they can be powered. Through the (1) USB jack, the (2) Power Jack, the (3) 5V Pin, and the (4) Vin Pin. Many Arduino’s also have a 3.3V pin, however this is only a voltage output and cannot be used to power the board. The voltage input (Vin) pin and power jack are both connected to the same power regulation circuitry, which can take in voltages between 7-12V and convert them into stable 5V used by the micro-controller. If you use the 5V pin to provide power, this regulation circuitry is bypassed and feeds the chip directly. This means that if the voltage provided to the 5V pin deviates by as little as ±0.1v, you could potentially fry the board.

As the voltage of a battery varies with use, I would always connect it through the Vin pin or the power jack. This allows the power regulation circuitry to deal with any variances in the voltage and prevent the battery from frying the board.

However, frying the board isn’t the only danger when using a battery! As the battery is drained and its voltage drops below the micro-controller’s minimum voltage requirement, a brown-out can occur. Brown-outs happen when the micro-controller partially shuts-down due to a power shortage. These usually cause the controller to restart, but they could also potentially damage the controller. Some batteries (especially LiPos) also are damaged if they are over-discharged and the voltage drops too low. This makes it important to monitor the voltage of the battery during use, so that we know when the battery level is too low and it needs to be recharged.

Protecting your Battery from being Over-discharged

The best way to prevent a brown-out is to make sure that the voltage being supplied is always above the minimum required voltage for the controller. If the battery falls below this minimum voltage, the micro-controller should display a warning message, or switch itself off. A nice way of dealing with this is to make the micro-controller monitor its own power supply by using one of the analogue input pins. If the voltage of the battery is less than 5V, it can be connected directly into the analogue pin of an Arduino. Otherwise, we will have to use a potential divider circuit in order to reduce the voltage to a safe level.

For my second version of the self-balancing robot, I am using a 11.1V lithium quad-copter battery to power a Galileo Gen2 Board and two 12V motors. As the voltage of the battery is greater than 5V, I used the following circuit in order to monitor the battery as it is being used:

When two resistors are connected in series like this, with one end connected to a certain voltage and the other to ground, the voltage at the junction of the two resistors is determined by the ratio of the two resistances. The formula is as follows, where (Vin) is the input voltage, (R1) and (R2) the resistances of the first and second resistor, and (Vdiv) the voltage at the junction between both resistors:

V_{div}=\frac{V_{in}R_2}{R_1+R_2}

The ratio of the resistors should be chosen, so that the voltage at the junction (Vdiv) is below 5V when the battery is at its maximum voltage. Since the Arduino measures the lower voltage (Vdiv) at the junction of both resistors, this formula can be rearranged to calculate the voltage of the battery (Vin):

V_{in}=V_{div}\frac{R_1+R_2}{R_2}=V_{div}\left(\frac{R_1}{R_2}+1\right)

The code I use to monitor the battery percentage on the Arduino is shown below. The same formula is used on line 22 to calculate the actual battery voltage. For the 11.1V LiPo battery I used, the maximum voltage at full charge was 12.6V. The minimum voltage below which it should no longer be used was 10.2V.

// Pin number of the analog input being used for the reading
#define ANALOG_PIN 2

// Values for the two resistors used in the potential divider
float resistorOne = 100000;
float resistorTwo = 47000;

float vRef = 5;               // Reference voltage - usually 5v or 3.3v
float maxVoltage = 12.6;      // The voltage of the battery when fully charged
float minVoltage = 10.2;      // Voltage below which battery isn't safe to use

void setup() {
    // Initialize serial communication
    Serial.begin(9600);
}

void loop() {
    // Calculate voltage at the analog pin
    float voltage = analogRead(ANALOG_PIN) * vRef / 1024.0;

    // Calculate the battery voltage
    voltage = voltage * (resistorOne / resistorTwo + 1);

    // Calculate the battery percentage
    float batteryPercentage = (voltage - minVoltage) / (maxVoltage - minVoltage) * 100;

    // Limit the percentage value to be between 0 to 100%
    if (batteryPercentage > 100) batteryPercentage = 100;
    else if (batteryPercentage < 0) batteryPercentage = 0;

    // Output the calculated values to the serial monitor
    Serial.print(voltage); Serial.print("V, ");
    Serial.print(batteryPercentage); Serial.println("%");
    delay(1000);
}

The serial output I got from the system then looked like this:

11.07V, 36%
11.29V, 45%
11.29V, 45%
11.29V, 45%

In my test, the actual voltage of the battery according to the Lithium battery charger was 11.3V, so the value recorded by the micro-controller was relatively accurate.


Here is a quick summary of the points I discussed above:

1. Never connect a battery directly into the 5v or 3.3v pins of the micro-controller. These inputs require the voltage to be properly regulated.

2. Always ensure that the battery is within  the controller’s specified voltage input range. For the Arduino Uno this is 7-12V.

3. Avoid brown-outs and battery over-discharge by monitoring the voltage of the battery and switching off the system if it drops below the minimum voltage.

4. When monitoring the voltage, take caution that the voltage being measured does not exceed the reference voltage (5V). Reduce the voltage by using a potential divider circuit.

Updated: 24th May 2019 – Reformatted post

Updated: 28th May 2020 – Added extra detail about the potential divider circuit

guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments