Drain Arduino Completely when no power input

Hi there,

I am trying to control a lamp so that when one presses the switch once the lamp turns on, on the second press the lamp doesn't turn on, I am trying to control high ac voltage with a solid state relay.

Everything is working fine the only problem I have is that, since I need to power arduino off of an old phone charger and so to remove power from the arduino completely and saving the solid state into eeprom when i switch off the system the arduino keeps current for a couple of seconds making it impossible to fastly switch between states. I hope that you understood my explanation, but if you didn't it is not a big deal, since my real question is:

How can you completely drain arduino so that it doesn't old any current when it is powered off? I am using a 5v 1A phone charger.

Thanks a lot

How can you completely drain arduino so that it doesn't old any current when it is powered off? I am using a 5v 1A phone charger.

Invalid question.

The arduino is not an energy storage device so any discussion about draining it is erroneous.

There is no energy storage device I am aware of that can be turned on and off as you describe, and any discussion of rapidly draining a battery is unwise.

"Draining" a cell phone charger rapidly by shorting it is also unwise due to heat generated.
Extending the shorting time by adding a resistor will just result in the same discharge time you already have.

If you wish to remove power from the arduino rapidly you will need to add a switch or relay.

Also, you might want to use a flip-flop for the 5V logic that drives your SSR.

No, I don't understand.

Obviously the Arduino has to remain powered-up while you write the EEPROM

And as long as power remains you can control the "states".

The power supply will continue to put-out DC for some undefined time and the voltage will drift-down after AC power is removed. That's the power supply, not the Arduino.... If you want to "instantly kill" the DC output, maybe you can switch the DC instead of the AC, or use a relay with a 120V coil (or maybe 240V if you are not in North America) to cut the DC as soon as AC is lost. (The capacitors on the Arduino will remain charged for some milliseconds so it won't actually be "instant".)

I am trying to control a lamp so that when one presses the switch once the lamp turns on, on the second press the lamp doesn't turn on.

I've read that several times, is that a typo or is that what you mean? On the second press the lamp is already on so I guess you could say that on the second press it doesn't turn on for the simple reason you can't turn on something that is already on. Somehow I don't think that's what you meant, but what you really mean eludes me. Please clarify.

I've read that several times, is that a typo or is that what you mean? On the second press the lamp is already on so I guess you could say that on the second press it doesn't turn on for the simple reason you can't turn on something that is already on. Somehow I don't think that's what you meant, but what you really mean eludes me. Please clarify.

My thoughts exactly. I just assumed he meant he wanted a flip-flop out put , press on, press off , press on,
press off , etc..

Hi there to clarify my experiment i will post my code, i want to have the switch working in that way because i want to control multiple lights from a single switch so that by switching on and off one or two times you can turn on only one light or more than one.

Here you can find a picture of my set-up
Google Photos

and a video where you can see that in order to obtain the aforementioned goal one has to wait until the fully discharge of the arduino, which takes a second or two, I am wondering if DVDdoug solution might work.

#include <EEPROM.h>
#include <Wire.h>
#include <SoftwareSerial.h>

const int ledPin1 =  11;
int check = EEPROM.read(0);

void control(int check) {
  switch (check) {
    case 1:
      digitalWrite(ledPin1, HIGH);
      break;
    case 2:
      digitalWrite(ledPin1, LOW);
      break;
  }
}

int quanto() {
  int check = (EEPROM.read(0));
  Serial.println("check1");
  Serial.println(check);
  if (check < 2) {
    ++check;
  }
  else {
    check = 1;
  }
  Serial.println("check2");
  Serial.println(check);
  return check;
}



void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  EEPROM.update(0, quanto());
  digitalWrite(ledPin1, HIGH);
}

void loop() {

  control(EEPROM.read(0));
  Serial.println("check3");
  Serial.println(EEPROM.read(0));
  delay(1000);
  Serial.println(EEPROM.read(0));
  delay(1000);
}

raschemmel:
Extending the shorting time by adding a resistor will just result in the same discharge time you already have.

I don't understand this statement. The time required to drain the residual energy is dependent on the power draw from the power supply. If you add a resistor, you increase the power draw and thus decrease the discharge time.

The time required to drain the residual energy is dependent on the power draw from the power supply

No , it is not. The power supply is already off .
Also, the power supply does not "draw" power, it sources it. The only thing that can "draw power" is a load, like the arduino.

It depends on the time to discharge the energy stored in thecapacitors of the power supply, which is why it makes much more sense to disconnect it with a relay so you don't have to discharge the power supply caps but a relay takes about 40ms anyway so there is no FAST way to remove all the energy unless you remove the resistor and dead short the power supply. Does that sound like something you should do ?

Resistors are and have been , used to short large electrolytic capacitors for over 50 years.
Using the same concept, shorting the power rails of the arduino with a resistor REDUCES
the shorting current, thereby INCREASING the DISCHARGE time.

Also, based on your post number and karma points , you should know more about it than I do...

I don't follow you. The resistor is in parallel, not in series, so it will increase the current.

I don't follow you. The resistor is in parallel, not in series, so it will increase the current.

The resistor goes from 5V to GND, so yes , that is in parallel with the arduino, but it is in series with the discharge current , which is flowing from the 5V rail, through the resistor, to GND.

V = I*R

t = R*C

Let V = 5V
R = 10 ohms
C = 5 uF

t = 10 * 5 uF
t = 5.0000e-05 S = 50 uS

Let R = 1 ohm
t = 5.0000e-06 = 5 uS

50 uS > 5uS

The energy is in the caps , which are across the 5v line.
Placing a resistor from 5V to GND creates a current loop from the 5V rail to GND.

You can test this by putting a 1 Mohm resistor across a 5000uF cap charged to 30Vdc, and then
replace it with a 1 ohm resistor and try the same thing, but I you should wear safety glasses.
The greater the resistance , the longer the discharge time, as indicated by the numbers above.
The 1 ohm resistor will probably explode into flames immediately. (FYI)

Use a 120vac relay that gets power from the same source as the phone charger, then have the output of the phone charger go though the N.O. contacts on the relay.

The instant the power is cut to the relay, it breaks the supply to the Arduino which will result in a very fast shutdown.

Use a 120vac relay that gets power from the same source as the phone charger, then have the output of the phone charger go though the N.O. contacts on the relay.

The instant the power is cut to the relay, it breaks the supply to the Arduino which will result in a very fast shutdown.

While on the face of it , this sounds like a brilliant idea, the catch is that we already discussed that the response time for relay is about 30 to 40 mS, which is about the same amount of time it would take for
the contacts to separate if the relay were wired as you describe.
In short :

will result in a very fast shutdown.

is subjective. I would not necessarily say that 40mS is "very fast".

However, a high side switch mosfet would be "very fast" .

td(on) = 40nS

FQP30N06L MOSFET
So , while it is not a bad idea, it just isn't as fast as a hide side switched mosfet.

ASAPELLA:
the arduino keeps current for a couple of seconds

Agree, a mosfet would be much faster and likely more importantly quieter.

I was just suggesting something easily added from a junk drawer of parts and 50ms is about the time a average person would take to remove a finger from a push button.

Agreed, but honestly I am not convinced that there is an issue,

As DVDDoug pointed out back in Reply #2 (at the beginning of this thread):

Obviously the Arduino has to remain powered-up while you write the EEPROM

This is the OP's original post:

the only problem I have is that, since I need to power arduino off of an old phone charger and so to remove power from the arduino completely and saving the solid state into eeprom when i switch off the system the arduino keeps current for a couple of seconds making it impossible to fastly switch between states.

Only the OP knows what he meant but it sounds like he is trying to say that he wants to save the STATE
of the SSR (Solid State Relay) before powering down and going into sleep mode. What I think he does
not realize is that saving the state is not going to keep the SSR on. If, on the other hand he thinks he needs to save it so that when the processor wakes up it knows whether or not to turn ON the relay,
then maybe he has a point but I don't see what that has to do with the shutdown sequence. If he saves
the SSR STATE before shutting down, when what difference does it make how long it takes for the arduino to power down. In short, I don't think this post makes any sense , from either a H/W or a
S/W viewpoint.

Hi,
thanks everyone for your effort. I understand I have confused some with my clunky explanation,
if you go through the code you can see that the state of the SSR is saved at the beginning of the cycle and when a new cycle starts it is changed by means of if else

int quanto() {
  int check = (EEPROM.read(0));
  Serial.println("check1");
  Serial.println(check);
  if (check < 2) {
    ++check;
  }
  else {
    check = 1;
  }
  Serial.println("check2");
  Serial.println(check);
  return check;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  EEPROM.update(0, quanto());
  digitalWrite(ledPin1, HIGH);
}
}

This is why it is not necessary to save the SSR state on switch off.
The fact that the arduino doesn't completely turn off immediately when power is cut down is a hassle because i want someone to rapidly turn it on and off in order for the cycle in the code to restart, that is not possible if the arduino stays on.

I understood I have expressed myself wrongly and what I really need is to discharge the current still held in the capacitors of the power supply.

The relay sounds like a neat idea, I am not really concerned with 40ms of latency because, as it was said, it is very hard to find someone with that fast finger.

For practical reason I would prefer to use a mosfet though, because it is much smaller, could you guide me on how to hook it up?
As you probably understood I am not that expert and wouldn't want to accidentaly short something.

Thanks again

You do realize that everyone else who writes code to do this uses delays so instead of expecting the arduino to magically turn off in an instant ?

I honestly believe this post does not belong in General Electronics because nobody is going to
be able to tell you how to change the laws of physucs. You need to learn to put the arduino in a delay loop after sending the shutdown signal so it can't do anything while the power is draining.
If you can't do that then ask the moderator to move this post to Programming because there is nothing "General" about stopping electrons.

ASAPELLA:
i want to control multiple lights from a single switch so that by switching on and off one or two times you can turn on only one light or more than one.

Might just be me, but the above's no clearer than the original explanation below:

I am trying to control a lamp so that when one presses the switch once the lamp turns on, on the second press the lamp doesn't turn on

All the arcane electrical discussion is moot, if you ask me, in the absence of a clear understanding of what's needed.

But then, those who have responded above may actually have twigged to the requirement; I certainly haven't :wink:

All the arcane electrical discussion is moot, if you ask me, in the absence of a clear understanding of what's needed.

Agreed, I don't understand what is required either.

This:

This is why it is not necessary to save the SSR state on switch off.
The fact that the arduino doesn't completely turn off immediately when power is cut down is a hassle because i want someone to rapidly turn it on and off in order for the cycle in the code to restart, that is not possible if the arduino stays on.

I understood I have expressed myself wrongly and what I really need is to discharge the current still held in the capacitors of the power supply.

The relay sounds like a neat idea, I am not really concerned with 40ms of latency because, as it was said, it is very hard to find someone with that fast finger.

While I don't really understand what the OP is trying to do, smells of an XY problem

It looks like the OP is updating the EEPROM in the main loop and then shutdowning power with no
delay after the EEPROM write. I think this whole post centers around the absence of that delay.
I am recommending that he add one. (as indicated in code below)

@OP,
If you are going to post your code for us to examine (in reply #5), at least have the courtesy to include
comments explaining each line of code. (like WHAT you are doing with "check" ! Put comments explaining
what that does !)

Software is not my area of expertise, but if you ask me, if you are going to put your EEPROM update routine in the main loop, you might want to put delay longer than 1 second.

Like this:

#include <EEPROM.h>
#include <Wire.h>
#include <SoftwareSerial.h>

const int ledPin1 =  11;
int check = EEPROM.read(0);

void control(int check)  {  
                                       switch (check) 
                                       {       case 1: 
                                                   digitalWrite(ledPin1, HIGH);
                                                   break;
                                                case 2:
                                                   digitalWrite(ledPin1, LOW);
                                                   break;
                                        }
                                    }

int quanto()  
          {  int check = (EEPROM.read(0));
                               Serial.println("check1");
                               Serial.println(check);

                               if (check < 2)   {  ++check;    }
                               else            {  check = 1;   }

              Serial.println("check2");
              Serial.println(check);
              return check;
           }



void setup() 
          {
            Wire.begin();
            Serial.begin(9600);
            pinMode(ledPin1, OUTPUT);
            EEPROM.update(0, quanto());
            digitalWrite(ledPin1, HIGH);

            delay(5000);   // powerdown delay
          
           }

void loop()
           {

               control(EEPROM.read(0));
               Serial.println("check3");

               delay(5000);     // powerdown delay

               Serial.println(EEPROM.read(0));
               delay(1000);
               Serial.println(EEPROM.read(0));
               delay(1000);
            }