Can anyone help me to build a code which will activate a LED after 2 minutes when the power supply is cut off and blink that LED after a time interval of 30 seconds. I want to import the code on Arduino-Uno.

I will power the Arduino with cell and assign two pins as sensor if they are connected to each other than every this is fine if they are cut the Arduino will blink the LED after 2 minutes and will keep blinking at 30 seconds interval

  • 1
    How do you expect to do anything if the power has been removed?!
    – Majenko
    Oct 2, 2016 at 17:17
  • I will power the Arduino with cell and assign two pins as sensor if they are connected to each other than every this is fine if they are cut the Arduino will blink the led after 2 minutes and will keep blinking at 30 seconds interval. Oct 2, 2016 at 18:48
  • Can any one help me with a code that can switch ON a pump for 5secs and then switch on a blower for 3secs and the operation continues like that. Mar 31, 2020 at 10:33

4 Answers 4


Use a commonly available USB power pack re-charging from mains and powering the Arduino. Use a light source directly powered by main and optically couple that to an Arduino. There are plenty of examples on the web where an Arduino is used to detect light. Once triggered, write some additional code to blink the led. Since you are not doing anything other than timing, you can use the delay Arduino library to time events.

  • I have edited it now can you please help me Oct 2, 2016 at 18:49
  • I think it would be easier to use a rechargeable USB power pack. They are very common. I added a let-me-google-that-for-you link so you can see what I am talking about. I still think optically isolating mains power from the Arduino is a good idea. So I am sticking with what the answer says. Read the pages the links take you to. Then ask your questions. It is always best if you make an attempt at programming then ask your questions posting your code with your question.
    – st2000
    Oct 2, 2016 at 19:32

The Blink example will tell you how to blink an LED, you just need to change the times in the delay statements.

You want to detect the failure of a power supply. You Arduino has a backup battery (cell) to power it when the power fails. Correct?

There are voltage sensor kits available that can monitor mains voltage, just search for "Arduino Voltage sensor". However there is a risk to you working with main voltages. So using optical isolation (as ST2000 says) is probably safer for you. There may be some optical isolator IC that can manage mains and 5V, they are usually white in colour.


I have a code edited by me

int pinButton = 8; //the pin where we connect the button
int LED = 2; //the pin we connect the LED

void setup() {
 pinMode(pinButton, INPUT); //set the button pin as INPUT
 pinMode(LED, OUTPUT); //set the LED pin as OUTPUT

void loop() {
 check:int stateButton = digitalRead(pinButton); //read the state of the button
 if(stateButton == 0) { //if not pressed

   digitalWrite(LED, HIGH);
   digitalWrite(LED, LOW);
   goto check;
 else { //if  pressed
  digitalWrite(LED, LOW);
  goto start;
  • The goto start is completely redundant, because at the end of loop it goes back to the start of it anyway. Please avoid using goto - your other one can be replaced by a while loop
    – Nick Gammon
    Oct 4, 2016 at 7:15

DISCLAIMER: This is just a suggestion. No hate for this anwser please.

You could use a capacitor. A super capacitor (high capacity) with a proper joule theft circuit for your project could power the arduino for propably another 5min even if main power is cut off, plus you do not need any extra power source once the main source is cut off. It can be useful if you need to change the battery or move it to an other location but you do not want the module to be turned off.

const int led = 13;    //led pin
const int sensor = A0; //connect this your power supply, use a voltage divider if necessary
                       //this will act like a volt meter

void setup()
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);

void loop()
  int V = analogRead(sensor);
  V = map(V,0,1023,0,5); // the 5 is replaced with the ammount of voltage that you supply the arduino

  if(V < 1) // when the power is below a point or simply disconnected, the capacitor 
            //will kick in by itself because the Arduino will draw its power
    while(V < 1)
      V = analogRead(sensor);
      V = map(V,0,1023,0,5);

      digitalWrite(led, HIGH);
      digitalWrite(led, LOW);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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