-
-
Notifications
You must be signed in to change notification settings - Fork 0
Battery Level Sensor
Change Language
Last update: 15-11-2023
The positive battery terminal is connected to an ADC IO on the microcontroller. The code below allows the calculation of the battery level, in percentage %, in a simplistic manner.
In my devices I use two 100k resistors:
#define R2 100 #define R3 100
The output voltage of the resistor divider is calculated as Vout = (Vin * R3) / (R2 + R3)
.
#define VOLTAGE_OUT(Vin) (((Vin) * R3) / (R2 + R3))
As a very rough estimation, one can assume a battery voltage of 4.2V as 100% and a voltage of 3.3V as 0%. Converted to millivolts to avoid floating-point calculations.
#define VOLTAGE_MAX 4200 #define VOLTAGE_MIN 3300
The reference voltage of the ESP32 microprocessor is 1100mV:
#define ADC_REFERENCE 1100
A value returned from ADC in 12-bit mode will be in the range of 0 to 4095. To determine the voltage on the ADC IO one can use the following formula:
#define VOLTAGE_TO_ADC(in) ((ADC_REFERENCE * (in)) / 4096)
The minimum and maximum values of battery voltage using a resistor divider are:
#define BATTERY_MAX_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MAX)) #define BATTERY_MIN_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MIN))
Retrieving value from ADC is pretty straightforward and well described in the official Espressif documentation. Assume the measured value from ADC IO in the variable called adc
, then the battery level calculation is done as follows:
int calc_battery_percentage(int adc)
{
int battery_percentage = 100 * (adc - BATTERY_MIN_ADC) / (BATTERY_MAX_ADC - BATTERY_MIN_ADC);
if (battery_percentage < 0)
battery_percentage = 0;
if (battery_percentage > 100)
battery_percentage = 100;
return battery_percentage;
}
A more accurate Battery level sensor can be implemented on the same electronics using Alberto Iriberri Andrés (PangoDream) Library.
First, is needed to get the value of ADC pin. This value may vary from 0 to 4096 depending on the voltage applied to it from 0V to 3.3V. So it can be established a constant to calculate the voltage applied to the pin based on its value. This constant, theoretically, will be 3300 / 4096 = 0.8056.
Since the measurement electronics is based on a voltage divider and the voltage applied to the pin is half the voltage of the battery, the constant value should be 0.8056 x 2 = 1.6113. This means, for each unit in ADC pin, represents 1.6113 mVolts applied to it. For instance, if one reads the value of the ADC pin of 2,543, then the voltage applied to the pin should be 2,453 x 1.6113 = 3,952V = 3.95V
ADC pins are not that precise, so the value of the constant should be adjusted to a level considered to be valid for the onboard components. In Alberto Iriberri Andrés case, after doing some tests concluded that the best value for the conversion factor is 1.7.
As mentioned before, calculating the charge level is a direct translation from the voltage we obtained to a charge level by using a table.
All the code to make these calculations is contained in a library Alberto Iriberri Andrés created for that purpose.
[1] https://www.pangodream.es/esp32-getting-battery-charging-level
🟢 Fully tested and working
A green circle means the hardware electronics or the programming code was fully tested, each of its functionalities and capabilities. And it can be installed in a vehicle. Keep in mind this does not mean errors won't happen. As in everything related to electronics and software, there are revisions and updates. This open hardware is no different.
💯 Fully tested & working, no improvements necessary - already being sold online
🆓 Fully Open hardware \ source code
🤪 There's better than this. don't use it
🔐 Fully closed hardware \ source code
⚡️ fully tested and working, however, it is a dangerous solution to deploy
🟡 Not tested. Working capability is unknown, it may work or not.
A yellow circle means the hardware electronics or the programming code was not fully tested, each of its functionalities and capabilities. This does not mean it not working, it simply means testing is needed before giving a green circle of approval.
🔴 Fully tested but not working.
A red circle means the hardware electronics or the programming code was fully tested, and found some kind of critical error or fault. This means the electronics or firmware code cannot be used in a vehicle.
⌛ Not started.
The hourglass means the hardware electronics or the programming hasn't started. Most likely because is waiting for the necessary test components needed for reverse engineering and also engineering of the new open solution.
🆕 New updated contents
The new icon means the link next to it was recently updated with new contents
💬 Comments on the Discussion page
The comments icon means there are useful and even new comments on the discussions page of the repository important for what you are seeing or reading.
Join the beta program to test and debug to provide feedback, ideas, modifications, suggestions, and improvements. And in return, write your own article blog or post on social media about it. See participation conditions on the Wiki.
The Beta Participant Agreement is a legal document being executed between you and AeonLabs that outlines the conditions when participating in the Beta Program.
Bug reports and pull requests are welcome on any of AeonLabs repositories. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
- Contributing
Please make sure tests pass before committing, and add new tests for new additions.
You can get in touch with me on my LinkedIn Profile:
You can also follow my GitHub Profile to stay updated about my latest projects:
The PCB design Files I provide here for anyone to use are free. If you like this Smart Device or use it, please consider buying me a cup of coffee, a slice of pizza or a book to help me study, eat and think new PCB design files.
Make a donation on PayPal and get a TAX refund*.
Liked any of my PCB KiCad Designs? Help and Support my open work to all by becoming a GitHub sponsor.
Before proceeding to download any of AeonLabs software solutions for open-source development and/or PCB hardware electronics development make sure you are choosing the right license for your project. See AeonLabs Solutions for Open Hardware & Source Development for more information.