Wake up on external interupt - Leonardo

Hi,

I am sure this is obvious... however I cannot get it to work! I am building an add on to a large project wihich requires battery backup when not powered externally. when the external power fails I want to retain the state of the processor/memory and resume when normal power is restored.

The board is a leonardo. I do not seem to be able to get the board to wake up on external interupt. I know the interupt is working, and I know the board is going to sleep.

I have explored a number of online examples, and those provided with the lowerpower library, but to no avail.

The external interupt I am using is int 4 on pin 7.

My test code

#include "LowPower.h"

volatile bool triggered=false;
void wakeUp()   {triggered=true;}

void setup()
  {
    pinMode(7, INPUT);
    pinMode(LED_BUILTIN, OUTPUT);  
    delay(10000) ; //so as not to brick it!
    attachInterrupt( 4, wakeUp,RISING);
    triggered=false;
  }

void loop() 
  {
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000); 
    USBCON = 0;
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
    USBDevice.attach();
    triggered=false;
    digitalWrite(LED_BUILTIN, HIGH);
    delay(5000); 
  }  
 // from testing the interrupt was fired 
  // if (triggered) Serial.println("interrupted!");
  // else {Serial.println("normal");}
  // triggered=false;

the bit commented out, and the volatile 'triggered' were part of testing that the interrupt is seen.

as it stands, the processor goes to sleep... and never wakes up - the led never comes on

As the project uses serial comms (both Serial1 UART and Serial via USB) I have included the usb on/off stuff to make sure the leonardo USB set up is happy.

Thankfully I had the forsight to put a 10second window in the setup to allow for reprogramming!

Electrically, for this test, I am powering the board via USB, pin 7 has a 10k pull down to ground, and the trigger is from the 5v pin on the leonardo.

Any thoughts please

Thanks Ian

Take a look at

http://www.gammon.com.au/power

if you haven’t. One thing off the top of my head is the use of detachInterruot in his examples.

I had better luck (total success) cutting and pasting from his examples. The libraries I used were less useful; TBH this may have been before I knew at all what I was up to rather than to be taken as an indictment.

But that link has a great deal of good info and is worth the time.

Also I wasn’t using LEO, rather UNO and a pro micro that I did the standard low power modifications to. So there may be some LEO specific problem unaddressed in a library or your use of it.

a7

Hi @ianmorris1960_2,

You could go about this a completely different way if you want to. You could attach the 5V pin of the Leonardo to an analog pin, and read the input as a voltage. When it drops below, say, 4.5V, then save a value in eeprom, and you could have the Leonardo go to sleep. On wakeup, you read eeprom to see if there was a power loss. Im not sure what your project is so I'm not sure if this would work for you.

You picked the wrong interrupt mode. :frowning:

The datasheet says that you can wake up from power-down with INT0 through INT3 and INT6. (For INT6, only level interrupt.) You are using INT6 (Pin 7) so you can't use "RISING". You have to use "HIGH".

The available pins for attachInterupt() on the Leonardo are 0, 1, 2, 3, and 7.

INT0 on Pin 3 Interrupt 0
INT1 on Pin 2 Interrupt 1
INT2 on Pin 0 Interrupt 2
INT3 on Pin 1 Interrupt 3
INT6 on Pin 7 Interrupt 4

NOTE: You should always use digitalPinToInterrupt():
attachInterrupt( digitalPinToInterrupt(7), wakeUp, HIGH);