this might sound like a stupid question but how long can a arduino run without power?
for school we have a project where we have to make a crane with the arduino.
we need to run the program and if there is a power outage we have to stop the crane and resume the project afterwards when power comes back on (unplugging an replugging the power source). recalibrating isn't an option, so we were thinking about using a battery backup!
if the main power fails we want a relay to switch so the battery can power the arduino.
but the question than arose how fast does it have to switch (miliseconds) so the arduino can continue without problems?
You can monitor the power with analogRead(),
use a voltage divider to bring power to about 2.5V and check it constantly and print the value to serial (use a high baud rate of e.g. Serial.begin(230400);
You might see the voltage drop to a certain level.
The maximum switching time depends indeed on how long the Arduino can operate on its capacitor charge after losing power.
Most Arduinos have small onboard capacitors to regulate power that can supply power momentarily. However, this duration is very brief, I'd say (never tried to measure) within 1 ms or less depending on the specific board and any external load that you power from the board.
In doubt you could add a capacitor on the Arduino’s Vin line to extend the time it can operate without power. Size the capacitor to help bridge a few additional milliseconds, giving the relay a bit more time to switch (if you go for the relay solution)
this gives me a minimum amount of volt I need to supply.
But not the maximum time (preferably in ms) to switch from one to the other supply without restarting the arduino, which i am interested in.
Measure the power consumed by your Arduino during normal operation.
If you want a "backup" power supply that lasts for, say, 10 ms, calculate the required capacitor size to effectively provide that backup.
Add a capacitor which is double the calculated size to be safe between the Arduino’s Vin and GND pins.
you can use the formula
C = (I x ∆t) / ∆V
where
C is the required capacitance in farads (F),
I is the current consumption of the Arduino in amperes (A),
Δt is the backup time needed (in seconds),
ΔV is the allowable voltage drop across the capacitor (in volts).
for ∆V, say if your Arduino needs a minimum of 5 V to operate, and your input power is 9 V, you have up to a 4 V drop before things go south and make sure the capacitor’s voltage rating is higher than your input voltage.
How are you currently powering the arduino, directly from 5V or a higher voltage going to the on-board regulator?
Relay is a slow way to switch power sources, and requires power for the relay itself (unless you use a latching relay). Using a diode to isolate a battery that is slightly below the normal operation voltage will allow the battery to take over whenever the power supply voltage drops, and if this is done before the on-board regulator the Arduino would never lose power.
If the desired result is for the crane to stop operating during the power loss, instead of continuing to run off battery, then the common solution is to have a capacitor large enough to maintain power while necessary data is saved to EEPROM, then when power is restored read the date back from EEPROM and continue from there. No need for a relay or battery, but the code needs to monitor the input voltage to detect the loss of power.
Note that if there are stepper motors involved, there is no way to detect if they have moved while the power is off. Servos can also move, but since those have positional feedback they can be returned to their previous positions easily.
You probably don't need a relay. If your battery voltage is lower than the regular supply voltage, you can connect both supplies through diodes, and the supply with the higher voltage will power the Arduino. The switchover is effectively instantaneous.
Or you can replace the battery line diode with a P-channel mosfet, which would eliminate the diode voltage drop.
Well I guess this needs the main power to be provided at Vin. If it's coming in through the USB connector, the two-diode version would work with a Nano, which already has a schottky diode between USB and 5V. So you would just add the battery and its diode at the 5V pin. Might be more complicated with other Arduinos.
The OP has not bothered to tell what all stuff is connected to the Arduino. That will have a massive effect on how much power is available to the actual Arduino processor. In addition, the time between power off and power on is variable and the number of instructions the Arduino can process in that time is also variable, especially if one of them is an analog read.