I want to use a sensor and relay as a safety device to allow/deny power to an AC mains-powered electrical unit. If the wall power is disconnected I loose the state of the relay which is stored in a variable on my Pro Mini. My Arduino is powered from an AC-DC voltage regulator that is between the AC mains and the Power On switch of the electrical unit. Apart from powering the Arduino with a battery/supercap, are there any circuit design tricks I'm missing?

  • Can you clarify your setup? Is the Arduino controlling the relay and also sensing the presence or absence of mains power (i.e., detecting a power loss?) Aug 28 '18 at 16:12
  • Yes the Arduino is controlling the relay. No, the sensor is a liquid-level sensor which indicates whether the Arduino should turn off the power on low liquid level.
    – Owen
    Aug 28 '18 at 16:43

When you change the state of the relay, save it to the EEPROM. In setup() read the value from EEPROM and apply it.

  • Ah... why didn't I think of that :-)
    – Owen
    Aug 28 '18 at 16:42
  • 1
    but is it necessary? Doesn't the state evaluate from the sensor value after the sketch starts?
    – Juraj
    Aug 28 '18 at 17:12
  • 1
    If the state of the relay is fully determined by the instantaneous state of the sensor, then there is no need to store the relay state. Aug 28 '18 at 17:14
  • Yes, I think you are both right. What was confusing me was that I do need to store the relay state in a variable so that I only switch the relay when the sensor changes, and not on every iteration of the loop.
    – Owen
    Aug 28 '18 at 18:36
  • 1
    If you set a pin to HIGH and it's already HIGH, nothing happens. It's okay to "set" the relay on every iteration of the loop. Aug 28 '18 at 19:35

I have run into a similar situation on a few occasions. Maybe my scheme will help. What I do is sense power loss early, so I can save a structure full of all my program states to EEPROM when the power is going away. Conversely, the first thing the program does on power up is restore the memory structure from EEPROM. Provided you save your relay state whenever it changes, this scheme should help you recover. You don't need a super cap. I use a circuit like this...enter image description here

In this circuit, I use a diode to isolate a large (but not super) capacitor from the supply, and that circuit point also feeds my VIN (This is a NANO board). Then, that capacitor voltage is further isolated from a voltage divider, with another diode. The voltage divider brings down the 12V (only 11V after the diodes) to pretty near 5V. there's also a small (100nF) capacitor there to filter any noise.

If power is lost, that 2200uF capacitor will keep my NANO, a bunch of LEDs, an LCD display, and even a transistor driver and relay circuit(not shown) alive for nearly a second. But the voltage at the digital input (D2 in my case) will drop within milliseconds! So if you are polling that pin regularly, you'll have plenty of time to react by saving your states. If you doubt whether you can poll that fast, pin D2 can easily be tied to an interrupt. (that's overkill.. easier to just create you're owe version of millis(), so that whenever you have a timer wait, you can continually test the power detect pin, and react within a millisecond).

Now if for some reason you would like to ensure that the arduino doesn't lose power at all during short power line disturbances, you can add another diode to the 2200uF capacitor, and connect it to a battery pack. It the battery voltage is a little lower than the main supply, no current will flow out of it until its really needed.

  • Nice. I like your implementation - thank you for sharing.
    – Owen
    Aug 29 '18 at 20:13

You power your device and your controller from same AC source. You loose that AC source and they both will go off line. You get your AC source back - restart controller and check the current state of your device /relay.

What did I missed ?

  • You are correct and missed nothing, only the bit where I said I got confused :-)
    – Owen
    Aug 29 '18 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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