That’s significant if you’re using a coin cell battery, but not when you have 100s of milliAmp-hours. I’ll use 100KOhm resistors in this example, which should only leak a maximum current of: 4.2 = I * 200,000
The downside to this approach is that the battery will also constantly discharge through the voltage divider, but if we use large resistor values we can keep the current low. Simple voltage divider to cut the battery voltage in half for the ADC.
Remember, if you have two resistors (R1 and R2) in series with a total voltage of V across them, the voltage across R1 is: V1 = ( V * R1 ) / ( R1 + R2 ) The junction where the resistors meet will have half of the voltage across the two of them put together, which would be a maximum of 2.1V – well within our 3.3V limit. I think that the easiest solution to that problem is to make a voltage divider with two resistors in series that have the same value. Just like when we run off of 5V supplies such as USB, we can use a 3.3V voltage regulator to make the STM32 happy, but we cannot use the ADC to read an analog voltage which is higher than the supply voltage.
Most lithium batteries today have a charge voltage of 4.2V, which I like to think Douglas Adams would appreciate, but most STM32s have a maximum voltage of 3.6V. Our first step is designing an STM32 board with a battery charger, a battery connector, and an ADC connection to read the battery’s voltage. And here is a GitHub repository containing design files for a simple board which demonstrates the concepts described in this post. So with those brief and not comprehensive safety warnings out of the way, let’s get started! I’ll use an STM32L4 chip for this example, but the ADC peripheral doesn’t seem to change much across STM32s. It’s also good practice to avoid leaving lithium-ion batteries unattended while they are charging, and you should try to get batteries with built-in protection circuitry to help mitigate bad situations like over-current, under-voltage, etc.
I will also talk briefly about how to add a simple battery charger to your design, but you should always independently verify any circuitry which interacts with lithium batteries! This circuit seems to work to the best of my knowledge, but don’t take my word for it it’s very important to double- and triple-check your li-po battery circuits, because they can easily become serious fire hazards if they are handled improperly. So in this post, I’m going to go over a very basic circuit to power an STM32 board off of a single lithium-ion battery and monitor its state of charge. Most lithium-based batteries also last longer if you avoid fully discharging them – there’s some good information about lithium battery aging in this article. Not knowing whether something has hours or seconds of life left can be really annoying, and trying to use a nearly-dead battery can cause strange behavior, especially if the battery’s power drops off slowly as it dies.
When you do move an application to battery power, you’ll quickly discover that it is very important for your device to be able to A) charge its battery and B) alert you when its battery is running low. Many types of devices would not be useful if they had to be plugged into a wall outlet all the time, and power efficiency is one of the biggest advantages that microcontrollers still have over application processors like the one in a Raspberry Pi. If you choose to pursue embedded development beyond the occasional toy project, it probably won’t take long before you want to design something which runs off of battery power.