Powering microcontrollers with batteries
For many TinyML applications, batteries could be the only power source for our microcontrollers.
In this final recipe, we will learn how to power microcontrollers with AA batteries.
The following Colab notebook contains the code referred to in this recipe:
06_estimate_battery_life.ipynb
:
Getting started
Microcontrollers don't have a built-in battery, so we need to supply an external one to make the device work when it is not connected through the micro-USB cable.
To get ready for this recipe, we need to know what types of batteries we need and how we can use them correctly to supply power.
Batteries are sources of electric power and have a limited energy capacity. The energy capacity (or battery capacity) quantifies the energy stored and is measured in milli-ampere-hour (mAh). Therefore, a higher mAh implies a longer battery life.
The following table reports some commercial batteries that find applicability with microcontrollers:
The battery selection depends on the required microcontroller voltage and other factors such as energy capacity, form factor, and operating temperature.
As we can observe from the preceding table, the AA battery provides a higher capacity, but it supplies 1.5 V, typically insufficient for microcontrollers.
Therefore, how can we power microcontrollers with AA batteries?
In the following subsections, we will show standard techniques to either increase the supplied voltage or the energy capacity.
Increasing the output voltage by connecting batteries in series
When connecting batteries in series, the positive terminal of one battery is connected to the negative terminal of the other one, as shown in the following figure:
Important Note
This approach will not extend the battery capacity but just the supplied voltage.
The new supplied voltage () is as follows:
Where N is the number of connected batteries in series.
For example, since one AA battery supplies 1.5 V for 2400 mAh, we could connect two AA batteries in series to produce 3.0 V for the same energy capacity.
However, if the battery capacity is not enough for our application, how can we increase it?
Increasing the energy capacity by connecting batteries in parallel
When connecting batteries in parallel, the positive terminals of the batteries are tied together with one wire. The same applies to the negative terminals, which are joined together as shown in the following figure:
Important Note
This approach will not increase the output voltage but just the battery capacity.
The new battery capacity () is as follows:
Where N is the number of connected batteries in parallel.
For example, since one AA battery has a battery capacity of 2400 mAh, we could connect two AA batteries in parallel to increase the battery capacity by two times.
Now that we know how to connect multiple batteries together to get the desired output voltage and energy capacity, let's see how we can use them to power the microcontrollers.
Connecting batteries to the microcontroller board
Microcontrollers have dedicated pins for supplying power through external energy sources, such as batteries. These pins have voltage limits, commonly reported in the datasheet.
On the Arduino Nano, the external power source is supplied through the Vin pin. The Vin input voltage can range from 5 V–21 V.
On the Raspberry Pi Pico, the external power source is supplied through the VSYS pin. The VSYS input voltage can range from 1.8 V – 5.5 V.
On both platforms, the onboard voltage regulator will convert the supplied voltage to 3.3 V.
How to do it...
Disconnect the Arduino Nano and Raspberry Pi Pico from the micro-USB and keep all the components on the breadboard.
The battery holder considered for this recipe connects the AA batteries in series. We recommend not inserting the batteries in the battery holder yet. The batteries should only be inserted when the electric circuit is completed.
The following steps will show how to power the Arduino Nano and Raspberry Pi Pico with batteries:
- Connect the positive (red) and negative (black) wires of the battery holder to the + and – bus rails respectively:
- The Arduino Nano and Raspberry Pi Pico have different voltage limits for the external power source. Therefore, we cannot use the same number of AA batteries on both platforms. In fact, three AA batteries are enough for the Raspberry Pi Pico but not for the Arduino Nano. In contrast, four AA batteries are enough for the Arduino Nano but beyond the voltage limit on the Raspberry Pi Pico. For this reason, we use a 4 x AA battery holder for the Arduino Nano to supply 6 V and a 3 x AA battery holder for the Raspberry Pi Pico to supply 4.5 V.
- Connect the external power source to the microcontroller board, as shown in the following diagram:
As you can observe from the preceding figure, VIN (Arduino Nano) and VSYS (Raspberry Pi Pico) are connected to the positive battery holder terminal through the + bus rail.
- Insert the batteries in the battery holder:
- 4 x AA batteries for the Arduino Nano
- 3 x AA batteries for the Raspberry Pi Pico
The LED application should now work again.
However, one thing we might be curious about is how can we evaluate the lifetime of a battery-powered application?
There's more
Once we have chosen the battery for the microcontroller, we can estimate its lifetime with the following formula:
Where:
The following Python code calculates the battery life in hours and days:
battery_cap_mah = 2400 i_load_ma = 1.5 battery_life_hours = battery_cap_mah / i_load_ma battery_life_days = battery_life_hours / 24 print("Battery life:", battery_life_hours,"hours,", battery_life_days, "days")
The preceding code estimates the battery life for the case when the battery capacity (battery_cap_mah
) is 2400 mAh, and the load current (i_load_ma
) is 1.5 mA.
The expected output is as follows:
Although the formula above is an estimation and valid under ideal conditions, it is enough to understand how long the system could last. A better model could include other factors such as battery self-discharge and temperature.