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

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

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) Building Air Quality Sensor: Luftdaten + Home Assistant HEIC to JPG: Build a Quick Action with Automator Make Your Garage Door Opener Smart: Shelly 1, ESPHome and Home Assistant Static webhosting benchmark: AWS, Google, Firebase, Netlify, GitHub & Cloudflare Why I don't take sponsorships Monitoring my 3D printer with a Pi Zero, Home Assistant and TinyCore Linux ESP32: Keep WiFi connection alive with a FreeRTOS task Home Energy Monitor: V2 Retrospective: 4 years on YouTube
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!