I guess that what I am going to ask must have already been asked before, but I promise I have tried hard a lot in order to find it but without success.
I am developing a project with Arduino where it is necessary saving some data when Arduino turns off. I am using the EEPROM to get it.
The problem I have is that I am not really able to DETECT when Arduino is turning off. I use a capacitor which let Arduino keep on for 1 second after power turns off. 1 second is certainly enough since I am saving 10 bytes. I do know the prgramation I have written is ok because I have checked it with a button FALLING like power would do.
I need some circuit so that a digital pin can detect 1 when powering and 0 when turning off. I have tried wiring the input directly to the power but although power turns off, the pin doesn't detect it (I have also tried with a pullup resistor).
Of course I am using an external 5V supply power.
Thank you very much for that useful answer. I don't know why I had completely forgot the fantastic operational component. I am triying to remember them. I have some UA741CN, it would be fine, wouldn't it?
Actually I should review the Arduino UNO datasheet since it detects USB/External power by one of these ICs, isn't it?
albuino:
I guess that what I am going to ask must have already been asked before, but I promise I have tried hard a lot in order to find it but without success.
I am developing a project with Arduino where it is necessary saving some data when Arduino turns off. I am using the EEPROM to get it.
The problem I have is that I am not really able to DETECT when Arduino is turning off. I use a capacitor which let Arduino keep on for 1 second after power turns off. 1 second is certainly enough since I am saving 10 bytes.
If the capacitor is keeping the +5V line high then you could be measuring the capacitor, not the supply.
If that's the case you need something like a diode in the supply line and measure the voltage on the other side of the diode.
You can measure voltages using the Arduino analog pins.
No the 741 is of no use here. Try the LM339. Or for the ultimate economy there is a built in comparator in the A/D section of the 328 processor.
The problem with using a diode is you drop the supply voltage. The capacitor is the supply voltage, that is what you want to detect the droop on.
fungus:
If the capacitor is keeping the +5V line high then you could be measuring the capacitor, not the supply.
If that's the case you need something like a diode in the supply line and measure the voltage on the other side of the diode.
Actually I was using a diode, I forgot to tell you. It was the initial idea. You can see it on the schematic attached "Initial idea". But it didn't work.
Grumpy_Mike:
No the 741 is of no use here. Try the LM339.
I definitely want to go on with the Grumpy's idea. It sounds more profesional, and that is exactly what I need. However any idea will be welcome.
I prefer to use an extra IC like LM339, I think I also have it (I do know I have the LM393, would it be fine?). I have also drawn a schematic with this component. Please Grumpy, could you review whether it was well made?
Here's a simpler way (see attached schematic). By putting the storage capacitor on the input side of the regulator, you can get a much earlier warning of impending power loss and much more time to react (i.e. the capacitor can lose about 80% of its charge before the regulator output drops to 4V, rather than 20% of its charge). The pin marked Ain can be a regular Arduino analog input, if you can arrange for your code to read the voltage on that pin sufficiently often to give your self enough time to save the data to EEPROM. Alternatively, you can use one of the analog comparator pins, connect the other analog comparator pin to a voltage divider across the 5V supply, and enable the analog comparator interrupt. No need for an external comparator unless you are short of analog input pins.
The problem with DC42's method is you have to constantly read the analogue in, but his use of a potential divider on the positave supply side of the regulator is what you want to do. Feed that into the comparator. Remember you will also need some capacitors on both sides of the regulator.
You had not mentioned a regulator before.
Grumpy_Mike:
The problem with DC42's method is you have to constantly read the analogue in...
I'm primarily a software engineer, so I don't consider it a problem having to read the analog input every 100ms or so. And I guess you didn't read my alternative suggestion of using the analog comparator in the mcu.
[I got the 100ms interval from assuming a 50mA load on +5V and a 1000uF capacitor, which will provide 5V power for around 350ms after the 24V supply is removed.]
There are very good methods by profesionals so that I have realized that I must be much more exact with I have and I want to.
I have either digital and analog pins available to get this porpuse working.
Having to read each 100ms an analog pin is not problem at all.
It is true, I am using a regulator. It is module based on LM2596 I can power it from 5 to 50VDC and get from 3 to 45VDC. I always use one of these in my Arduino projects.
The most important thing is that I do use the 5VDC to power other componentes like LCD16x2. So I am afraid this will change the DC42's schematic. I think he understood me to use the 24VDC to power the rest of the elements. Sorry for that.
albuino:
The most important thing is that I do use the 5VDC to power other componentes like LCD16x2. So I am afraid this will change the DC42's schematic.
The LCD itself takes only a few mA, but the backlight may need around 100mA. If you know what the supply voltage is, then you can power the backlight from that supply (before the diode) through a resistor. That way, the capacitor does not power the backlight when the power goes off, so the backlight does not drain the capacitor.
albuino:
I think he understood me to use the 24VDC to power the rest of the elements. Sorry for that.
Yes, I misunderstood you. If the input voltage is lower than 24V then you need to use a higher value capacitor, or accept a shorter time to detect loss of power and save to EEPROM.
btw you can feed the voltage divider that drives the analog input from the supply before the diode instead of after if you prefer. That gives you a more immediate detection of loss of power, but will also detect brownouts that might be short enough not to matter.
Thanks you very much for helping. Finally I got my project working by reading voltage. But in the near future I will try with comparator and the other very nice ideas on this post.
dc42:
[I got the 100ms interval from assuming a 50mA load on +5V and a 1000uF capacitor, which will provide 5V power for around 350ms after the 24V supply is removed.]
By the way DC42, could you explain us how you calculated the numbers above. Calculating capacitance (1000uF) and time (350ms) by voltage (5VDC) and current (50mA) is very useful. In fact this has been a very good example because the mcu needs about 50mA/5V so that we need a 1000uF capacitor to keep it on for around 350ms.
albuino:
By the way DC42, could you explain us how you calculated the numbers above. Calculating capacitance (1000uF) and time (350ms) by voltage (5VDC) and current (50mA) is very useful. In fact this has been a very good example because the mcu needs about 50mA/5V so that we need a 1000uF capacitor to keep it on for around 350ms.
The basic equation for a capacitor is Q = CV, where Q is charge, C is capacitance and V is voltage change. If the current draw is constant, then Q = It where I is current and t is time. So It = CV, or to put it another way, t = C*V/I. That is, if we draw current I from a charged capacitor with value C, then it will take t seconds for its voltage to drop by V. I was assuming a 24V initial charge on the capacitor, which can drop to about 7V before the 5V regulator drops out; so V = 24 - 7 = 18. Hence for 1000uF and 50mA, t = 1000e-6 * 18/50e-3.
dc42:
btw you can feed the voltage divider that drives the analog input from the supply before the diode instead of after if you prefer. That gives you a more immediate detection of loss of power, but will also detect brownouts that might be short enough not to matter.
I prefer to feed the voltage divider that drives the analog input from the supple after the diode because the 24VDC are not very good.
dc42:
The LCD itself takes only a few mA, but the backlight may need around 100mA. If you know what the supply voltage is, then you can power the backlight from that supply (before the diode) through a resistor. That way, the capacitor does not power the backlight when the power goes off, so the backlight does not drain the capacitor.
I prefer to feed the LCD and its backlight from the 5VDC as I have said the 24VDC are not very good. I have been thinking about some ideas for this and I think I have got it but I am not sure. Perhaps putting a transistor to disconnect everything which needs 5VDC except the Arduino power, would be fine. I have attached a schematic with this idea in order to be checked. In addition it will help to understand your idea easier by other people since it unifies several paragraphs written.
Ok, I admit I don't really know how to integrate a transistor here. I only can put a relay instead. But let's try to calculate the transistor which fits here.
For example we can use a BC547 (Vcbo = 50, Vceo = 45), and assume an Ic = 200mA. I guess Ie = Ic = 200mA. I suposse I need hfe value but in the datasheet there are 3 values:
Classification A B C
hFE 110 ~ 220 200 ~ 450 420 ~ 800
I can't go on with calculations, sorry. Any help?
Grumpy_Mike:
Then that is fine. Some transistors can not stand the base being that much higher than the collector.
If the base goes more than about 0.6V above the collector, then the collector-base junction will conduct, feeding some current to the MCU +5V supply (limited by the 4K7 base resistor), but otherwise doing no harm.
I guess that what I am going to ask must have already been asked before, but I promise I have tried hard a lot in order to find it but without success.
I am developing a project with Arduino where it is necessary saving some data when Arduino turns off. I am using the EEPROM to get it.
The problem I have is that I am not really able to DETECT when Arduino is turning off. I use a capacitor which let Arduino keep on for 1 second after power turns off. 1 second is certainly enough since I am saving 10 bytes. I do know the prgramation I have written is ok because I have checked it with a button FALLING like power would do.
I need some circuit so that a digital pin can detect 1 when powering and 0 when turning off. I have tried wiring the input directly to the power but although power turns off, the pin doesn't detect it (I have also tried with a pullup resistor).
Of course I am using an external 5V supply power.
Waiting ideas. Thanks a lot.
I did exactly what you want to do. What I did was this:
(1) Connect a 4700 uF cap across 5V and gnd to maintain power for a few seconds.
(2) Used a resistor divider connected to an analog input pin. This serves two purposes: First the program itself monitors the battery voltage and reports when the battery is low. Secondly, the voltage value is checked once for each pass through the main loop and compared to the previous reading. If it drops fast enough (i.e. the difference between two readings is large enough), that is considered a "powering down" condition and then the program saves it's current state to EEPROM, to be re-loaded and resumed at next powerup.
The key is the capacitor to keep the board powered long enough to recognize a shutdown event and then have enough time to write to EEPROM.