Datalogging with power-loss protection & How to make a battery backup circuit if mains fails (using mosfet)

Hello,

I've been thinking about a power loss protection feature for a datalogging application on a 3.3v ESP32, which is powered from a mains power supply.

The data logging I'm doing is saved in ascii onto an SD card (for excel exporting later). Lets say that 25 bytes are printed to the sd card every 30 seconds, using the "file.print" function in the standard SD card library.

When using the file.print function alone, the data only actually gets written to the sd card after every 512 bytes are file.printed. You could just keep executing the file.close function to force it to write to it, but I've read this is not a very efficient thing to do.

That means that if power is cut, a lot of the data that is still in this buffer will be lost.

What if you initially wrote the data to an external FRAM (which I assume gets data written to it immediately, and not placed into a buffer like the sd card), and then continually dumped the data to the sd card every time the FRAM gets filled with 512 bytes. Then you wipe the FRAM and repeat. Each byte of FRAM is rated for 10 trillion writes, so there's virtually no problem with wear levelling.

If power is lost, the FRAM has retained the data that did not get written to the SD card. Once power comes back on, the FRAM dumps that unsaved data onto sd card.

Sounds like a good idea? I have a 8 Kbyte SPI FRAM available, so If I was to implement this, could I use an SD card and an external SPI FRAM on the same SPI bus without many issues? I've read that some SD card modules don't play well with other device's on the bus with 5v logic, but since I'm using 3.3v, would it not be much of a problem? Otherwise, cheap i2c FRAM exists, or software spi.

cheers

I think that that is only relevant if you transfer lots of data in very short time; that does not seem to be the case for your application.

One could choose to use the mains power to run a battery charger, LiFePo4. The battery charger can maintain the battery charge. The battery can feed into a 5V regulator. The 5V regulator can go to the ESP32. Now when the mains power is lost, the ESP32 does not know.

What I described, in the basic, keeps my Raspberry Pi running for 12 hours when mains are lost. Basically, a UPS.

Ah ok, maybe I'm overthinking it then. Thanks.

For another application I might want to log 10 bytes to SD card every 1 second. Would executing file.close every second in this case be very inefficient?

thanks

Why don't you time it?

  1. record start time (e.g. millis() or micros())
  2. open file
  3. write data
  4. close file
  5. record end time
  6. subtract to find out how long it took

What other processing are you doing, that requires you to be efficient? It matters, because latency and throughput, are different things.

sounds like a good idea. thanks!

Also note that even if the 512 bytes are written to the card, that may not be reflected in the directory entry for the file if it's updated only when the file is closed. So while the data is on the card, you would not see it when reading the file on your computer. You could test this and see how your SD library handles things. Write 512 bytes to the card, then close the file, then write another 512 bytes to the card, but cut the power without closing the file. Then see what shows up when you read the file. My guess is only the first block would be visible.

But I agree with Idahowalker that a mini-UPS may be the best way to go. Does your existing mains supply provide 3.3V, or is it a 5V brick? How much load current does the whole ESP32 circuit draw? Does it ever go into low-current sleep mode? If your mains brick supplies 5V, you could use something like this:

https://www.amazon.com/Diymore-Battery-Shield-Raspberry-Arduino/dp/B0784FPF8J

That's for an 18650, but may not terminate charging properly depending on the load situation. I'd like to know more about Idahowalker's LiFePo4 circuit.

Thanks for the info! I'm using a 5v brick. I will try to get some more info on the power consumption, as well as explain the project, in a few days.

Don't bother with the efficiency. It is bad practice and will eventually lead to grief.

I can’t quite figure out the current requirements of my project yet, but basically what I’m trying to make is a programmable PID temperature controller (with wifi), which runs firing schedules for my kiln, and makes data logs of the firing schedules to the SD card.

The Set temperature, actual temperature, Time, and a few other variables are logged to the SD card every 30 seconds.

Executing a file.close every 30 seconds doesn’t seem like a bad approach, based on a lot of data logging examples I've seen online,

Having the data logged to the SD card every 30 seconds could also have another purpose, in that it serves as a save-state for the firing schedule. I.e. If the power is lost during a firing schedule and then turned back on, the program searches through the datalog to the latest data entry before power loss. The latest data entry contains the last logged set point, segment and/or hold time, etc, and it resumes the firing schedule from that point.

This should work fine, but I am toying with the idea of having a save state created within a second of the power being cut. This is a feature of conventional PID controllers you can buy online. A few ideas have come to mind on how to achieve this;

1. Logging to FRAM every second;

Saving the current program variable’s to specific memory locations in the FRAM every single second. As there are Trillions of write cycles in each location of the FRAM, there would theoretically be no chance of wearing out the FRAM at this rate. This also avoids the need to detect a power loss and backing everything up using either capacitors and batteries. At the same time that the FRAM is doing this, the SD is making a data log every 30 seconds, as before. If power is lost, the FRAM is checked and the program is resumed at the last save state in the FRAM

2. Implementing a battery backup circuit to detect the power loss and save the variable's to the SD card

This diode ORing circuit seems like the easiest way to do this;

  • When mains power fails, a battery backup takes over. When the microcontroller detects that mains power is lost (perhaps done through a mains voltage detector, like an optocoupler module, or AC voltmeter), a save state is created on the SD card and the ESP-32 shuts itself off through actuating a latching relay.
  • There's also a possibility that I could have the kiln's alarm go off when this power loss occurs, to notify.
  • I'm also assuming that such a battery backup could save the electronic components in an AC brownout event.
  • This also solves my original concern about executing too many file.close’s. A file.close would only need to be performed at the end of a program, or when mains power has failed.
  • Since the power cut would be a rare event over the Kiln controller’s life-time and it would only need a very brief amount of backup power to save the variable’s, the life-time of the batteries would seem to be equivalent to their shelf life. non-rechargable lithium batteries seem to have the longest shelf life.
  • It sounds like the same backup mechanism could also be done with a supercapacitor, but I don’t quite know enough about them yet, or how much current the circuit will be drawing.

It just seems that with battery backup, there would be no need to save everything and shut down. If you had several hours of runtime on battery, would that be enough to just warn you that power had been lost, and then you could manually shut it down if you want?

You could monitor the battery voltage using a resistive divider, and trigger a warning if it goes below, say, 3.5V. No need to monitor mains voltage. If the battery is 4V, you don't care whether mains is still on or not.

Your two-diode method will work just fine. But if you use Schottky diodes, the voltage drop will be much less. Where this matters of course is in the battery line where a fixed voltage drop is a reduction in battery life. Using a mosfet and a resistor in place of the battery diode would eliminate any voltage drop at all in that line. And since you only need 3.3V for your ESP, you would just need a TP4056 charger/protection module, a battery holder, and the mosfet/resistor/diode circuit. You presumably already have the 3.3V regulator.

Thank you!

I've found what I believe is the same circuit to yours, but adds in a second Schottky diode, so as to not cause the mains supply to charge the non-rechargeable battery.

source: ups - Backup power for adsl modem - Electrical Engineering Stack Exchange

Am I correct in that this circuit would allow the battery voltage to be higher than the Main's supply voltage, and continue to ONLY supply battery power when the mains supply is lost?

My thinking is to use 4 x AA lithium non-rechargeable batteries (6V+ total) and the Mains supply is 5V+ .

Both power sources go into a 3.3V buck converter that powers the ESP-32 etc, but I've also recently discovered that "buck-boost" converter's exist, which might help when the battery drains enough, however, that shouldn't happen for many years with the extremely infrequent draw on the battery.

No, it's not the same circuit as mine. Mine doesn't need D2 because the mosfet is oriented in the opposite direction, so the mosfet's body diode provides the same battery protection as D2 in your circuit. The problem with your circuit is that D2 produces the very voltage drop that the mosfet is supposed to avoid.

I would have to think about it, but I don't think either circuit would stay on the 5V mains supply if the battery voltage was materially higher than that. In both circuits, if the voltage is higher by the Vgs threshold of the mosfet, it's going to turn on. In my circuit, there is an additional path through the mosfet's body diode which would kick in at about 0.7V. I think both circuits are designed for the battery voltage being lower than the mains brick voltage. But you might think about using three primary lithiums instead of four.

hmm, well 3 lithium AA batteries (4.5v) will still supply plenty of current anyway, so maybe I'll stick with that.

This is the first time I've used a Mosfet, so bear with me, but does the battery supply voltage AND the mains supply voltage need to be above vgs(th) max for both circuits to function properly?

This might be a more clear example of the circuit I provided; P Channel MOSFET to do automatic switch between battery and power source - Electrical Engineering Stack Exchange

thanks!

If you're looking at Energizer Ultimate Lithium, I think you will find that they are close to 1.8V if there's no load. So three of them would be the right number.

Vgs threshold is the difference between the gate voltage and the source voltage. The mosfet will turn on only if the difference is greater than the threshold voltage. However, that's just when it begins to turn on.

In the case of a P-channel mosfet, that difference is negative - the gate voltage is below the source voltage. So when the mains supply drops out for some reason, the resistor pulls the gate to ground, and the mosfet turns on. That should work fine in these circuits. The problem is if the mains voltage, even when present, is lower than the battery, then the threshold voltage could still be exceeded, and the mosfet could be on when you don't want it to be.

Here's a Microchip app note on this kind of circuit:

http://ww1.microchip.com/downloads/en/AppNotes/01149c.pdf

You can see that the mosfet is oriented so the drain goes to the battery, and the source goes to the load. But I think the assumption there is the same as mine - the mains supply is at a higher voltage than the battery. If you actually want to switch to a lower voltage supply when it is present, I think that's going to be a more complicated circuit than any of these.

I think I'm understanding this now, thank you!!!

Lets me run the numbers and see if I'm understanding it correctly. Sounds like you are right in that the battery voltage CANNOT be significantly higher than the mains supply;

[update: might've got a bit mixed up, check the updated answer at end of post]

Example 1: Battery voltage NOT significantly higher than mains

Mains voltage = +5V
Battery voltage = +5.4V (1.8V lithium * 3)

Mosfet used in diagram (DMP1045U) has VGS(th) max of -1V.

P-FET Mosfet is turned on when VGS(th) > VG - VS

Battery is connected to drain, and exits at source, and has a small voltage drop (1v typical), so drain = 5.4V and source = (5.4 – 1) = 4.4V

  • When mains power is ON
    • Gate is +5V
    • Source is + 4.4V
    • Vgs = VG - VS = 5 – 4.4V = +0.6V
    • 0.6V (Vgs) > -1V (vgs threshold max), so mosfet is OFF.

(also note that the minimum vgs(th) is -0.3 for the DMP1045U, so we are 0.9V away from the mosfet beginning to turn on)

  • When mains power is OFF
    • Gate pulled to GND, so Gate = 0V
    • Source = + 4.4V
    • Vgs = Vg - VS = 0 – 4.4V = -4.4V
    • -4.4V < -1V, so Mosfet is turned ON

Example 2 when battery voltage is higher than mains voltage:

Mains voltage = +5V
Battery voltage = +8V

The source will equal to 8V - 1V(diode drop) = +7V

  • When mains power is ON
    • Gate is +5V
    • Source is +7V
    • Vgs = Vg - VS = 5 – 7V = -2V
    • -2V (Vgs) < -1V (vgs threshold), so mosfet is ON (battery is draining when mains is ON and circuit doesn't work).

Now, to account for the battery drain over time, it sounds like I should be using a mosfet with a low Vgs(th), as when the battery loses voltage, the source gets closer to the VGS(th), correct?

The NDP6020Pseems like a pretty common through hole one, with a vgs(th) max of 1V. Plenty of amperage too (24A);

The one in your diagram though (DMP1045U) seems a lot more appropriate though, as max amperage is 6A. My application shouldn't exceed 2 amps I'm guessing

Thanks again

[updated answer]

based on this stackexchange answer; arduino - Advice for this P-Channel MOSFET Power Source Selector - Electrical Engineering Stack Exchange

Example 1: Battery voltage NOT significantly higher than mains

Mains voltage = +5V
Battery voltage = +5.4V (1.8V lithium * 3)

When 5V mains is connected:

  • VG = 5V from mains

  • vs = vg - vf1 (diode drop of SB220 is 500mV) = 5V - 0.5V = 4.5V.

  • vgs = vg - vs = 5v - 4.5v = 0.5v

  • VGS(th) of mosfet in your diagram is -1V (maxium, i.e. fully on), -0.3V (minimum i.e. just starting to turn on)

  • VGS > VGS(th) = 0.5V > -0.3

result; VGS is NOT lower than threshold voltage VGS(th) minimum, so mosfet is OFF

When 5V mains is disconnected:

  • VG = 0V, as it's pulled to ground

  • vs = vd - vf (where vd is voltage of battery and vf is voltage drop of mosfet (Vsd is on datasheet) )

  • vsd on datasheet is -0.6V

thus,

  • vs = vd - vf = 5.4V - 0.6V = 4.8V

  • resulting in Vgs = Vg - Vs = 0 - 4.8 = -4.8

  • Vgs(th) > Vgs = -1 > -4.8

result; vgs(th) threshold (Max) is higher than vgs, so Mosfet is turned ON. And it is fully turned on because it's well below the maximum Vgs(th) of -1

[Further updates]

After mosfet is turned on and battery takes over;

Once mosfet is turned on, voltage drop from body diode goes away and now the voltage drop from internal resistance takes over. Voltage drop is now;

Vs = Vd - Rds(on) * current

my application will probably be no more than 1.5AMPS, so;

Vs = 5.4 - 31mΩ * 1.5 = 5.35V min

  • vgs = vg - vs = 0v - 5.35v = -5.35v

What happens if mains power returns (according to ShermanP's reply);

If the battery bas been powering the MOSFET and the mains of 5V returns back to normal, then the source voltage is still the battery voltage of 5.35 to 5.4 V, since the mosfet is initially on. The mains power is 5V and battery is 5.4V minimum, so gate-source voltage (vgs) is -0.4, which is actually lower than the minimum -0.3V vgs(th) . Therefore there's a chance the battery is still powering the Mosfet a little bit.

  • vgs = vg - vs = 5v - 5.4v = -0.4v
  • **vgs < vgs(th) = -0.4 < -0.3, which means slightly on.

However, batery voltage will probably actually be more like 1.7, so battery voltage adjusted to 1.7*3 = 5.1, so;

  • vgs = vg - vs = 5v - 5.1v = -0.1v
  • vgs > vgs(th) = -0.1 > -0.3, which means MOSFET is now OFF. And we have to also account for the fact that the minimum vgs(th) is probably greater than -0.3V

You have some of that backwards.

The gate has to be at a lower voltage than the source to turn on the P-channel mosfet. The threshold voltage is how much lower that has to be, and we are interested in the minimum threshold voltage from the datasheet, not the maximum. So that's -0.3V.

So let's take the easiest case first. If there is no mains power, then the gate voltage is zero. If the mosfet is off, then the source voltage can be no lower than the battery voltage less the drop across the body diode, so the mosfet will turn on, which bypasses the body diode, and the source voltage now equals the battery voltage.

But now suppose you have been running on battery power, and now the mains 5V is restored. Since the mosfet is initially on, the source is at battery voltage. But the mains 5V is lower than the 5.4V battery voltage, by more than 0.3V, so in theory the mosfet could actually stay on a little bit. That's not going to happen because:

  1. The actual threshold voltage is very likely to be greater than -0.3V.
  2. The no-load voltage of the lithium battery more like 1.7+V. See the discharge curves here:

https://data.energizer.com/pdfs/l91.pdf

So I think three primary lithium batteries would work ok. But you would have to make sure your 5V mains supply is really 5V under load. Well, basically, you just have to test your circuit. If you were dealing with an 18650 (4.2V maximum), there would be no question.

Unfortunately, the NDP6020P has been discontinued. I don't know of another through-hole part with equivalent performance. But you may be able to find some on Ebay.

Hold on a second,

Yes my battery is at 5.4V, but that's when it enters the drain. I thought it will actually become lower when it reaches the source, by the voltage drop on the internal body diode vf (vf = vsd on the datasheet, which is 0.6V)?

So, the mosfet drops it by vf (0.6V). Which means it's actually 5.4V - 0.6V = 4.8V at the Source, right?

So, If VG is restored to 5V+ from the mains, and the mosfet is still being powered by battery;

VG = 5V+
VS = 4.8V+

vgs = vg - vs = 5V - 4.8V = 0.2V

vgs > vgs(th) minimum = 0.2 > -0.3. Therefore mosfet is actually off.

(edit: I'm pretty sure I'm wrong about this voltage drop, as I've seen people use p-fets for reverse voltage protection and I swear the voltage didn't drop that far down)

update: yes, I got confused. I read the stack exchange post I linked earlier a bit more carefully, and it seems that 0.6v voltage drop only happens the instant the mains power supply goes off, but then once the FET is fully turned on, the source voltage is now; Vs = Vd - Rds(on) * current, which is a very negligible drop.

Thus VS is roughly equal to the battery voltage of 5.4

I've updated my calculations in my previous post now

Seems like they have heaps on Aliexpress, but maybe they're junk. SMD shouldn't be a huge problem anyway.

thanks!

Yes, the whole point of using the mosfet is to reduce the voltage drop to near zero when it's fully turned on. Otherwise you would just use the two-diode model.