Show by Label

Thursday, September 17, 2026

Investigation of a DIY UPS for all Raspberry Pi models

 The Investigation and Prototype


This project will first investigate the possibilities for a DIY UPS system for all Raspberry Pi models, at least that is the goal for the design.

When this investigation is successful, I will design a PCB for and continue testing the solution.

Why do I do this?

Well, I have a pretty long history of building a number of UPS versions for my Pi projects, starting with an elaborate version for my wireless thermostat that was running on the first version of the Pi, the Model 1B and even de Model 1A, now many years ago (2015). That post is on my Blog, search for "UPS". Since then, I designed a number of other attempts to basically protect the SD card in the Pi from power outages or glitches.

My friend Bud designed two very nice UPS systems, in 2018 and 2019, using the HAT module layout, that could use 14500 and 18650 Li-Ion cells. He used a dedicated chip to handle the Pi power and charging (for the Pi Model 3 at the time), and used a micro-controller to keep things "under control". The latest version of that design worked for years for me, and at least two of them in Bud's applications, but recently the one I used developed an issue.

Here is Bud's posting of the latest design with all the details : 
https://hackaday.io/project/162653-u1liupsrpi

It turned out that the IMR cell that I used for several years got bad while in the application, but the UPS also showed an issue that I wanted to investigate. During that process, I successfully managed to create some magic smoke and blew a hole in the main chip, the LTC4040. Replacing that chip has it's challenges due to the footprint, and getting just a single one would cost me too much, and I still would not know if there are other bad parts on the board.

I abandoned that resurrection project as being too costly, cumbersome and risky.

Although I do not have an immidiate need for a UPS, I kept thinking about it and the involved challenges. Bud designed a very nice UPS, much better than even commercially available products, but we were surprised that only very few Makers build one. It's not the lack of visibility because several projetcs we did together, he himself or myself, had a lot of success with Makers, so that could not be the reason. 

Putting myself in their shoes, I realized that there are two main obstacles preventing more global use. 

  • First, the LTC4040. It's not widely available, and not cheap. The other challenge is the package, a 24-Lead 4mm × 5mm QFN that makes it a challenge for most Makers to solder.
  • Second is the controller, a PIC16F18323. The package is OK, but the programming is not. You need to have a programmer and a socket for the chip. Bud published the Microchip MLAB information and the flow-chart, but you have to program the chip yourself.
So, while thinking about another DIY version that is sturdy, reliable and easy to build, I started to piece a few things together that will potentially eliminate some of the challenges that plague most DIY UPS systems for applications like the Pi.

There are actually three rather difficult to overcome challenges:
  • There are many Pi models and they all have peculiar differences for this application, in addition to OS or firmware/ROM/EEPROM related changes at just about every new OS version. The Pi's are all different in several aspects when dealing with the powerdown and power-up processes and that makes the restart a bit of a challenge. There are a myriad of postings on the official Raspberry Pi Forum from users and Makers that struggle with this. (sadly, this constant changing of the OS landscape by the foundation has forced me to avoid the Pi as much as possible, and I now only use it when I have no other solution)
  • The later models, like the Model 4 and 5 consume a lot of power, especially when you use USB adapters. The current requirement can quickly get up to 5A for the Pi or even higher for a loaded system.
  • You need access to the GPIO connector. 
    • You can use it to supply power to the Pi, rather than using the standard USB power input connector. 
    • But you have to use the GPIO connector to tell the Pi that power has dropped in order to let it gracefully terminate running processes and start the powerdown sequence. (only the Pi Model 5 can communicate over the USB power cable connection)
    • You need access to the 3V3 rail or a GPIO pin that drops to GND when there is a powerdown. This is needed to sense the end of the powerdown so you know when to turn off the power. Luckily, the 3V3 voltage rail is available on the GPIO connector.

With the advent of newer Pi models and newer ways to protect or even circumvent the SD card, the UPS functionality is a bit less important for that part, but if you have applications that do not tolerate a sudden power loss or power glitch, you still need protection.

The UPS goals for this project

Rather than keeping the Pi alive for a very long time, the focus will be on riding out power glitches and to provide a controlled power down and restart when power returns. Most of us live in countries where the main power is very reliable and stable, so the focus is not so much on a long period with the loss of power, but a graceful termination of a running one. With this design you could potentially cover long periods of a power loss, but the battery will most likely have to be very different (al least very large) and this is not covered with this project.

The powerdown process will be under control of the Pi itself so you are free to do what's needed to cleanly terminate your processes and then execute the powerdown.

The UPS function only needs to supply power to the Pi for a few minutes. Three minutes to ride out power glitches, and an additional up to four minutes for the shutdown process. This should be long enough to do what's needed, so that will leave enough remaining capacity in the cells to go through another cycle that happends before the cells are fully recharged again.

The switching from main power to the battery power should be automatic and instantenous without glitches. 

To make the design simple, I'm planning to use a normal 12V DC power-brick with up to 5A for the Models 1 through 4 and a 6A to 8A version for a loaded model 4 and 5.
To avoid the issues with Boost regulators that will convert a single Li-Ion cell (between 4.2 down to 3.0V) to the 5V the Pi needs, I elected to use two cells in series. That configuration only needs a simple Buck regulator to regulate the cell voltage from between say between 8 and 6V down to the 5V the Pi needs. 

The charger function will be one that can handle a 2S (2 cells in Series) configuration with all the protection needed for Li-Ion cells. An active and simple balancer circuit willl be used to keep both cells happy and at the same voltage level. This is important because the cells will normally be off-duty for very long periods with only an occasional use. There will also need to be an undervoltage protection, a high discharge temperature protection and protection for back-feeding and a cell short.

In the selected topology, a Buck convertor sits after a diode-OR that is fed by the main 12V and the 2S cell voltage. The Buck regulator reduces the incoming 12V or the 8-6V from the cells to 5V for the Pi. The Pi sees no difference and is not very sensitive for the selected Buck regulator's high frequency switching noise.

The diode-OR switch will be constructed with so called active Ideal Diodes. This will ensure that the diodes will not have a significant voltage drop and nothing will get hot. Besides that, the ideal diode regulator has reverse voltage protection built-in.

The two Li-Ion cells I'm planning to use can be 14500 IMR or 18650 cells that feature a very high discharge current. I'm planning to use two Efest 14500 IMR cells that can discharge up to 9A, but will also provide the option to use 18650 cells that will have more oompf for high current applications.

In the initial investigation, I specifically avoided using a micro-controller and used a discrete component  design based around two timers.

With the above building blocks, we can provide the Pi with power and when the mains drops away for a longer period, it will instruct the Pi to initiate a powerdown all by itself. We will use a Watchdog to keep en eye on that process.

The shutdown process is easy to create, the real trick for all these UPS solutions is to reliably wake-up the Pi when power returns and avoid false restarts. This is where in most UPS designs a micro-controller is used, but we won't have one. A clever state-machine circuit will keep an eye on the power loss and will turn-off the Buck regulator output when the Pi is made powerless after a powerdown. 

The state-machine will also allow the Pi to start normally when main power returns, and will need to avoid interruptions when power comes back too soon. Yes, you can use the features newer Pi models have, but these features are not available on the older model Pi's. Allowing the Pi to only startup when power has been removed completely before it comes back is reliable for all current models.


The Prototyping phase - proof of concept


When I had this rough design done, mostly in my head, I looked for modules that would allow me to build a functioning prototype of the system and test it out.

I could find everything I needed on Amazon, for reasonably low prices and with a fast delivery.
Here are the building blocks:


Li-ION 2S Charger Module:



This is actually a solar panel based charger with an MPPT feature that I won't need, but it's easily disabled. The input is the 12V from the wall-wart, and the output goes to the two cells in series, as if it was only one cell. The MPPT feature can be disabled by turning the trimmer all the way down. The other trimmer sets the maximum charge output voltage for the cells.

The specifications of this module are:
Model: CN-3795-2A.
Input voltage: DC 6,5-24 V.
Output current : max. 2 A
Output voltage range: adjustable from 2,4 V to 20 V.
Can be used for : 1 to 5 lithium-ion cells in series.
Charging modes: constant current / constant voltage plus MPPT.
DC-DC-efficiency: ca. 85%
Module size: 20 x 35 mm.

However, do NOT order this board!
It turns out that the version I got has two serious flaws that can cause a very serious problem as I discovered when testing it. First of all, the Schottky diode (on the left side of the inductor) is a very flimsy one. On my unit it has an 0805 size. The next even more serious issue is that the current limiter resistor has a value of 50mOhm, resulting in a 2.4A current, not the 2A listed in the specification. The diode cannot handle that. In my case, when I recharged my almost depleted 2S set, the diode blew and took the MOSFET with it, supplying an unregulated almost 9V to the cells. This can result in a catastrophic failure.

Only then did I search for another module, and after some time searching, I found the one below. As you can see, it has a much beefier SS54 diode (50V 5A) (just North of the inductor) and a current limiter resistor with a much larger size (3528?) (to the right of the inductor) with a marking of R120, a 120mOhm value, resulting in a 1A current. Still a little too high for my 1C for the 650mAh 14500 cells, but at least the board is capable of handling it.
It also has much better input and output capacitors, a combination of a 47uF/63V electrolyte with a parallel ceramic capacitor. Also note that the inductor has a 47uH value much more in line with the current, while the other board has a 15uH value.

The result of this failure event is that I'm adding safety features to the circuit that will avoid this from happening again.  I will add a temperature cut-out because I found that the 14500 cells got too hot to my liking when I used a contineous 4A discharge current with my DL. Yes, this is unrealistic compared to a Pi Model 1-3, but consider it worse-case. Finally a fuse and a circuit to protect the cells from an under-voltage discharge.



When you order this board, make sure you get it with the MPPT setting of 12V. (it comes in 9V, 12V and 18V versions, and I got the 18V one that I needed to modify (change R15 to 12K to disable it) The 12V version has a max current of 1A.
Look for this one on Amazon:

Solar Power Regulator, 1A 12V MPPT Solar Panel Controller Li-ion LiFePO4 Lithium Battery Charger Module, Solar Power Controller Module

There are also instructions to modify it.

This unit is behaving very well in the tests that I'm running, in combination with the 2S balancer below.


The 2S Balancer Circuit:




The output from the Balancer Circuit goes to the plus of the top cell, the middle to the connection between both cells,  and to the bottom of the second cell. That's it. It simply balances the voltage of both cells to within 30mV and uses a 10mA charge to reduce a higher voltage cell.

Because the controller chip is so tiny (see if you can find it in the above picture) and without leads you can probe, I already decided to create a simple discrete version that Bud designed and does the same and is more DIY friendly.

The Buck Convertor:

This regulator will be used to reduce the cell voltage down to the 5V the Pi needs.

I have number of these little boards, several that use the LM2598S. It has an EN pin (pin5) that can be easily lifted from the PCB so we can switch it off.
The LM2598S even has a softstart/EN input. The EN signal input will be used to switch off the 5V supply to the Pi when the Pi has been through the Powerdown process, so it will make the Pi truly powerless. This is an important pre-requisite for the clean restart of all the Pi models when power returns.

The above one is the one I will use during the prototyping phase, but not in the final design. Please keep that in mind. If you need to buy one, get one of these until I can give you a better alternative.


There are also many other Buck regulator boards that use the XL6009E1 chip. 



However, the XL6009 E1 is a Buck and Buck-Boost regulator, and that poses issues, including some serious ones for this circuit. The EN input is active high and that needs to be taken into consideration. 
Don't use these regulator modules for this design!


Here is one that uses the XL4015, but this chip has no EN pin and therefore cannot be used in this configuration at all.



The typical key specifications for these regulators are:

Input voltage: 4-38VDC
Output voltage: 1,25-36 VDC adjustable
Output current: 0-5A (but stay below 3 to keep the heat down).

Depending on the Pi Model, we will have to select a higher power circuit with at least 5A up to 8A for the PCB design, we'll get to that later.


The Ideal Diode Switch



This circuit uses N-Fet's (very low RDS-on) and the LM74700 controller. It is ground based (many other boards are not and that gives a problem) The LM74700 has an EN (enable pin) that I could bring out for this board, so the Pi can switch-off the battery supply when it has reached the powerdown status. To use an N-FET in this circuit, the LM747001 has a built-in charge pump. It also has reverse voltage protection built-in.

Specifications:
Input voltage: 5-30V  (another spec says 0-50V; that's not correct)
Current: up to 10A

These Ideal Diode boards will need a minimum input voltage to let the LM74700 function, typically 4V is OK. We'll find that out when we're testing.

On the two modules that I got because they claim support for 10A, they use MOSFETS that are an absolute overkill, and have a very large package to boot. The used MOSFET is a HYGO15N10NS1A that featurs 100V and a whopping 360A. Rest assured, I will use more common smaller ones in the PCB design.

The create the diode OR circuit, one of the diode inputs is connected to main 12V, and the other diode to the cell voltage. The outputs from both boards are then connected together and go to the Buck voltage regulator that will create the Pi 5.1V power input.


The Li-Ion High Current Discharge Cells

I used EFEST IMR 14500 650mAh cells before and still have two that have now lost their maximum capacity, but they are still usefull for testing purposes. They can provide up to a whopping 9.75A discharge. I ordered two fresh ones for this project.



The newer Vapce IMR cells are 1250mAh and have a maximum contineous discharge capability of 3A.

With power requirements from 5A upwards we cannot use the 2S configuration for the Vapce cells, but that can only be used with Pi models up to 3.

I also ordered two new 18650 cells with a high quality. I already have two that I purchased several years ago but are fakes. They list 8800 mAh but I measure only just 1132 mAh on one and the other one a mere 676 mAh. They seemed to come from a seemingly reputable German company M2 Tec, but they delivered rubbish to me. Buyer beware! Not only can they be fake in terms of capacity, but worse they can have manufacturing defects that could cause catastrophic failures.

WARNING
Use only reliable suppliers and select well known cell manufacturers.

Dual Battery Holders

I have ordered dual cell battery holders for both 18650 and 14500 cells with smd soldering lugs.



The Powerdown State-Machine Circuit

This is the beating hart of the UPS and is a circuit made from discrete parts that will turn off the Buck regulator after the Pi has powered down. This will provide a clean power-up when the main voltage returns. The State-Machine also has to take care of situations where the power returns while the Pi is still in the shutdown period. This is a key requirement for the clean startup procedure for all Pi models. There is also a watchdog to cut-in when the Pi is not reacting soon enough and there is protection when the cell voltage is too low.


The high current Buck regulator

I ordered this module that is specified for 300W at 9A with an input voltage of 5-40V while the output could be adjusted from 1.2 to 30V. The lowest input voltage of 5V is only 1V above the voltage when the cells get switched off, so that is a test I need to do. It should be sufficient for the Pi Model 5, so I tought...








Because I had to wait for some critical parts, I captured the complete design in KiCad, and used my Google AI friend in a browser to help out when I had questions. I typically do my research on an iPad and when done, transfer the info to my Linux laptop where I do the heavy lifting. I had to go back and forth to share the pdf versions of the KiCad schematics for the agent to use, but now that the conceptual design is mostly done, I can continue on my laptop.

Here are the various schematics of the complete conceptual(!) design. Keep in mind that we will most likely run into Mr. Murphy and that things will change. I will keep the diagrams up-to-date while I'm progressing, so keep en eye out for changes.


The Schematics


Below are the schematic diagrams that follow the conceptual design in detail. They have been updated since the first upload, and I will continue to do so to keep everything current and up-to-date. 

The schematics below do not yet show the final version, things can change during the building, testing and verification. When I'm done with that, they will be available on a special Github project.

The 2S Battery Charger circuit

Note that I had to make some changes after I had a catastrophic failure of the first module I was using.

Circuit Explanation:
Q20 is used to protect the charger circuit from a reverse polarity connection. Q20 is also used to protect from a backflow of the cells to the input. The CN3796 was selected because it is commonly available and comes in a " normal" package. It is a solar panel driven cell charger, but that function (MPPT) is disabled by R40 and R45. The chip has an on-board under voltage lock out (OVLO) when the input voltage drops below 6.5V

I'm using a beefy SS54 (5A 40V) Schottkey diode to have some headroom. Remember that this diode blew on one of my boards. The AO3401A MOSFET has a very low RDSon and with a maximum current of 2A, will not get warm. The inductor value and the current specifications will also keep the losses low and the temperature development low. The value of the sense resistor R38 of 60mR sets the current to 2A when you use 18650 cells. It needs to be changed to a 100mR value when you want to use 14500 cells, in which case the charging current will be 1.2A which is 2C for the 650mAh IMR cells.

The regulation voltage can be adjusted in a fairly small range so it is easier to set the voltage with a single turn trimmer. The range should be between 7.43 and 8.73V, which translates to 3.71V and 4.36V per cell. The recommendation is to stay below 4.2V and for a better longevity, use 4.1V or 4.0V. This limits the available capacity somewhat, but for this typical application, there is enough available headroom.
Note that with higher currents, like 4A or more, as I will show later in this Blog, the actual voltage will immediately drop to about 7.2V, so the available headroom is only about 1.2V.

R46 and C37 is the recommened loop compensation circuit.

I put an LED circuit on the PCB to show the charging status. The LED can be populated on the PCB, or connected to a front panel mounted LED by using the 2-pin connector. You could even use both LED's at the same time. Depending on the used LED's, the current limit resistor R39 of 4K7 may have to be adjusted to get the desired brightness.


The 2S Cell Balancer Circuit

This is the 2S Cell Balancer in the discrete version. This is a different version, and based on a design that my buddy Bud did a number of years ago. It seems simple but has has some nifty features.


Circuit Explanation:
The operation of this analog dissipative (bleed) cell balancer is actually quite simple. Precision resistors R54 and R56 create an accurate mid-point for the combined (2S) cell voltage. That voltage is compared to the actual mid-point, taken from the mid-position of the two cells. If there is a difference, the complimentory MOSFET's  Q22 (N-CH) or Q25 (P-CH), that together form a push-pull source follower, is turned on by U12. This is causing a current through R53 or R58, drawing a bleeding current from the cell that has a higher charge than the other one, removing some charge until they are equal again. In that case, both MOSFET's turn off, with a dead-band created by their different VGs thresholds. 

The clever bit is that because Q22 and Q25 act as source followers inside the opamp's feedback loop (R52 and C51), the circuit regulates smoothly in linear mode, rather then oscillating wildly. The natural gate-threshold gap between the NMOS and PMOS devices prevents shoot-through current from BattA+ straight to PGND.

The amount of draining/bleeding current can be set by selecting different values for these current sink resistors. 47R for a 90mA sink for 14500 cells and for 18500 cells 20R resulting in a 210mA sink.

A major flaw in simple resistor-divider balancers is that they slowly drain the battery when idle. This circuit solves that by using three N-channel MOSFET's (Q23, Q24, Q26), turning it off. Note that Q23 and Q24 are source-to-source wired. A single MOSFET has an internal body diode that would still allow current to leak in one direction when turned off. Putting two back-to-back blocks current in both directions so the battery mid-point cannot leak back into the op-amp when the system is idle.

Note that R55 is a clever addition to the circuit. When charging is finished, Q26 turns on and opens the back-to-back switches Q23 and Q24, disconnecting the opamp from the Gates of the source follower. Without R55, these Gates would be left floating. Any remaining charge in their gate capacitance (Ciss) or any stray electrical noise - would keep one of the MOSFET's partially turned on, draining the connected Li-ion cell completely flat. Normally, a Gate pull-down resistor is tied to GND, or a pull-up to BattA+, however that would always drain the cells. Because both sides of R55 sit at Batt-mid once Q23/Q24 disconnect, there is no voltage across R55 at rest, resulting in a zero leakage current through the Gate resistor.

But wait, there is more. During active balancing, Q22 and Q23 are both off whenever the cells are nearly equal (inside the gate-threshold deadband of +/- 1V) inside that deadband, the opamp would otherwise see a purely capacitive load (the MOSFET gates) with no resistive path, which can cause ringing or oscillation when driving through the series resistance of Q23 and Q24.
R55 gives the opamp a predictable 1K resistive load to drive against around Batt-Mid, keeping the feedback loop well-behaved as it transistions smoothly between top-cell and bottom-cell balancing. 

The combined value of the sense resistors R54 and R56 of 200K cause a drain of about 42uA to the fully charged cells, which is only about 37mAh per month which is negligible for 14500 or 18650 cells.

The UPS Controller Circuit

Following is the hart of the UPS controller with in a first attempt, using a dual 555 timer version. It looks busy, which it is and is also rather complicated, but it helps to understand why I eventually decided to switch to a controller.




The conceptual design uses two 555 timers (re-triggeable one-shots). One to create a 3 min grace period to ride out the short losses of the main voltage. At the end of the grace period, it must be a serious loss of power so a signal is send to the Pi to tell it to start the shutdown/powerdown process. A second timer will be started and acts as a 4 min watchdog to monitor the shutdown process. When the circuit detects the end of the shutdown by the drop of the 3V3 rails, the power to the Pi will be cut, or as a safety, when the watchdog runs out. When main power returns, the Pi should perform a normal power-up boot. This seems rather easy to do, but as usual, the devil is in the details. 

Part of the complication is that the output of 555 timers act different from what you would expect when you factor in that they need to start behaving right after a power-up, besides the fact that the output levels are inverted to what the Buck needs, in order to shut the power off. Then there is the required protection for premature power returns when the Pi is in the middle of the shutdown period. It has to completely finish that and become powerless for a short while before the power-up can be reliably applied when the mains returns. However, if the Pi hangs for whatever reason, the power has to be cut regardless to protect the cells capacity and force the power off state to do a clean power-up again.

To preserve cell energy, there is a little circuit that monitors the level of the Pi 3V3 rail, which will decay after it finishes a shutdown. I use that "feature" to terminate the 4 min WD window so we don't waiste cell energy.

By the time you add that all up, you get to the circuit above. Believe me, it went through many iterations, including LTspice simulation and double-checking many times. I then build the circuit as a prototype (below), still uncovering issues. My buddy Bud and several AI agents were instrumental to get most of the kinks out of the circuit and at this stage, there is still one major shortcoming. If you spot it in the schematic, make a comment to the Blog.




Above is a picture of the breadboard that I used to test the functionality in real life. 
Unfortunately, it still needs more work to cover the last condition and that will require even more parts to get it working under all conditions.
Reconsidering the original goal
The original goal was to create a rather simple discrete circuit that could be easily understood and used by DIY Makers. However, it's already too complex and requires too many parts, and still may not fully function under all conditions when you factor in part tolerances and different manufacturers.
Therefore, I've decided to switch horses and use a processor. The plan is to use a castellated ESP32-C3 mini board that can be easily soldered onto the main PCB, and is rather simple to program and also simple to make software modifications. 

The schematic diagram is a lot simpler compared to the discrete version, even though there are several additions and refinements:



Using a controller allows us to add a few functions that are easy to implement.

Circuit Explanation:
I've added a special way that Bud designed to measure the cell voltage with a higher resolution, using the on-board ADC. If that notoriously poor ADC poses too many issues, I'll swith to an external i2c one. 
Q14 acts as a diode with two functions. One because we want the VBE to compensate for temperature changes in Q12. And two, it compensates for the VBE drop of Q12. In essence, 5V is subtracted from the 2S cell voltage and then further reduced to get in the lowest ADC range without clipping.
I've also followed Bud's suggestion to add a small optional circuit to measure the 5V rail so fluctuations can be factored in with the V_Bat readings. This is important when you run the controller with the USB cable connected when you test or program the ESP and that will result in a slightly different 5V rail voltage.
I've added an optional status LED that will show the various States the controller is in. On some ESP-C3 mini boards there is already a LED or even a RGB led that can also be used. The software will support both versions.
Note the Schottky diode in the +5V supply for the ESP32 module. This tiny board does not have the usual blocking diode on it to prevent backfeeding of the board supply to the USB or vice-versa. This is important when you connect the USB to the board to program it etc. This is specifically "forbidden" in the documentation when the board already receives power from another source. In most situations, designers use a jumper but I find that to be error-prone.
Because we are now running with a processor that has 3V3 level GPIO pins, I added a MOSFET (Q13) to drive the EN pin of the LM25116. The datasheet says that the voltage on that pin should be greater than 3V3 to enable it, so to be safe I added a level convertor. That changes the activity level of the Buck_EN and is reflected in the software.
The low-drop 5V regulator U14 needs almost 6V to stay in regulation, and this voltage level determines the minimum cell voltage of 6.0V that we will need to adhere to in the UPS system. It limits the total capacity we can draw from the cells, but it is what it is.


Testing it on a prototype

I built the ADC front-end and tested the software framework on an ESP32 Devkit while waiting for the C3 modules to arrive:



I used my dual Lab Supply to provide the cell voltage, and changed that with 100mV increments/decrements while noting the measured ADC voltages. The other channel of the Lab Supply provided the 5V rail. The result is very lineair form top to bottom.

The ESP32 above has two red LED's connected, one for the EN output and one for the Status signal.

I have now also started a section below called Software Design.


The Ideal Diode OR Circuit

The Ideal Diode OR circuit either selects the Cell voltage or the 12V main supply that will go to the Buck regulator.


Circuit Explanation:
The reason for selecting the active ideal diode OR instead of using two Schottkey diodes is actully quite simple. Schottkey diodes have a voltage drop that is a problem with the cell voltage, we need all of it. The voltage drop and the diode dissipation at 8A for the 12V supply and an even higher current to maintain the cell voltage for the 5.1V output is way too much.

Ideal diodes that use MOSFET's only have a minute voltage drop, caused by their RDSon resistance. The selected AOD4184A N-channel MOSFETS (40V 80A) only have a 4.6mOhm RDSon resistance resulting in only a minute voltage drop and virtually no temperature rise. N-channel MOSFET's typically have a lower RDS-on than their P-channel counterparts, but need a voltage higher than the supply voltage. This is why I selected the LM74700 Ideal Diode Controller. It has an enable function, and a charge pump.

They do need to be protected by a TVS diode, which I added to the 12V main input anyway (D13). I also added a Polyfuse F11 for extra protection that needs to be selected based on the maximum current you want for the UPS to supply.

I deviated somewhat from the datasheet by adding 100R Gate resistors and a small 1nF capacitor to act as an LP filter to protect the Gate of the MOSFET's.

The Ideal Diode for the cell side uses the EN pins of the LM74700 to act as a teperature sefety switch, but can also be used to turn the cell voltage off from the Buck regulator, again for safety.

To protect for high temperatures of pecially the 14500 cells during the discharge, I'm thinking of using a N.C. 65 degrees C temperature switch to cut the power from the ideal diode. The tab can be positioned in-between the two cells.

The output of both Ideal Diodes are connected together to for the OC circuit, and the resulting voltge goes to the Buck regulator circuit. The same output also goes to the low-drop 5V regulator that is used to power all other circuits. That regulator is located on the Controller Circuit section.


The Buck Voltage Regulator

This regulator should bring the input voltages from either the 2S cells (6-8V) or the 12V main supply down to 5.1V for the Pi. My design will support currents up to 8A to power all the Pi models 1 through 5, even though 5A is the specified maximum for the model 5. There are optional component changes on the PCB that can be made to lower the current requirements for the less power-hungry Pi Models. Unfortunately, Buck regulators that can provice 8A or more at 5V1 with an input voltage down to 6V are very rare and mostly available in a tiny VQFN-20-EP (3x3mm) package. This will pose a significant road-block for most DIY Makers, so I abondoned this approach.

After searching hi and lo for suitable circuits, I only found very few chips that could be used. Again the challenge is to find parts that are generally in stock at many suppliers, and that turned out to be a much larger problem than just finding a chip. (BTW, I always check LCSC.COM, TME.EU, Digikey and Mouser, in that order) 

The more modern designs are all lead-less. We also need a full synchronous Buck architecture, one that uses two MOSFET's, not one with a Schottkey diode. Unfortunately, most of these chips use many more external components for option selection and compensation of the internals. In any case, I found the LM25116MH from TI that fits the bill for this application. The stock position is not great, but is generally speaking available and orderable in quantities of one for just over $3.




Circuit Explanation:

This earlier generation chip needs a number of additional parts that are otherwise integrated in the newer chip designs. These parts are most notably the two NFET's the parts to drive them and also loop compensation networks. The good news is that it has an "Under Voltage Lock Out" or UVLO function that will turn the output off when the incoming voltage (from the cells) is getting too low, and it has a hysteresis to turn the output back on. This means that we will not need the TL431 based circuit that Bud designed and that saves a few parts. (no longer shown)

The NFET's I selected are inexpensive, come in a TO-252 package, do not take a lot of space and are easy to solder. They are both different versions, specifically selected for their task.
  • The High-Side IPD60N03LG:
    At 200 kHz, its 19 nC gate charge ensures the LM25116's gate driver can charge and discharge the gate capacitance instantly. This sharp transition practically eliminates switching overlap losses, keeping the high-side FET running very cool.

  • The Low-Side NTD4804N:
    When the 2S Li-Ion pack is fresh and sitting at 8.2V, the low-side synchronous rectifier has to pick up the free-wheeling inductor current for roughly 40% of the cycle. Dropping the on-resistance down to a tiny 4.0 mR gurantees that conduction heating remains negligible, even at a sustained 5A draw from the Pi Model 5.
I have added a 4.7 Ω in series with the high-side gate, which is a classic, highly effective trick for managing Electromagnetic Interference (EMI) and high-frequency ringing.
  • The Benefit: The trace from the LM25116's HO pin to the gate acts as a tiny inductor. Paired with the gate's internal capacitance, it can create an LC resonant circuit that "rings" (oscillates) dramatically when switching. The 4.7 Ω resistor dampens this ringing.
  • The Trade-Off: Slowing down the gate turn-on time slightly reduces EMI, but it increases the time the MOSFET spends in its linear region, slightly increasing switching losses. At 200 kHz, this added loss will be practically invisible, and the FET (with its low 19 nC charge) will handle it easily while running cool.
  • The Reality: In a synchronous buck converter, you want the low-side MOSFET to turn off as fast as humanly possible to maximize efficiency and prevent dead-time conduction.
  • The Insurance: You could start with a 0 Ohm resistor and if the board shows any minor high-frequency oscillations on the low-side gate trace during heavy load tests, you can easily desolder the 0 Ω jumper and pop in a 2.2 Ω or 4.7 Ω resistor to clean it up without spinning a new PCB. I will start with the 0R resistor.

The 10 kΩ resistor from the low-side Gate to Source is the perfect safety net for a DIY Maker board.

  • The Mishap It Prevents: When the circuit is powering up, or if the LM25116 controller ever enters a high-impedance reset state (like when the UVLO circuit cuts out at 6.0V), the gate of the low-side MOSFET becomes "floating". Without a pull-down resistor, stray electrical noise or capacitive coupling from the switching node can accidentally turn the low-side FET partially on.
  • If the low-side FET turns on while the high-side FET is active, it creates a dead short circuit across the battery pack (known as shoot-through), which instantly destroys the MOSFETs. The 10 kΩ resistor guarantees the low-side FET stays firmly locked off whenever the controller isn't explicitly telling it to turn on.

Under Voltage Lock Oout (UVLO)
The undervoltage safety for the cells is now implemented with the LM25116 UVLO function. The resistor values I have choosen cuts the power to the Pi when the cells are below 6V and turn back on when the cells have recharged to 7.4V to make sure they have enough capacity for another cycle.
Cutting the UPS at a cell voltage of 6V seems a waist of cell energy, however, we need at least 6V for the 5V rail to function. I have selected a low-drop regulator, but we need to stay safe.


The LM251116MH comes in a 20-pin TSSOP package, that also has an EP on the backside, I redesigned the footprint a bit such that it is relatively easy to solder the EP with just a solder iron.



I extended the copper for the EP beyond the outside of the chip such that you can use a solder iron to heat the sides and let the solder flow. In this application, the chip itself will not get very hot so it's not a deal-breaker.

The switching frequency has been selected to be 200KHz and is set by R48 and C55. The ramp-up slope is set by C54 and is selected to provide the Pi with controlled power-on slope.

The UVLO or Under Voltage Lock Out (R61, R51, C38, D18) is used to protect the cells from getting lower than 6V. This value is required to provide the 5V rail low-drop voltage regulator with ample headroom to supply all parts in the controller circuit.

The charge pump/high side driver required for Q27 is formed by C58, D12 and C39.

The maximum current is set by the current sense resistor R65. With a 7.5mR value it sets the limit at 6V to 9.7A. 

L12, the 4.7uH inductor needs to be selected for the switching frequency, the minimal resistance and the maximum currents (also saturation) to prevent losses and therefore high temperatures.

Particular care has been taken in the selection of the input and output capacitor selection. You need to take the voltage deration into account to make sure you have enough capacitance to prevent instability at the input, and filter the output.

The 5.1V output is set by the feed-back circuit of R47, R49 and is loop compensated with C57, R48 and C55.


Conceptual design done (for now)

Now that I have that conceptual design under my belt, I was able to order most of the critical parts. While waiting for them, I can continue with the modules.

I have already used the charger a few times with different cells, and tested the cell balancer. This was with the older Efest cells that were not in a good shape, so discharge testing was minimal. In he meantime I have received two fresh Efest IMR 14500 cells, and fully charged them. With these cells I can run some tests with the ideal diode and the 5A discharge, using my Dynamic Load in the Battery Test mode.


Testing the cell discharge curves.

These are made after the cells got fully charged by the CN3795 charger circuit. After that, they rested for several hours up to more than a day.
The discharge is done by connecting the Dynamic Load leads right on the battery cells themselves while also using the sense leads. 

Later I will do the same with the Ideal Diode and the Buck regulator added, and connect the load to the output of the Buck regulator to account for all losses (mostly the Buck circuit).

14500 cells for the 5A version

Here is the Vapse F12 14500 cells with a 4A continous load. These are brand-new cells that have only been discharged and charged once. They got pretty hot at the end of the discharge, and they will need a temperature safety precaution that will terminate the load.

The screenshot below is created by the Dynamic DC Load that Bud and I designed a few years ago. The design is covered in another post on this Blog. One of the modes of the DL is the Battery (discharge) Mode that you configure using a USB connected PC. The software we wrote also shows the discharge curve in real time, shown below. This application is designed for Windows, Apple and Linux as I'm using myself.


In this screen shot you can see what happens with the cell voltage when the load is applied and also when removed shown at the Recovery Start. The cell voltage shoots up and starts to gradually climb further up when the cell chemistry reconfigures. This is an important effect when you measure the cell voltage and determine actions.

Here is a 5A discharge run for the Efest IMR 14500. This cell can deliver 9.75A peak. After the discharge ended, I measured 57C on these cells, so again they need to have a temperature cut-out feature, that I was planning to add anyway. These cells are new, but have been through this treatment a few times now.



18650 cells for the 10A version.

Here is a discharge curve for the Vapcel K30 18650 with a load of 8A. These cells only have been charged once. These cells only got warm, but no alarming temperature development.


My Dynamic Load does not allow a larger discharge current than 8A, we added that limit for safety reasons. I need to change the firmware to let it go up to 10A.


Just for kicks, to show you what rubbish is being manufactured and sold if you're not careful. Here is the attempt to get any capacity out of two cells from M2 Tec Germany with cells they claim to be 8800mAh. I had to go as low as 1A to get any meaningful results. Have a look before they are dumped in the battery graveyard.
(BTW, the hash in the beginning is a bug in my graphing code, added after completion of the test run.)


Buyer beware!


Broken charger board.

While testing the discharging of various 2S cells, I noticed an issue when I wanted to recharge the half-depleted cells again using the charger module. The charger would not charge them with the high current, but only used the trickle current of around 30mA. At first I was suspecting the CN3795 chip archtecture, and was led astray by an AI agent, that claimed that the CN3795 had a known issue with getting stuck in the trickle charge mode. 

The true explanation was that the window comparator for the cell voltage was stuck because my cells recovered to 7.4V after a complete depletion test down to 6V. No matter what I tried, I could not get it fixed. While investigating this, I used my DSO to look at the waveforms (PWM) from the chip, the MOSFET, the diode and the output and in principle, they all looked good and clean. That was reassuring. 

I started to suspect an el cheapo XR5 capacitor for C4 that is in the COM path, so I replaced it with a X7R version, to no avail. The DSO showed that the MOSFET worked, the chip worked, the diode worked and the coil worked. I could still set the output voltage with no load, but didn't get any current out of the module. 

Upon very close inspection with a very good magnifying lens, I noticed a minuscule tiny brown spot on the sense resistor. Aha, sure sign of trouble. The value was supposed to be 0.12Ohm. It turned out it had changed personality and was now a 4.7Ohm imposter. Duh. Replacing it with a 0.1 Ohm 1206 resistor fixed it. On my own PCB I will increase the size to a 2512. The root cause is not clear to me, it may have happened when I inserted or removed cells from the cell holder, which I'm doing a lot. This may also have been causing the demise of the other charger, that blew the diode and a MOSFET and maybe even the chip itself.

First test with the complete chain of modules

So, for the next step, I connected the output of the charger and the 2S cells to one of the Ideal Diodes and connected the joint outputs to a high current Buck regulator, the one I was going to use externally for the Pi Model 5. The output was first set to 5.1V and the current limiting way up. The output of the Buck convertor was then connected to my Dynamic Load that was going to act as a Pi. Channel 1 of my Lab Supply was powering the charger with 12V at 1A. Channel 2 was also set to 12V, but with the maximum 3A and that supplied the 12V that went to the other Ideal Diode, acting as the mains connected wall-wart. So by simply turning the Lap Supply channels on or off I could simulate the drop of the main voltage. 

I started with channel 2 off and channel 1 for the charger still on. For the first test I simply shut channel 1 off too, so the cells now supplied the voltage to the Buck regulator. While monitoring the cell voltage, I started applying the Constant Current load of the DL and increased it first to 3A, then 4A and finally 5A. It all worked, but the Buck regulator suddenly started smelling and got extremely hot, so I terminated the measurement.

After the heatsinks were cool enough, I toned down the current to 3A and tested the switch-over of the mains, by first applying the main 12V and then turning both channels off simutation a loss of the mains. I looked at the output of the Buck with a DSO and saw no glitches or anything else, as was to be expected. So in priciple it all seemed to work, expect for the Buck temperature issue.

Time to regroup. 
The Buck was specified for 9A and 300W, with a minimum input voltage of 5V, that's why I bought it. 

What actually happens is that when the Buck regulator gets an input voltage below say 7V, the MOSFET no longer get a full PWM input but at maybe 90% is almost fully turned on to supply the 5.1V output and while virtually in the lineair region it will get extremely hot. 

So with this particular Buck regulator design, there is a real noticeable jump in the temperature when the cell voltage drops below a certain value and the PWM almost stops. This makes this specific Buck convertor design unfit for this application.

The remedy is to use a different architecture for the Buck, as an example, one that is synchroneous and has a 4 switch Buck-Boost convertor system that can raise the input voltage when it is too low, and then regulate it down. The most common versions I found are based on the LTC3780 and I got one of these:


At least this vendor is more precise (honest) about the specifications.

Features:
Input voltage: DC 5-32V; (the LTC specsheet says 4V)
Output voltage: DC 1V-30V infinitely adjustable; the standard is a 12 V supply
Output current: 10A (max), long lasting within 8A
Output power: 80W, peak value 130W, more than 80W, strengthen heat dissipation
Output wave: 50mV (12V rotation on 12V, 5A measured)
Short circuit protection:
Reverse connection protection: no
Output prevents reverse polarity protection: no
Operating temperature range: -45 ~ +85 °C.

This is a simple version, and I have no hopes this will work for the pi Model 5. It does not have enough Wattage but I can use it all the way to the Pi Model 4 I hope. The design is interesting enough for me to study it.
There are other versions using the same LTC chip that have a large and thick heatsink on the bottom, but for my testing, this version will do.

Conclusion for the Buck(s)

I have decided to take another approach and found a circuit that I can put on the PCB itself that will fullfill the requirements for all Pi models.

With that decision made, and a succesful test of the modules, I can now finish the design in KiCad and now that all components and their footprints are decided, can start the layout process.


PCB Layout

In principle, the layout is now done, and I'm checking and fine-tuning it. This will not take too long I hope such that I can put the fabrication order in at PCBWay, my sponsor.

[update] the Gerbers are uploaded to the PCBWay website and I'm waiting for the go ahead with the sponsoring.





Here is a 3D viewer picture:






The large pads on the back are for the two dual cell battery holders, one for the 18650 and one for the 14500 cells.


The layout went through a number of refinements after I asked my buddy Bud to also have a look. He is a specialist and pointed out a few things I needed to improve.

PCBWay generously sponsors the activities I publish on this Blog by producing and sending me the PCB's free of charge. This allows me to spend my limited funds for this hobby on parts.




Software Design

In the meantime, I configured my VSC environment to start the software design. I do not yet have the ESP32-C3 yet, they are on their way, so I'm using an ESP32 Devkit to test the software framework.
The code is working and I can successfully cycle through the various States of the UPS simulating the inputs and monitoring the outputs with LED's.
As soon as I get the C3's I will wire that up and can also start to test the ADC measuring circuit. The ADC of the ESP32 Devkit is notoriously poor in the lower input ranges so that can wait. I did prototype and test the V_Batt Cell Window Measurement front-end for the ADC and that works really nice and is very linear. If we need the optional V_Vcc measurment to add more compensation to the V_Bat measurements will be determined when I have the PCB in hand, for now that circuit is added to the schematic.

Who's in Charge?

The major decision I made was to make the Controller in charge of the UPS. This means that power to the Pi will only be provided when the Controller has deemed that to be OK. The alternative is to let the main power decide the fate of the Pi, but then there is no good way of knowing that the Controller is functioning. My number one priority was to protect data integrity of the system, and in that case, the Controller should be in charge.

The State Machine

Here is a short description of the different States the UPS will go through. There are several additions for the debugging phase.
The firmware is a non-blocking state machine. State changes are printed on the serial port
only when the state actually changes. `PWR_Good` is sampled once per second and held
between samples, so contact bounce and noise are ignored.

### Power-up
At cold startup, sample `3V3_sense` and initialize `Request_shutdown` low and the buck off.
Wait for `PWR_Good` to be high; then enable the buck and flash Status three times, 1 second
on and 1 second off. Then enter Idle.

### Idle
This is the standard State. Monitor `PWR_Good`. When it goes low, enter Grace. Flash Status for 0.5 seconds once every 5 seconds.

### Grace
Start a 3-minute timer. If `PWR_Good` becomes high, return to Idle. If the timer expires,
or the cell voltage is below 7.4 V for 3 consecutive 1-second readings,
set `Request_shutdown` high and enter ShutDown. The battery voltage is printed once
per second. Flash Status for 0.5 seconds once every 2 seconds.

### ShutDown
When entering this state, the Pi has to finish the powerdown and will be made powerless.
This process cannot be interrupted.
Start a 4-minute watchdog timer and record if `PWR_Good` returns high during this state.
Keep `Request_shutdown` high; it is a command, not a feedback signal.
Detect shutdown completion when `3V3_sense` goes low.
When completion is detected, or the watchdog expires first, disable the buck to make the
Pi powerless and enter the Recovery state.

While in ShutDown, the battery voltage is printed once per second. If it falls below 6.4 V,
a warning is printed once. This is log-only for now; it does not disable the buck. The status
LED should be turned on without flashing.

While the buck regulator is enabled, flash Status 0.5 seconds on and 0.5 seconds off.
Keep the Status LED off while the buck regulator is disabled.

### Recovery (power-up after ShutDown)
If `PWR_Good` returned during ShutDown, wait 10s after disabling the buck,
clear `Request_shutdown` low, then enable the buck and enter Idle so the Pi sees a
definite power interruption. If power did not return during ShutDown, remain here
until `PWR_Good` is high, clear `Request_shutdown` low, then enable the buck and
enter Idle. While waiting, flash Status 0.5 seconds on and 0.5 seconds off.

## Watchdog
The ESP32 task watchdog (10 s) reboots a hung controller.
The gate pull-up keeps the buck off until the state machine restarts, so a watchdog
reboot cuts the Pi's power briefly. 

At first, using the WD seemed a good idea that I use in most of my designs. 
In this case however, this creates an unwanted side-effect so I disabled the ESP WD 
and instead will harden the application later to prevent a hung process or a condition that will
not restart the Pi.

Note that the battery is protected by the hardware UVLO protection from the Buck.


Testing out the software


Below is the ESP32-C3-Mini with the cell measurement circuit. With the two red jumper
wires on the bottom, I can simulate the PWR_Good and 3V3 level so that I can test the State machine.
The red LED shows the state of the EN signal.
This Waveshare board that I'm using below has the RGB led on it. I also have just received alternative
boards through Aliexpress that do not have this special led on it, just the power led and a normal
one that can be use to show the status. There were some complaints on the net that they would not
program or function well but the three I got and tried worked without a hitch.




Eventually, the final software sources with it's environment and build instructions will be available on a special Github Project.

Construction

In the meantime, the PCB's arrived and the remaining parts will be here in a few days. With an assembled board I can start to test and evaluate the proper operation.


The boards look great as usual. The silkscreen is very crisp even though in many places I used a smaller font to get all the text close to the parts. The LTC25116 has pads that are tightly spaced but the pads and the silkscreen look great also. This will help a lot applying solderpaste and with the soldering.


I will soon show some more of the test results here.



Stay tuned, for more...