Testing Batteries 1
A lithium quad-copter battery, powering a Galileo Gen2 Board

How to connect a battery to the Micro-controller

Batteries are a very tricky thing to get right. You would expect that simply buying one with the same voltage as the power adapter for the micro-controller would do the job, but this would be a very foolish thing to do! The issue with batteries is that their voltage does not remain constant. The voltage stated on the battery tells you the approximate max. voltage of the battery, when it is fully charged. Once you begin to drain the battery, its voltage starts to drop. Battery power indicators work by analysing this voltage and then deducing how much power is left.

Now when we look at the most common micro-controllers (Arduino’s etc.), we can supply power in five different ways. Through the (1) USB jack, the (2) Power Jack, the (3) 5v, the (4) 3.3v and the (5) Vin Pins. I would always avoid the USB jack to provide power, as you may want to use it to monitor/debug the controller. Providing power through the 5v or the 3.3v pins is a rather stupid thing to do, unless you know what you are doing. These pins bypass the voltage levelling circuitry, meaning that if your voltage deviates by as little as ±0.1v, you could potentially fry the board. I have already destroyed one of my micro-controllers by playing around with these power pins, and I don’t intend to do that again!

Finally  we can use the Power jack or the Voltage-In (Vin) pin. These are essentially the same thing as the Power jack is connected to the Vin circuit. These inputs are then passed through the crystals and voltage levelling circuitry, and power the 5v and 3.3v lines. As the voltage of a battery varies with use, I would always connect it through the Vin pin/jack as it will deal with any variances and prevent the battery from frying the board.

Frying the board isn’t the only danger when using a battery! As the battery is drained and its voltage drops below the micro-controllers min. 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!

Protecting your Micro-controller from Brown-outs

The best way to prevent a brown-out is to make sure that the voltage supplied is always above the min. 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. Otherwise, we will have to use a potential divider circuit is 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:

Testing Batteries 2
Potential divider circuit, using a 100K and a 47K ohm resistor

The code used to monitor the battery percentage looks something like this:

// Test program for battery voltage level measurement system
// (CC) Creative Commons - Simon Bluett - December 2014
// hello@chillibasket.com

#define analogPin 0

float Vout = 0;                     // The voltage reading of the battery
int batteryScaler = 0;              // Used to calculate battery percentages
float resistorOne = 100000;         // Connected to the voltage source
float resistorTwo = 47000;          // Connected to Ground
float vRef = 5;                     // Reference voltage - usually 5v or 3.3v
float maxBatteryVoltage = 11.1;     // The voltage of the battery when fully charged
float minBatteryVoltage = 10.0;     // Voltage below which battery isn't safe to use

void setup(){
    // Initialize serial communication

void loop(){

    //   .------R1-----.-------R2------.     | The sensor systems use a potential |
    //   |             |               |     | divider circuit, as shown, to get  |
    // Input        Analogue          GND    | an accurate value for the voltage. |

    Vout = (analogRead(analogPin) * vRef / float(1024));
    Vout = Vout * (1/(resistorTwo/(resistorOne + resistorTwo)));
    Vout = Vout - minBatteryVoltage;
    batteryScaler = Vout / (maxBatteryVoltage - minBatteryVoltage) * 100;
    //if (batteryScaler > 100) batteryScaler = 100;  // Uncomment to max at 100%
    //if (batteryScaler < 0) batteryScaler = 0;      // Uncomment to min at 0%

    Serial.print("v - ");

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

11.07v - 97%
11.29v - 116%
11.29v - 116%
11.29v - 116%

The actual voltage of the battery according to the Lithium battery charger was 11.3 v, so the value recorded by the micro-controller is 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.

2. Always ensure that the battery is within  the controller’s specified voltage input range.

3. Avoid brown-outs by monitoring the voltage of the battery and switching off the system if it drops below the min. working 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.

The next part of my Self-balancing Robot series is long-overdue, so you should expect to see the post released soon on this blog!

Updated: 24th May 2019 – Reformatted post

Leave a Reply

newest oldest most voted
Andy Hockin
Andy Hockin

I built one of your Wall_e’s. It is a very good size and was well engineered for fit and strength. My electronics is slightly different as I am using an Arduino Uno with a Bluetooth shield and a robot (CMDR) shield. No need for separate batteries or power converters. I have no raspberry yet. However, when a command is sent to the Arduino from my pad, the servos jump violently. So I had to use disconnect on all the servos and motors till I can figure it out. (I may yet need separate power supplies.) I have posted some pics… Read more »