Powering Off: Does the arduino know when you turn it off?

Hey Everyone,

Is there a way software-wise for the arduino to know the moment you are unplugging it? My arduino project sends out serial data based on voltage readings from potentiometers. If I turn off the arduino that runs my unit, somewhat random data gets sent out the serial out for a moment. I assume this is because the voltage coming out of the 5V is droping very quickly to 0, and therefore triggering a serial write like my code tells it to do. I would like a way in my software to block the potentiometer reading section the moment the arduino knows it’s about to be powered off. Any ideas?

Thanks!! :slight_smile:

There is a brownout feature that will trigger a RESET if the voltage drops below a certain level. Upon power-up a flag will be available indicating that a brown-out took place, and you can take some action. See the ATMega data sheet.

You can try connecting one of the analog inputs to the 5V supply and monitor/check it before reading the pots.

It all depends on how fast the voltage decays when power is lost (you’d like that to be relatively slow) and how fast you can read the pots and send-out the data after confirming the voltage is good (you’ll like that to be fast).

If you are running the Arduino off a higher voltage through the regulator, you can use a voltage divider (2 resistors) to bring that voltage down to 5V (or less) and monitor that voltage. That would give you more time because that voltage can drop before the 5V regulator drops-out, giving you more time to react.

Of course the problem is, when the voltage drops too far the processor will stop or get flaky and your serial transmission could get flaky. You need to know there’s a problem before that happens.

If you have any control on the receiving end, there may be things you can do to confirm the data is good. RS-232 has optional parity checking or you could do something more advanced. Ethernet has error checking, and I don’t know how the error checking/correction works but you rarely-rarely-rarely get corrupted data over the Internet even with data flowing all over the world through all kinds of unknown/flaky connections.

Maybe you can send the data twice and make sure it’s the same twice? Not efficient, but it might work…

You can also set up a circuit such that Arduino has control over it's own power supply. Then you give Arduino some sort of input and it cuts it's own power. Under those circumstances it would certainly know the condition.

Yeah, basically the equivalent of an AC UPS in DC...

Battery, caps - whatever storage device that can provide enough power for long enough:

  • Input to UPS goes kaput
  • there is enough charge in the UPS to deal with your serial comms issues
  • shut down the arduino yourself or...
  • charge runs out eventually and 'shuts it down' for you (but serial comms have stopped)

You may find something here of interest: https://code.google.com/p/tinkerit/wiki/SecretVoltmeter

Thanks everyone for your information! This was very helpful. I will look into the flag for the brownout, that might be the best solution, but I will have to test it.

If that isn't good then I will just do the analog input to the power reading and mute the pot code if it drops below 5ish.

Thanks again!