Detect power loss during Arduino operation

Hi everyone,

Fairly new to the Arduino thing. I am using an Arduino Mega 2560 and 12 Teensy 3.2 modules to control 12 stepper motors.

During the sketch the Mega is controlling the 12 steppers via signals sent to each. I am using individual Teensy modules to drive Leadshine DM320T drivers. The Teensy modules also check for limit switches on each motor and other input which is why I am using individual modules for each motor. I would not have enough pins using just the Mega.(trust me on this)

The motors are all zeroed at startup. During the sketch, there is little chance of missed steps, but that is always a concern. The motors all return to starting position when each routine is completed. Depending on input (switch selection), each routine will vary as will motor travel. At the end, they always return to the start position.

I need to check for power interruption which would leave the motors in an "unknown" position. I need to ensure that they always return to start position.

How can I add a detection circuit (?) to tell if power was lost and the routines interrupted. If this is detected, I would stop all opeartion until a button switch is pushed and the motors are zeroed and sent back to the start position.

What is a simple way to tell if power was lost? I do not want to use battery backup. I just need to know if power was interrupted.

I am using 2 power, one for the stepper drivers (18V 30A), one for the arduino modules (5V 10A) supplies and 14 voltage regulators. One input line (AC 110V) supplies both power supplies and they in turn supply the regulators.

My concern is for missed steps if power is interrupted.

Is there a simple circuit/module available for this?

thanks in advance. I read several threads on this on here but nothing seems to fit what I need.

Don't you need to zero them at the first startup anyway? Why not just zero them on every startup?

If you're running setup, there was power loss, whether deliberate or accidental.

At initial power on, they are zeroed and then move to start position.

Once they are at start position I know their position. some routines run one after the other. Zero is determined by limit switches at the maximum travel for the motors. Start position is in the center.

I want to detect power loss during operation. Power loss might happen in the middle of a routine. If that is the case, I want to stop all operation, sound an alarm, and wait for a push button to be pressed after the machine is checked for possible problems (jams) caused by the power loss.

You could write something to EEPROM that tells you that an operation is in progress and clear it when you're done. At startup, if the EEPROM flag is set, you know (or strongly suspect at least) that there was a problem with power.

Do you really want to monitor the outputs of all the 14+ voltage regulators, to find out about possible power loss?

If you only want to track power-on of all boards, you can add a "power good" output signal to each Teensy, which is OFF in power off/loss state, and goes ON only after the reference point has been reached. Only if all lines are ON, the master controller will start working, and will stop everything as soon as any board looses power. These lines can be combined into one signal (wired AND), but then you'll lose indication of which Teensy failed.

If you want to be safe using hardware, monitor the AC. If you miss a cycle or more, the DC storage caps in the power supplies will start to drop. That gives you some time. The loss of AC is really the first event in the sequence so it gives you the earliest warning.

If power loss is a serious problem why not implement a serious solution - an uninterruptible power supply?

You can do that easily by powering the Arduinos from a 12v lead-acid battery that is always being trickle charged from the mains. If you put a diode in the trickle charge circuit (to prevent back-feed from the battery) you can monitor the trickle-feed voltage which will let you know about mains failure and allow the Arduino(s) to take appropriate action.

...R

wildbill:
If you’re running setup, there was power loss, whether deliberate or accidental.

I am not sure what you mean. Are you saying setup causes power loss? confused

After considering the options. I plan to use a UPS for main (AC) power. Thank you all for your input.

Not only a mains shut off can cause serious problems, also interfering signals (EMF) or a defective voltage regulator or driver chip can cause trouble.

So we have to consider at least 3 overall states:

  • master reset, immediately stop operation
  • initialize all reference positions
  • normal operation

Another "emergency stop" signal should go to all modules, which is ON after a reset or failure, and OFF when the master determines that everything is okay.

All these signal lines should have their "alert" state when the module looses power, and are reset only if the module finds everything okay.

artistinfla:
I am not sure what you mean. Are you saying setup causes power loss? confused

No. The opposite. The Arduino only runs setup() when it starts. So if it is in the setup() function it means that the Arduino has just started.

...R

DrDiettrich:
Not only a mains shut off can cause serious problems, also interfering signals (EMF) or a defective voltage regulator or driver chip can cause trouble.

I think I will monitor all modules for individual power loss (possibly due to a defective regulator)
Possibly from a separate module, since I want to ensure the main module (Mega) is also working properly.

good idea since even with power to the main power supply, any regulator could go bad

Thanks

Mark

artistinfla:
I think I will monitor all modules for individual power loss (possibly due to a defective regulator)

That is a good argument for minimizing the number of modules - fewer things to go wrong.

...R

PS ... I can't understand the need to use Teensy modules for each motor. An Atmega 328 should have enough capability.

Is there no way to have the motors trigger a sensor (microswitch, optical sensor - whatever works) when they are in the START position, rather than some other position you call home?

Steppers may lose steps occasionally (shouldn't happen but slip happens) and that can also put you off a bit. By having your limit switch linked to the start position you get to see that they are where you think they are every time you move them back to (or through) start.

This also means you don't have to worry about power loss and the motors not being in start position upon startup. Who says someone didn't move them while the power was out, even though it shut down properly? You see upon startup which motor is at their expected start position and all the others you just move until they're there.

Main DC supply -> diode -> (tee) and (decent electrolytic cap) > VIN pin.
Measure the (tee) voltage with an analog pin.
When it drops, you can start your crash routine.

  • effectively a mini-UPS on your VIN pin.

The electrolytic cap will keep the cpu running long enough to save eeprom etc, and send stop commands.
How you ensure there is power to hold the stop position is up to you...

When supply is restored, check what happened, and as much as possible pick up from where it all went wrong.