13
\$\begingroup\$

If I have an Arduino, can I abruptly turn the power on and off without corrupting it? If I am designing a product that has an on/off switch, do I need to incorporate a delay before turning off the power so that the microcontroller can do a clean turn off of some sort? Or is it okay to just connect an SPST switch to the Arduino's power line?

(By designing a product, I mean using an ATmega with Arduino code independently on a custom board with other components.)

Edit: By Arduino I just mean flashing the Arduino bootloader so I can program in Arduino instead of AVR. The chip I want to use is Atmel atmega328P I do not need any EEPROM usage.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ Note that "Arduino" is a brand name so it gets a capital 'A'. Capitals matter. \$\endgroup\$
    – Transistor
    Mar 10, 2019 at 8:35
  • 3
    \$\begingroup\$ Perhaps if it is actually an "Arduino", but the question is actually about an ATmega on a custom board which would be more of an "arduino". As for the actual topic, in addition to the ATmega one should also consider any other components which may have state, especially external memories - for example, if there's an SD card that needs a large amount of thought and concern. Finally, some power supplies do nasty things at turn on/off. \$\endgroup\$ Mar 10, 2019 at 17:14
  • 1
    \$\begingroup\$ Can we assume you're not using an SD card either? \$\endgroup\$
    – UKMonkey
    Mar 11, 2019 at 12:57
  • \$\begingroup\$ It is your product. Do you need to save something if the power turns off? The answer to that question answers your question. \$\endgroup\$ Mar 13, 2019 at 17:26

3 Answers 3

18
\$\begingroup\$

Yes, it is safe to abruptly shut off an Arduino.

Well, mostly safe.

The processors used in the various Arduinos have three types of memory:

  1. Flash - Where your program is stored. Your program can read stuff from here, but cannot write it.

  2. RAM - Where your program variables are kept while the program in running. The data here disappear when you turn the Arduino off. Your program reads and writes here constantly.

  3. EEPROM - Where your program can store stuff it will need the next time it runs. Usually stuff that changes rarely, but is needed anytime the program runs. Like calibration data for a sensor.

Most programs only use Flash and RAM. You can switch the Arduino on and off any time you like with those programs.

If your program writes to EEPROM, then shutting off the power while writing to the EEPROM could corrupt the data there.

How that affects your program depends on what the data is.

If it corrupts the calibration data for a sensor, you would get bad measurements for whatever the sensor is detecting.

If you write checksums with your EEPROM data, then you could detect the corruption and your program could shut down instead of using bad data.

You, of course, would know if your program writes to the EEPROM - you have to load a seperate library and use special commands to read and write to the EEPROM area.

The danger is really only in that short moment when you write to the EEPROM. Since that happens rarely (and usually only under controlled conditions) it will also be rare to corrupt the EEPROM data.


Summary:

You can turn an Arduino on and off at will with no danger, unless you are using the EEPROM - and even then you will get away with it most of the time.

\$\endgroup\$
2
  • 8
    \$\begingroup\$ "Flash - Where your program is stored. Your program can read stuff from here, but cannot write it." - Many AVR chips (the 328P definitely included) can self-program the flash. In fact, this is how the Arduino programming cycle works; the bootloader, running on the 328P, programs the 328P itself. It's pretty unlikely the OP will be doing that, but it is possible. \$\endgroup\$
    – marcelm
    Mar 10, 2019 at 14:02
  • 2
    \$\begingroup\$ Also, if you use an Arduino with extra hardware on it, (like a sensor), you might as well add a capacitor and spend a portpin to measure the external power, so that when if disappears, you can safely save to eeprom whatever you want. On an ATmega an EEPROM write cycle is typically 3.4 ms. So if your capacitor can keep the processor alive for 5 ms, and before each eeprom writing operation you check the external power, you are safe. \$\endgroup\$
    – vsz
    Mar 11, 2019 at 5:07
10
\$\begingroup\$

Yes, you can turn the power off quickly without corrupting it.

The only reason I can see for putting a delay into the power down of the circuit would be for safety or functional reasons of your external peripherals.

E.g. on power off, need to save data to non-volatile memory. Or on power off, need to ensure a mechanism is in a safe position at power off.

This would require monitoring of the supply and having sufficient hold-up capacity to run the processor and what ever function that was required to be performed.

\$\endgroup\$
6
\$\begingroup\$

The term Arduino refers to a very broad spectrum of microcontroller boards that have various different chips, and the effect of power loss thus varies. So, I would recommend you put the exact part number of the ATmega chip being used. But, in a general way for chips like ATmega328, the following holds good.

Yes, you can remove power from an Arduino without corrupting or damaging anything but do keep in mind the following things:

  • If your code utilizes EEPROM then the flash might get corrupted or the data may not get stored accurately when the device is flashing data and power is removed.

  • The only-way a sudden power loss can do harm apart from EEPROM is to the devices being used with Arduino (e.g. an SD card).

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.