How to check if vin pin has voltage or not

Im currently on a project on a digital clock/stopwatch (basically a multi function clock) running on 12 volts at the moment. The 12 volt line is also supplying my arduino power through the vin pin.

However if a power outage happens, the clock time on the project will stop counting and when power comes back on the time will reset. Im planning to try to put a backup power source using a rechargeable battery in parallel with the arduino connected to the 5v pin.

My plan is that if I sense that the vin line has no more power, the arduino will still run though the 5v battery and turn into sleep mode maintaining the time. When power comes back on, the battery will get charged and the arduino wake up from sleep mode and continue running on 12 volts.

My problem is, how do i sense power from vin? is there a command that can easily do it? or do i have to put up a voltage divider on the 12 volt line and connect it to one of the analog pins for reading?

Additional method im considering is, after reading the schematic diagram of the arduino, If vin has power, power from the usb line is cut off and not used. Is it better if i connect the 5v battery to the usb power line or directly into the 5volt pin in the arduino

Voltage divider to bring the 12V down to 5V, connect that to an input pin.

The more sensible solution to your problem (and to have a far more accurate time than the Arduino will offer) is of course an RTC module.

wvmarle:
Voltage divider to bring the 12V down to 5V, connect that to an input pin.

The more sensible solution to your problem (and to have a far more accurate time than the Arduino will offer) is of course an RTC module.

welp, how in the world i did even forget about an rtc module, lol thanks for that. This just basically solves every problem i have and simplifies the circuit/code even more. Thanks again!

wvmarle:
Voltage divider to bring the 12V down to 5V, connect that to an input pin.

The more sensible solution to your problem (and to have a far more accurate time than the Arduino will offer) is of course an RTC module.

Another quick question though, if i use a rtc module? Does it have a function to interrupt every 1milisecond? To provide accurate ms resolution for my stopwatch? And if i use an rtc module? I wouldnt be able to edit the time through some buttons in my project right? (Which i can do right now). Id have to upload the new time and date through i2c using a computer?

Most RTC modules have a square wave output, not milliseconds but could be 4,096 Hz.

The millis() timer of the Arduino can function quite well to give you ms resolution on short time spans, like minutes to hours. Accuracy is as good as the crystal, so within 30 ppm or so.