## Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Hi all

I am using a RPi 0W.
Its usual current consumption is 100-200mA.

Up until now I have been powering it from a USB Power Bank,
since it's completely protected from the problems that can happen when powering it from the wall,
e.g. power outages and voltage drops.

I would now like to switch to have my RPi be working 24/7,

I can of course power it from a 5v AC Adapter, which there are many today, that can provide the needed Current,
but my problem is that I fear from the instability that comes with wall power..

So, I have seen some UPS modules for Raspberry Pi (or for 5v in general),
they look like a nice solution, but the problem is that they are all based on a Li-Ion battery.

This means, that they might quickly wear the battery,
since there's a limited number of times that you can recharge a Li-Ion battery.
Also, constantly charging it, also contributes to its wear.

So I thought,
maybe building a small circuit using a Super Capacitor might do the job?

What I need is something that will
1) Protect the RPi from Voltage Drops, which are usually 1-2 seconds in duration
2) Protect the RPi from Power Outages, by giving it 1 minute to properly shutdown.

Important Note:
I do not need the circuit to provide power for the RPi to work during the whole power outage,
so for example If the power outage is 4 hours,
I only need 1 minute to properly shutdown,
and it's OK that the RPi will not be working during the power outage.

So,
If we take the 100-200mA current consumption that I mentioned in the beginning,
(let's be generous and take the maximum - 200mA)
and we calculate it for 1 min,
then it will be:
200mA, for a Full Hour = 200mAh
so:
200mA, for 1 Minute = 200mAh / 60 = 3.33mAh

Can a Super Capacitor provide 3 or 4mAh, in 5v?

The advantages of a Super Capacitor is that it does not get worn by Charge and Recharge cycles,
and also it does not get worn by constantly charging it to its full capacity, 24/7, for years..

What do you think?

Or maybe there are other options for implementing such a short duration UPS,
besides the 2 that I mentioned?

Thank you very much

drgeoff
Posts: 13593
Joined: Wed Jan 25, 2012 6:39 pm

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

The battery is not constantly being discharged and recharged. The battery is only supplying current when the AC mains fails. The battery is only charged when the AC mains returns after an outage.

For a supercap the calculations use the same two equations as any capacitor.

Q=CV (Q is charge, C is capacitance, V is voltage.)
Q= It (I is current, t is time)

You can use those to calculate the voltage drop for a given current drain after a given time. (Be sure to use the correct units. Farads, Volts, Amps and seconds.)

For example. 200mA for 1 minute dropping from 5 to 4.75 Volts needs a 48 Farad capacitor.
Last edited by drgeoff on Tue Mar 21, 2023 6:15 pm, edited 3 times in total.
Quis custodiet ipsos custodes?

MikeDB
Posts: 1878
Joined: Sun Oct 12, 2014 8:27 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

A super capacitor can just about keep a Pi powered up whilst Linux shuts down. But that's all. So it could smooth out power line glitches but not a complete power cut.
Always interested in innovative audio startups needing help and investment. Look for InPoSe Ltd or Future Horizons on LinkedIn to find me (same avatar photograph)

redvli
Posts: 941
Joined: Thu Sep 03, 2020 8:09 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

If you have a proper / intelligent power management for a LiPo cell there is almost no wear out if you have short power-outages every now and then. But I don't know how good and smart the IC's in those powerbanks are. I have made a design myself with an arduino and measured over a year. 8 charge cycles per year, so nothing compared to how a LiPo in a smartphone is used.

If you are worried about SD-card or filesystem corruption: Use Btrfs instead of Ext4 for the rootfs. It has no problems with sudden powerloss. All my Pi's use it. 1 Pi3 is solarpowered, looses power many times per day when little sun, no problem at all.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Thank you drgeoff, MikeDB, and redvli.

drgeoff wrote:
Tue Mar 21, 2023 5:37 pm
The battery is not constantly being discharged and recharged.
The battery is only supplying current when the AC mains fails. The battery is only charged when the AC mains returns after an outage.
What you're describing is the ideal case.
Indeed that's what we want.
the way the chip decides this is already predefined, by the chip.

For example,
the charging chip may decide that even if the battery was not used (because the was no power outage),
it would still recharge it now, even tho it's 95% full, and only lost 5%, during the time that passed,
and that way you get a 95% full battery, being recharged to 100%..
Not exactly ideal.

the only way I see to overcome this,
is maybe via building your own circuit using Li-Ion and a charging chip,
then, If a good charging chip is chosen,
or if it's a chip that can let the circuit builder choose some options,
(for example, "only start recharging, when battery is below a certain voltage)
then you can feel confident that the Li-Ion battery would not be worn out by improper charging management..

That's what I think,
maybe anyone has better info..

drgeoff wrote:
Tue Mar 21, 2023 5:37 pm
For a supercap the calculations use the same two equations as any capacitor.

Q=CV (Q is charge, C is capacitance, V is voltage.)
Q= It (I is current, t is time)

You can use those to calculate the voltage drop for a given current drain after a given time. (Be sure to use the correct units. Farads, Volts, Amps and seconds.)

For example. 200mA for 1 minute dropping from 5 to 4.75 Volts needs a 48 Farad capacitor.
That's cool,
50F or even 100F (at 5v) is available, on AliExpress, and for quite cheap even.

I hope anyone can contribute more regarding the Li-Ion charging chip behavior,
it will help choosing between the 2 options..

redvli wrote:
Tue Mar 21, 2023 6:36 pm
If you have a proper / intelligent power management for a LiPo cell there is almost no wear out if you have short power-outages every now and then.
But I don't know how good and smart the IC's in those powerbanks are.
Yes,
that's exactly what I'm talking about..
We don't know what decisions the chip designer made..
And the UPS module does not advertize its charging behavior..

Maybe the designer wanted to maximize the time the battery is at 100%,
while I (and many others), would prefer to maximize the Li-Ion battery's lifetime..

The algorithm in the above 2 options is different..
In the first you want to recharge the battery quite soon after it looses some of its capacity,
and in the second it's completely OK for you that the battery is not at its 100% capacity most of the time,
as long, for example, as it is above 50% capacity..

The ideal Charging Chip (and UPS Module) is one that let's you choose between several options.

redvli wrote:
Tue Mar 21, 2023 6:36 pm
I have made a design myself with an arduino and measured over a year. 8 charge cycles per year,
so nothing compared to how a LiPo in a smartphone is used.
That's fantastic.
That's what I would want from a UPS module.

redvli wrote:
Tue Mar 21, 2023 6:36 pm
If you are worried about SD-card or filesystem corruption: Use Btrfs instead of Ext4 for the rootfs.
It has no problems with sudden powerloss. All my Pi's use it.
1 Pi3 is solarpowered, looses power many times per day when little sun, no problem at all.
Yes, that's what I'm worried about.. and the whole reason for wanting a UPS module between the 5v AC Adapter and the RPi.

Regarding btrfs:
so should I continue to flash it to the microSD card, and then convert the root partition?
Or there's a different way to do it?

redvli
Posts: 941
Joined: Thu Sep 03, 2020 8:09 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Tue Mar 21, 2023 6:43 pm
Regarding btrfs:
so should I continue to flash it to the microSD card, and then convert the root partition?
Or there's a different way to do it?
There are several ways, RPF doesn't make it easy, as after the first boot your SD-card has no spare room anymore. You can convert the root partition, but more steps have to be done. Maybe have a look at: viewtopic.php?t=104108
It is old, but a quick look shows that it is still valid.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Oh..

BTW,
Why don't the RPF foundation makes this the default?
If it's more resilient..

redvli
Posts: 941
Joined: Thu Sep 03, 2020 8:09 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Tue Mar 21, 2023 9:15 pm
Oh..

BTW,
Why don't the RPF foundation makes this the default?
If it's more resilient..
There are many reasons I think, but mostly historical. Where the RPi platform comes from and how it is presented (TV STBs and education). You know, Windows users have always been told to properly shutdown their PC, otherwise... format C: That was when rootfs was FAT. But long time ago.
At least for education, you won't start with advanced filesystems, especially not if display (CRT and later HDMI) are most prominent. If something goes wrong, the equivalent of 'format C:' is write the latest image to a (new) SD-card.

If you use Btrfs, it might be that you yourself have to deal with warnings and errors because of bad SD-cards. So then you need to read man pages etc and have a plan how to repair. If you don't know or don't have time or whatever, Btrfs doesn't make sense.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

So a 5v UPS of some sort is a must then..

redvli
Posts: 941
Joined: Thu Sep 03, 2020 8:09 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Wed Mar 22, 2023 7:19 am
So a 5v UPS of some sort is a must then..
You can maybe compare with smartphones, they are always powered, so their eMMC and SD-card are pretty save w.r.t. corruption.

In theory Ext4 (and NTFS in Windows) should be resilient enough as they are journalling filesystems. But in PC's (Linux or Windows) HDD's or SSD's are used and they have much better internal management, so a sudden power loss isn't a disaster nowadays. Older and cheaper SD-cards can have really simple internal firmware that just ruins the filesystem on the SD-card much more easily. At least that is what I read many times.

But I have almost no experience with Ext4 on SD-cards. Since I use Pi's for real functions, home automation etc, they use Btrfs, same as PC's since 2013 or so. I have learned my lessons in the past with silent corruption of critical / important files on various media where the filesystem has no build-in CRC checking on every block.

ame
Posts: 7145
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Wed Mar 22, 2023 7:19 am
So a 5v UPS of some sort is a must then..
A regular mains UPS will work.
Hmm. What can I put here?

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

ame wrote:
Wed Mar 22, 2023 9:14 am
A regular mains UPS will work.
Of course it will,
the problem is that it's bigger and more expensive (to replace every year or so).
A small, 5v UPS, is small, and cheap to replace after the battery wears out,
so I think that's what I'll do.
I might buy a Super Capacitor and try to create something with it too.

Paul Hutch
Posts: 825
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Wed Mar 22, 2023 10:44 am
ame wrote:
Wed Mar 22, 2023 9:14 am
A regular mains UPS will work.
Of course it will,
the problem is that it's bigger and more expensive (to replace every year or so).
I have had many full size UPS's over the past 30 years. I have never had one fail, or even need a battery replacement, in under 5 years. I've used UPS's from APC, CyberPower and, TrippLite. Also when I've bought them at discount they have sometimes been less expensive than the RasPi specific UPS hats.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Paul Hutch wrote:
Wed Mar 22, 2023 12:35 pm
I have had many full size UPS's over the past 30 years. I have never had one fail, or even need a battery replacement, in under 5 years.
That's great,
and surprising.
I got the impression that the battery requires replacement every 1-2 years.

Paul Hutch wrote:
Wed Mar 22, 2023 12:35 pm
Also when I've bought them at discount they have sometimes been less expensive than the RasPi specific UPS hats.
Full Size UPS cheaper than a RPi UPS module/hat?
On what store(s) were those sales?

timrowledge
Posts: 1430
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Most UPS boxes used lead gel cells and they last quite a while; I’ve recently replaced the cells in my units after about 7 years of use in an area where outages are annoyingly common.
Lithium based batteries can last even longer. My iPhone 3 is still working as a daily use phone. My iPad air2 likewise. You really shouldn’t buy into the whole “lithium batteries can only last 500 days because they have to be charged every day” nonsense.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

timrowledge wrote:
Wed Mar 22, 2023 6:05 pm
You really shouldn’t buy into the whole “lithium batteries can only last 500 days because they have to be charged every day” nonsense.
A rechargeable battery's lifetime is not defined by a number of days,
but by a number of recharge cycles..

And while with a Smartphone/Tablet you decide when you plug it to an AC Adapter and charge it,
with a UPS of some sort (full size, or 5v/module/hat) the decision is done for you, not necessarily in the ideal way to maximize the battery's life.

Paul Hutch
Posts: 825
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Wed Mar 22, 2023 1:44 pm
Full Size UPS cheaper than a RPi UPS module/hat?
On what store(s) were those sales?
It's been a few years since I was in the market for more UPS's but last time I looked I saw Raspberry Pi specific hats with batteries ranging from \$30 to \$45US. The smallest standard UPS's ranged from \$50 to \$60US, so I sometimes caught sales at BestBuy, NewEgg or Amazon and got small UPS's for about \$40.

Another benefit of the smallest standard UPS's is they are about 350VA whereas a Pi only consumes 15VA so you can power quite a few Pi's off of one standard UPS. With the large quantity of excess power available, you can also add on your router/switch/etc. so that the network stays up while the power is out.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

Paul Hutch wrote:
Thu Mar 23, 2023 12:01 pm
last time I looked I saw Raspberry Pi specific hats with batteries ranging from \$30 to \$45US.
RPi specific surroundings can be way more expensive than a more general counterpart.
You don't really need a hat for RPi for UPS,
any "5v UPS" will do, assuming it gives the required Current, or more.

Searching AliExpress for 5v ups, leads to many results, many of them are less than 10\$.

Example:

https://www.aliexpress.com/item/1005005020050993.html

timrowledge
Posts: 1430
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Wed Mar 22, 2023 6:39 pm
timrowledge wrote:
Wed Mar 22, 2023 6:05 pm
You really shouldn’t buy into the whole “lithium batteries can only last 500 days because they have to be charged every day” nonsense.
A rechargeable battery's lifetime is not defined by a number of days,
but by a number of recharge cycles..
Yes, that was the point I was making. And likewise the “number of charge cycles” is not simply the number of times you charge. Partial charging is not the same thing. My iPhone 3 is what, 15 years old and most likely has been plugged into the charger about 3 times a week - getting on for two and a half thousand charges.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

memjr
Posts: 2829
Joined: Fri Aug 21, 2020 5:59 pm

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

You have a requirement, keeping the pi from losing power. If that is requirement number 1 replacing a battery every few years is not an issue.

But you then try to make a point that you don't know what the ups/chip designer did with their algorithms and may be charging a battery to 100% when it is 95%, which is a bit of non-sense because you can see the battery management ic's part number and from that look up its datasheet and know exactly how the thing works and possibly even modify it to your own needs.

But you want the holy grail. Not let the Pi die and also not replace batteries. You can't have both. If you could all UPS out there would do just that because computer UPSes have been around for decades and none of them do it.

If your requirement is that your pi should stay on until it has been able to properly shutdown, an UPS works just fine. And for the price of the Pi UPS hats out there, you might as well go with an off the shelf UPS for 50 or 60 bucks. At least those can recharge while things are plugged into it and virtually every UPS hat I've seen cannot charge if the Pi is still connected to them. These UPS hats out there are a huge waste of money.

If replacing batteries were such a huge deal, very few contractors would rely almost entirely on cordless power tools

Get the UPS, replace a battery every 5 or 6 years, IF you do indeed need to and only after testing it and determining it does indeed need to be replaced..

-

By the way, super capacitors do degrade overtime like batteries too. Regular fast recharging them can ruin them just like batteries go bad with the number of charges. A place I worked at had bots that ran on super capacitors instead of batteries. They needed fast recharging because of the way the system worked. Some clients had spare bots that would go out and replace a bot that went to the charging station to charge slowly. Some clients just had their bots recharge as fast as possible to save money by not purchasing spare bots. Guess which clients were frequently servicing bots to replace super caps? Perhaps the tech has improved lately, but a few years back, charging speed was the factor on how soon a super cap would die.

spaceman5
Posts: 289
Joined: Sun Aug 25, 2019 5:26 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

memjr wrote:
Fri Mar 24, 2023 3:23 am
You have a requirement, keeping the pi from losing power.
If that is requirement number 1 replacing a battery every few years is not an issue.
Maybe you're right.
I should prioritize which vaiable is the most important for me, and go with it.
The only way to get both variables, is If I build the circuit myself..
(but honestly, I prefer to pay, and get a ready-made solution faster)

memjr wrote:
Fri Mar 24, 2023 3:23 am
But you then try to make a point that you don't know what the ups/chip designer did with their algorithms and may be charging a battery to 100% when it is 95%, which is a bit of non-sense because you can see the battery management ic's part number and from that look up its datasheet and know exactly how the thing works
When the enclosure of the product can easily be opened - yes.
(of if it's a module, meaning PCB only withno enclosure, then of course yes)
but some of the products, come with plastic that is welded.
No screws, so a bit hard to open it, unless you want to ruin the enclosure..

memjr wrote:
Fri Mar 24, 2023 3:23 am
and possibly even modify it to your own needs.
Modifying an existing PCB?
Like starting to cut the trails on the PCB, to reroute them and such?
It's possible, but not the style of work I like to do.
If I had to do that, I would prefer to redesign and re-print a PCB myself,
rather to work with something like that..

memjr wrote:
Fri Mar 24, 2023 3:23 am
But you want the holy grail. Not let the Pi die and also not replace batteries.
Now it's a bit exaggeration.
I did not say I don't want the batteries to eventually die,
I said that I do not want them to be worn fast, by bad charging algorithm that maximizes the available capacity,
instead by reducing number of charges.
That's completely different than what you said.
And I do understand that the battery will eventually finish its life,
but at least it wouldn't be as fast as with the first case.

memjr wrote:
Fri Mar 24, 2023 3:23 am
By the way, super capacitors do degrade overtime like batteries too. Regular fast recharging them can ruin them just like batteries go bad with the number of charges. A place I worked at had bots that ran on super capacitors instead of batteries. They needed fast recharging because of the way the system worked. Some clients had spare bots that would go out and replace a bot that went to the charging station to charge slowly. Some clients just had their bots recharge as fast as possible to save money by not purchasing spare bots. Guess which clients were frequently servicing bots to replace super caps? Perhaps the tech has improved lately, but a few years back, charging speed was the factor on how soon a super cap would die.
Interesting, thank you for this info.
In my case I do not need fast charging, so slowly charging a super capacitor would be OK.

ame
Posts: 7145
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

A regular mains UPS will work.
Hmm. What can I put here?

rpdom
Posts: 21842
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Fri Mar 24, 2023 8:37 am
Modifying an existing PCB?
Like starting to cut the trails on the PCB, to reroute them and such?
It's possible, but not the style of work I like to do.
If I had to do that, I would prefer to redesign and re-print a PCB myself,
rather to work with something like that..
Quite often it can be a simple case of changing one resistor. I've modified some battery charge and UPS modules to reduce the default charge current from 1A to 500mA or 100mA depending on the cell I'm using with them.

I've found some powerbank/UPS modules recently that support charging while in use. I'm testing a few out to see if they could run a Pi for a short while from an 18650 cell. I only need a minute of backup power to cover the short gap before my main backup cuts in and can run everything for several hours.

redvli
Posts: 941
Joined: Thu Sep 03, 2020 8:09 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

spaceman5 wrote:
Tue Mar 21, 2023 6:43 pm
redvli wrote:
Tue Mar 21, 2023 6:36 pm
I have made a design myself with an arduino and measured over a year. 8 charge cycles per year,
so nothing compared to how a LiPo in a smartphone is used.
That's fantastic.
That's what I would want from a UPS module.
Maybe I should mention that UPS is just a side-effect. The main reason for own design is that roughly 120W power is needed every now and then and the system is PoE powered (max 12W), therefore the LiPo's (and not 5V but 12V). The PoE PSE is connected to a normal 230V UPS.

MikeDB
Posts: 1878
Joined: Sun Oct 12, 2014 8:27 am

### Re: Protecting a Wall-Powered Raspberry Pi from Power Outages and Voltage Drops

memjr wrote:
Fri Mar 24, 2023 3:23 am
If replacing batteries were such a huge deal, very few contractors would rely almost entirely on cordless power tools
That's because in heavy use the tool dies before the battery You just can't get the quality of tools you used to be able to 40 years ago. But they are much much cheaper - a hammer drill was a week's wages in my dad's time.

By the way, super capacitors do degrade overtime like batteries too. Regular fast recharging them can ruin them just like batteries go bad with the number of charges. Perhaps the tech has improved lately, but a few years back, charging speed was the factor on how soon a super cap would die.
The ones used in trams to take the charge from slowing down into stations do seem more reliable than most. But I assume they have carefully designed charging to spread the current equally into lots of supercaps.

These are now finding their way back into cars such as Lamborghini after initial disasters with them in F1 a decade ago when batteries turned out to be the better solution to take all the charge in just a few seconds of braking.
Always interested in innovative audio startups needing help and investment. Look for InPoSe Ltd or Future Horizons on LinkedIn to find me (same avatar photograph)