惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
罗磊的独立博客
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
A
About on SuperTechFans
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
IT之家
IT之家
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
T
Tailwind CSS Blog
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog

Simply Explained

Converting a Tuya Thermostat to ESPHome Bringing Foam Monsters to Life: How I Wrote and Illustrated a Children's Book Using AI How I Built an NFC Movie Library for my Kids Analyzing Link Rot in My Newsletter (After 31 Editions) How I Use Alfred to Search My Obsidian Notes Faster (with Spotlight!) Year in review: 2022 Smart lights behind a wall switch (Shelly, Z-Wave, ESPHome) Serverless Anagram Solver with Cloudflare R2 and Pages Integrate Home Assistant with Apple Reminders How WebP Images Reduced My Bandwidth Usage by 50% Tracking gas usage with ESPHome, Home Assistant, and TCRT5000 My Sixth Year as YouTube Creator (statistics + retrospective) EZStore: a tiny serverless datastore for IoT data (DynamoDB + Lambda) ESP-IDF: Storing AWS IoT certificates in the NVS partition (for OTA) How to securely access your home network with Cloudflare Tunnel and WARP I Built a CO2 Sensor and It Terrifies Me Filtering spam on YouTube with TensorFlow & AI Building a killer NAS with an old Rackable Server How I Structure My ESPHome Config Files Howto Virtualize Unraid on a Proxmox host Preventing Cumulative Layout Shifts with lazy loaded images (Eleventy + markdown-it) Migrating This Blog From Jekyll to Eleventy Good Home Automation Should be Boring ESP32 Cam: cropping images on device Retrospective: My Fifth Year on YouTube Secure Home Assistant Access with Cloudflare and Ubiquiti Dream Machine Shelly 2.5 + ESPHome: potential fire hazard + fix Impact of Adblockers on Google Analytics (vs. Plausible) Shelly 2.5: Flash ESPHome Over The Air! Tuya IR Hub: control Daikin AC (Home Assistant + ESPHome)
MAX17043: Battery Monitoring Done Right (Arduino & ESP32)
Xavier Decuy · 2021-04-28 · via Simply Explained

Building a battery-powered IoT device? Then you'll want to monitor the battery's percentage. Here's how to do it properly.

The problem

Many websites tell you to measure the battery level by measuring its voltage. Usually with a voltage divider to bring down the voltage so that an ADC can read it.

But this method is not ideal. First up: it continuously drains the battery (depending on the resistors you use). And secondly, the voltage of Li-ion or LiPo batteries doesn't drop linearly. The voltage drops off quickly in the beginning, stays very stable for a long time, and then suddenly drops low at the end of its life:

LiPo battery discharge curve LiPo battery discharge curve. Source: prototalk.net

It's challenging to convert a measured voltage into a battery percentage.

Fuel Gauge: MAX17043

A better solution is to use a "battery fuel gauge," such as the Maxim Integrated MAX17043 (datasheet).

This tiny chip uses the ModelGauge algorithm to measure a battery's capacity. It doesn't require resistors or calibration.

It works through an i2c interface, and it can report the battery's percentage and voltage. It also has an interrupt pin, so you can have it wake your microcontroller when the battery dips below a certain level.

And even more good news: it's available as a breakout board for DIY projects, such as this one from DFRobot:

Other brands have similar boards: SparkFun / AliExpress.

Wiring it up

Connecting this breakout board to your microcontroller is easy: connect the power output to the VIN of your board and connect the SDA and SCL pins for i2c connectivity.

In real life, that looks like this:

Photo of MAX17043 fuel gauge connected to an ESP32

This might look a bit complicated. Here's a simplified schematic that should work regardless of the breakout board you have:

How to connect a MAX17043 fuel gauge How to connect a MAX17043 fuel gauge

I'm using a 2000mAh LiPo battery and a LOLIN32 board (ESP32 based), but you can use any microcontroller you'd like, including an Arduino.

Note: Normally, you only have to connect the battery to the fuel gauge, and the fuel gauge to your microcontroller. The fuel gauge will pass along power to the microcontroller. However, in this case, my ESP32 board can't be powered through its pins. It only accepts power over USB or the battery connector. That's why there are 2 additional wires in my setup.

Arduino code

Now that everything is wired up, we can start writing some code. There are several Arduino libraries for the MAX17042:

Both of these will work fine, even with breakout boards from different brands. I'll use the second library.

I'll start by adding it as a dependency to my platformio.ini file:

lib_deps = 
    https://github.com/porrey/max1704x

Then, in the Arduino sketch, I'll start by including the library and initializing the sensor:

#include "MAX17043.h"

void setup(){
    Serial.begin(115200);
    FuelGauge.begin();
}

Finally, we can use the loop function to regularly check the battery's percentage and voltage:

void loop() {
    float percentage = FuelGauge.percent();
    float voltage = FuelGauge.voltage();

    Serial.print("Battery percentage: ");
    Serial.print(percentage);
    Serial.println("%");

    Serial.print("Battery voltage: ");
    Serial.print(voltage);
    Serial.println("V");

    delay(1000);
}

That's it! Flash this to your board, and you should see the battery percentage and voltage in the console.

Result

I also did some longer testing with ESPHome. I made a custom component to support the MAX17043 and configured the ESP32 to sleep for 1 hour, measure the battery capacity, send it to Home Assistant, and then sleep again.

So far, it has been running for about 2 weeks, and the battery is still at 66%.

Battery life is highly dependent on the microcontroller you use, how long you let it sleep and how much it consumes during sleep. My particular one (LOLIN32) isn't optimized for low-current during deep sleep, so I have to swap that out for better battery life.

Conclusion

The MAX17043 fuel gauge is fantastic. It's super easy to use and very accurate. I don't understand why so many battery-powered dev boards ship without it.

Can a manufacturer please integrate this chip onto an ESP32 dev board please? Thanks!