Writing EEPROM, sudden battery loss, capacitor keep-alive calculation? - Page 1
Forum section: electronics / beginners

author: jnz
author_rank: Frequent Contributor
timestamp: October 11, 2018, 05:20:12 pm
content: This is a bit of a basic question, but I realized I've never had to learn this topic.I have a 12V to 5V LDO regulator. A micro that may be writing it's EEPROM when the power shuts off. The datasheet makes note to make sure you have enough powder to keep the micro alive until it's done writing a block. I figure this to be about 2ms or so.I could use a battery, but cost isn't ideal, plus I'd need some extra components to switch to/off that, plus it's a 5V system which isn't great for battery. My enclosure is extremely height limited, so adding a large electrolytic won't work. But there are other types of caps that might.I don't know the size of capacitor that would keep the system at 5V 50mA alive for 2ms. Is there a calculation for this?And I can't fit an electrolytic... so would a tantalum work? I can fit that as a chip style in 100uF but not sure if it would give the desired results.Is it more desirable to "buffer" the 12V side or the 5V side? I think the 5V because I wouldn't incure losses from the LDO, but if I put too much capacitance on the 5V side my chip may start up slower/goofier, right?

author: german77
author_rank: Regular Contributor
timestamp: October 11, 2018, 05:46:47 pm
content: You need to know the lowest operating voltage of your micro to calculate the capacitor size. And yes there are equations for this.For example lets say you put the capacitor on the 12v side. the micro will work until the voltage reaches 6.2v. Assuming that you draw 50mA all the time you use this equation.v(t)=-(1/C)It+v(0)Changing the values that we already know.6.2v=-(1/C)50mA 2ms + 12vwe got1/58000=C= 17ufOn the 5v side and the minimum is 4v you will get a capacitance of 100uf. and it's indeed a bigger capacity. If space is an issue you already know where to put that capacitor.

author: mariush
author_rank: Super Contributor
timestamp: October 11, 2018, 06:04:05 pm
content: Your problem is a bit more complicated.Capacitors don't behave like batteries,as you take energy out of the the voltage drops quickly, so you need to be aware also of the minimum voltage your microcontroller will need to actually operate at some frequency and the minimum voltage required for eeprom to be written to. For example, your micro may need 4v or more to run at 16 mhz, 3.3v or more to run at 12mhz or higher and so on ... If you have your micro configured at some high frequency and your voltage drops, your micro may become unstable or shut down even though voltage may still be high enoughWith a 100uF your voltage may be 5v initially but drop down to 2v at the end of 2 ms - I don't know, I didn't do the math .. would that be enough to keep the micro running and writing to eeprom? I don't know.keep in mind that, depending on how often you need to write to memory and how much power your micro uses, you'd may also need to monitor the input voltage somehow and detect if you have a power loss or something. If your capacitor is too big, your micro may idle for minutes consuming uA of current still operating as the voltage does down towards the minimum the micro runs at.either way, i'd avoid tantalum... also don't forget that some ldos are really picky about output capacitors, for example the 1117 series of LDOs want a capacitor with esr between 0.1 ohm and 1 ohm - some ceramic and tantalum capacitors can have esr below 0.1 ohm and then your regulator could oscillate in some conditions.There are polymer capacitors that have same size as electrolytic but have much bigger capacitance and there's also supercapacitors , for example 0.1F at 5.5v in 11.5mm :elna dx series 0.1f 5.5v

author: jnz
author_rank: Frequent Contributor
timestamp: October 11, 2018, 11:13:25 pm
content: My height requirement is ~4mm.The ARM micro claims it's operating range is 1.8V to 5V over all frequencies. It also has brownout detection at 2.25V that I'll certainly use. I looked for supercaps, but too expensive and too large. Even the smallest 5V I found was unlikely to work.German77, I'm a little confused at that calculation. Do you think you can break it down for in a couple more steps? Obviously 100uF is 0.0001000 and 2ms is .002, but I must be doing something wrong because I'm not getting 1/58000 anytime. I am definitely missing something because the "I already know where to put it" is definitely lost on me. I think you're saying before the LDO I need 17uF because I can tolerate a larger swing from 12V to 6V and on the 5V side if I only had 1V I'd need a much larger storage. The difference here being I could go with a smaller/cheaper 10V cap on the 5V side and on the 12V side I'd need something like a 20V+ part.... While I'm still trying to figure out the above... What happens if I have caps on both sides? I need to wait for the supply side to the LDO to drop below 6V which is when it'll cut out so then the 5V side would start it's drain - right? In this case I might be able to split the difference between size / type / cost of parts.

author: Momchilo
author_rank: Regular Contributor
timestamp: October 11, 2018, 11:34:49 pm
content: Quote from: jnz on October 11, 2018, 11:13:25 pm6.2V=-(1/C)*50mA*2ms+12V-5.8V=-(1/C)*100µAs-58000V/As=-(1/C)1/58000As/V=CC=17.24µFI hope that helps a little bit.

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 01:19:48 am
content: That does help! I mostly get it now, although couldn’t have figured that out in any reasonable amount of time! 1/58000 amp seconds per volt just becomes capacitance... I’m just going to say I totally get it!Ok.. so if I have a 5V chip and it works down to 1.8 that’s about 31uF I need (I think)... so still better to utilize the end with the larger swing if possible. Glad I posted this question!Advantage on the 12V end is that I’m looking at 5V solid for that amount of time vs a declining slide the micro can see.I love it!Still not sure if / why tantelum is bad, or if working with my smaller uF but higher volt numbers I can find a capacitor that meets the goals.

author: german77
author_rank: Regular Contributor
timestamp: October 12, 2018, 01:27:04 am
content: Quote from: jnz on October 11, 2018, 11:13:25 pmYes when the supply side reach 6v the 5v side would start to drain. Usually having two capacitors instead of one will increase cost and complexity. Also there isn't much benefit to split the capacitance as mostly any capacitor in the 5v side would be meaningless or very big/costly. I would rater go for a 22uF 16v ceramic capacitor at $0.14 on 100 units. An electrolytic will be around the same price but the ceramic is much smaller.Tantalum capacitors age and fail in short. Very bad failure for a Power supply. Higher voltages will give you smaller capacitance but you will waste a lot of energy in heat at 12v 50mA you are wasting 350mW on heating the regulator. if you use 24v for example you will need a 4uF capacitor but wasting 1W on heat.

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 05:48:17 am
content: Well, that’s a good start for tomorrow’s part selection but... as to capacitor voltage ratings - how hard a rule is that? I have a 12V nominal, but this could be plugged into a car that gets 14V nominal with nasty spikes of 18-60v for very short times. Going up on the voltage rating for a ceramic or electrolytic should increase the overall life but I’ll have to read about damaging them with over voltage.

author: mikerj
author_rank: Super Contributor
timestamp: October 12, 2018, 08:52:38 am
content: It should go without saying that you need to monitor the incoming supply to ensure you only start an EEPROM write whilst power is turned on, otherwise no amount of capacitance will help.

author: SL4P
author_rank: Super Contributor
timestamp: October 12, 2018, 02:31:02 pm
content: I have a dinky ATMEGA1284 project with some other stuff on the board... some fairly hungryI detect loss of the unregulated incoming supply, and have a primary 1000uF in front of the protection diode, and 470 after... before the regulators...The processor has enogh time to write at least 2 of EEPROM - before the board gets shut down.I work it this way because I keep a lot of transient variables which would wear down the EEPROM life,so only save it at shutdown or when power fails.

author: sokoloff
author_rank: Super Contributor
timestamp: October 12, 2018, 02:55:14 pm
content: One strategy I've used is to write some signature bytes to know on the next startup if I got a valid write previously.You could have an A and B bank and a start/end signature byte. If the start and end bytes mismatch, that bank is invalid. If the start and end bytes match, take the bank with the higher incrementing counter as the good one and write to the other one next time (write the new start byte, then the data, then the end byte). It's not cryptographically secure nor error-correcting, but it seems to work well enough.

author: mariush
author_rank: Super Contributor
timestamp: October 12, 2018, 03:04:08 pm
content: Quote from: SL4P on October 12, 2018, 02:31:02 pmAt some point, wouldn't it make more sense to add an i2c or spi based FRAM chip? You can buy them for around 1$ for 16 kbit and have 10^12 read/write operations per byte :https://www.fujitsu.com/uk/Images/MB85RS16N.pdf(random example)

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 05:33:27 pm
content: Quote from: mariush on October 12, 2018, 03:04:08 pmCost, size, none I could find quickly were 5V, so I'd need a 3V rail and not certain a voltage divider would be a good idea which just means more size and more cost. However - I will give another look into them and see if an external memory solution might work. It's not a bad idea at all.

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 05:35:41 pm
content: Quote from: sokoloff on October 12, 2018, 02:55:14 pmAlready doing. The issue is that I need the data that was attempted to be written. I'll be configuring the application to get the writing done while it's doing it's main tasks so as to keep the probability of the user disconnecting it to a minimum - but if there is corruption typically I'd lose important data or the module will stop working requiring an RMA, and really trying to avoid that!

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 05:37:21 pm
content: Quote from: SL4P on October 12, 2018, 02:31:02 pmWhy one before the diode and one after? What does that accomplish?Also, just clarifying, 2 of eeprom? Two bytes? Two blocks? 2ms?

author: jnz
author_rank: Frequent Contributor
timestamp: October 12, 2018, 09:22:06 pm
content: Wait... I'm dumb.Just because I know I have 2ms of run time... Doesn't mean I know when the 2ms started. If I'm buffering at the supply side, I don't know that they've already pulled the power and I'm in the middle or end of my 2ms before I start writing.This 2ms buffer really needs to accompany monitoring on the supply side - where I don't start writing eeprom unless I'm over some minimum voltage. So if my nominal is 12V, I don't start unless I'm over 11.5v or so. If I'm at 7 and start, that LDO will drop at 6V and this was all for nothing.So... This seems like an extra pain. Now I need some small voltage monitor or detector part/circuit just to see if I have 2ms by means of the capacitor to start with. First thought is a zener and voltage divider to my ADC, but I need a low power mode for the whole circuit to be under 1mA so I'm not sure the leakage and ADC speed will work out. I guess I know what I have to read next.Any advice?

author: german77
author_rank: Regular Contributor
timestamp: October 12, 2018, 11:37:10 pm
content: Well one solution is to use a transistor and make it to just turn on at 12v. And for example when the voltage reaches 11.5v it changes state and the micro will know when to save the data.Here is a schematic, you may need to tweak the voltage divisor to make it work with your transistors. It may not be the best solution as is highly dependent on the transistor, but it's cheap.

author: jnz
author_rank: Frequent Contributor
timestamp: October 14, 2018, 04:51:15 pm
content: So fairly interesting update... (to me anyhow)If I put a large cap on the supply side before the LDO, I need less capacity in uF but can’t really fit the desirable electrolytic becsause of size issues. Which leaves ceramic or tantelum or MLCC. Turns out there are a lot of MLCC parts that fit on paper but their rated uF is at their rated voltage, if you go over you burn the part, if you go under you get much less actual capacity. So a 47uF MLCC rated for worst case 50V might only get 20uF at 12V, and the mfgs make this data relatively hard to find. This is all in addition to the issue that just because the LDO hasn’t cut out, I don’t know I haven’t already been pulling power off my capacitor for the last 1.9mS - which is why I’d need a voltage detection circuit on the supply side from my last post.So....I discovered aluminum polymer electrolytics they have absurd uF ratings in the 200-400 range with less size, less cost - their downside is that they are very low voltage rated. About 6.3V seems common which is fine for me 5V LDO side. So yes, it’s less efficicient to buffer on the LDO side, but with 330uF I can get a lot more run time and I can use the micro’s brownout detection before writing to confirm the voltage hasn’t already started dropping.I still need to look into it, but I thought it was an interesting idea and relatively new because the whole series of alum polymer caps I’m looking at are brand new.

author: mariush
author_rank: Super Contributor
timestamp: October 14, 2018, 06:02:14 pm
content: No, in general capacitors will have the same capacitance no matter the voltage, you don't get less capacitance at lower voltage. Ceramic capacitors have an important exception... their capacitance can DECREASE as the voltage gets close to their maximum rating, seehttps://pdfserv.maximintegrated.com/en/an/TUT5527.pdf(Temperature and Voltage Variation of Ceramic Capacitors, or Why Your 4.7μF Capacitor Becomes a 0.33μF Capacitor)You could use a switching regulator instead of a LDO to increase the time... you'll find such regulators that can be up to 96-98% efficient, compared to LDOs which will always be as efficient the smallest the difference between input and output voltage is.With 12v in , 5v out, you get at best 41% efficiency, get something like an AP65111 from Diodes Inc. and you get up to around 95% efficiency:https://www.diodes.com/assets/Datasheets/AP65111A.pdfSee page 5, second chart from the left, you get ~ 90% efficiency at 0.1A output and up to ~95%. So at least twice as efficient as your LDO, reducing the need for big capacitance even further. I only suggest this particular regulator because it's cheap (<0.4$ ondigikeyif you buy 10 or more and it's relatively easy to solder, easy package to deal with, and can work with tiny smd inductor and resistors so very little footprint)You could add a tiny shottky diode in front and from there use a zener or resistor divider to detect when the voltage drops12v ----> tap for detect input voltage ---> diode ---> bulk store capacitor --> switching regulator -> your circuit.diode will prevent you from measuring the voltage on the bulk capacitor.. at very low currents like 0.1..0.5A you can find diodes that only drop only ~ 0.2v so you don't lose much. Use either a zener diode or a voltage divider to drop down input voltage to something reasonable like 3v or whatever. you can then use a digital input pin to know when input voltage is removed (though filter that a bit, in case you use for example a round barrel dc jack and power gets interrupted for something like 1 ms by twisting the jack in the socket or something like that)

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: October 14, 2018, 06:17:22 pm
content: E= V*I*t5*50e-3*2e-3= 0.5 mJ (after the LDO)12*50e-3*2e-3= 1.2 mJ (before the LDO)E= CV²/222e-6*12*12/2= 1.58 mJ

author: jnz
author_rank: Frequent Contributor
timestamp: October 14, 2018, 09:50:50 pm
content: Quote from: mariush on October 14, 2018, 06:02:14 pmHave you read on MLCC capacitors? Because your text is 100% counter to what I’ve read elsewhere. The issue being that X7R or X5V or whatever are just ratings that need to be true-ish for the stated voltage, but for less than the rated voltage can be wildly off, where a R rating may be closer to a V percentage at the voltage you actually hook it up to. What I read on MLCC is that a 50V rating may be 40-60% less capacity at lower voltages. IDK, maybe I’m wrong about that, but that was my take-away. Their advantage being you get more density and still have high frequency response of ceramic which I don’t need. Please correct me if I’m wrong!I’ll look at that part for a switching regulator, but I’m running up on space and height requirements for all components.For $.40 to $.60, if I use a polymer electrolytic I need one single component and the micro’s built in brownout detection should let me know when my internal reference is lowering (which I don’t really underatand how that works, but it’s a different post I’ll make). I’m highly inclined to go that route where I have 5v, 330uF, and as long as I’m above 4.5V I’m good to write knowing it’ll be X time before my 330uF let’s the voltage come down to my minimum 1.8V the chip stops working at. Or I’m all mixed up!!

author: jnz
author_rank: Frequent Contributor
timestamp: October 14, 2018, 09:52:17 pm
content: Quote from: GeorgeOfTheJungle on October 14, 2018, 06:17:22 pmI’ll also copy that down into my notes. I think it does make sense to consider in joules as well as in farads.

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: October 14, 2018, 11:30:01 pm
content: Quote from: jnz on October 14, 2018, 09:52:17 pmI find it easier to think about this in terms of energy.We know:Vo= Initial voltage= 5VandEo= Initial energy in the capacitor= CVo²/2= 330e-6*5*5/2= 4125 µJandThe µC consumes 500 µJ every 2 ms === 250 µJ every 1 ms. (*)To know the voltage of the capacitor at a given time as a function of its energy:E= CV²/2=>V= sqrt(2*E/C)andE= Eo-(250*t) (t in milliseconds)=>V(t)= sqrt((4125e-6-250e-6*t)*2/330e-6) (t in milliseconds)And then we get:https://docs.google.com/spreadsheets/d/1e7VZKLXoYMvlpKKmdPDZG3AF-rFIVKtU4AA9TOVvR8s/edit#gid=0(*) V(t) at constant power is a different curve than at constant current than at constant R. Constant power is best in this case i.m.o.http://google.com/search?q=capacitor+discharge+at+constant+power&tbm=ischhttp://google.com/search?q=capacitor+discharge+at+constant+current&tbm=ischhttp://google.com/search?q=capacitor+discharge+at+constant+r&tbm=isch

author: T3sl4co1l
author_rank: Super Contributor
timestamp: October 15, 2018, 02:55:03 am
content: Here's a JS calculator for three common scenarios:https://www.seventransistorlabs.com/Calc/PSHoldUp.htmlTim

author: max_torque
author_rank: Super Contributor
timestamp: October 15, 2018, 08:55:14 pm
content: Quote from: SL4P on October 12, 2018, 02:31:02 pmIf your device only needs a small amount of power, simply "hide" it behind a suitable self reseting polyfuse and make sure the power rail after that fuse is clamped with a suitably rated TVS diode. One other thing to note is that depending where you get your power for your device from, your supply could be in parallel with existing loads, and when the supply is interupted, those loads can "steal" power from your device (if you don't have a suitable input current direction limitier in place) meaning any capacitance on your incomming rail gets discharged into those other loads, rather than sitting their and being available for your micro.And conversely, if your circuit is being fed on it's own, when that feed is opened, your internal power rail capacitance may tend to keep the feed at highish voltage, meaning you may not detect a nice obvious drop in voltage to be able to say "oh look the powers gone off".........

author: jnz
author_rank: Frequent Contributor
timestamp: October 16, 2018, 05:57:11 pm
content: Quote from: max_torque on October 15, 2018, 08:55:14 pmYou're implying that the micro's brownout detection wouldn't be fast enough if I'm buffering on the 5V side of the power supply because it itself is being buffered? Hmm, I guess I haven't considered that, but in this case the micro says it'll work down to 1.8V... My plan is to ride 5V down to 2-2.5V with the cap, I sort of assume the micro will can let me know between 4-4.5V if I can start EEPROM or not. IDK.This is a lot more complex than I expected.

author: SeanB
author_rank: Super Contributor
timestamp: October 16, 2018, 06:44:01 pm
content: You probably want a power supervisor there, that will detect the falling power supply voltage, and generate an interrupt for the micro to tell it the power is on it's way out. As a bonus many also include, almost for free, a precision brownout detector, and as well a reset generator with a well defined trip level, and also often a watchdog system that can restart the microcontroller if it stops giving the watchdog reset for a while. Some also include a battery backup section that drives an external RAM and both provides power to it from an external cell, either primary or secondary, along with the right control signals to minimise RAM standby current.

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: October 16, 2018, 07:52:16 pm
content: It's better to put the capacitor before the LDO, why?1) Because if (as it seems) there's more than double the voltage before the LDO (input) than after it (at the output), the energy in the capacitor is going to be 4 times more: E is proportional to V squared (C*V2/2)) => twice the voltage, 4 times the energy in the (same µF) capacitor. While the power consumed is I*V not squared (4 times more reserve energy but only twice the power).2) It's easier to monitor that and you'll be able to detect a drop in V much sooner than after the LDO, and the sooner the better.1)+2) = much better before than after.Put 12V (2.4 times more) into V and leave 330µF in C and multiply the µJ/µs x2.4 in the spreadsheet and see.

author: jnz
author_rank: Frequent Contributor
timestamp: October 16, 2018, 11:01:24 pm
content: Quote from: GeorgeOfTheJungle on October 16, 2018, 07:52:16 pmBecause those nice cheap 330uF parts - have 6.3V maximums.