PIR example doesn`t wake Mega 2560?

  • Hello all,

    I just wired and tested the Motion Sensor example, (https://www.mysensors.org/build/motion),
    but my Mega 2560 wont wake from sleep because of the interrupt when motion is detected.
    It always finishes the sleeping. I serialprinted the sleeps output, it always states -2.
    The motion sensor works, I can see that, when I shorten the sleep time (tested with 1 sec).
    I also tested the output of the PIR, it generates more than 3.7 volts.
    I`m using the latest library and IDE. I made my best to read all connected forum posts but didn't find a solution.
    Anyone please can check it if it works for him?
    Any help would be appreciated!

  • Mod

    @vobi which pin (DIGITAL_INPUT_SENSOR) are you using?

    The following pins on the Mega has support for interrupt: 2, 3, 18, 19, 20, 21

  • @mfalkvidd
    I follow exactly the example, so D3.
    But D2 also doesn't work.

  • Mod

    Same problem here. I tried pretty much anything without success so far. I still need to check pins other than D2 and D3

  • Thanks, I will test the sketch on UNO.

  • So, I have tested the following:
    Sketch on Mega 2560,
    pin 3 with int1 - not working
    pin 3 with int5 - not working
    pin2, int0,4 - not working
    pins 18-21, (digitalPinToInterrupt(DIGITAL_INPUT_SENSOR)) - not working
    Sketch on UNO - works as it should!
    So definitely something strange going on with 2560s interrupts.
    Any advice?

  • Mod

    It looks like the mega is not meant to be used as a sleeping node 😁

  • Mod

  • Then what does it do when sleep is issued?
    Because the defined sleep-time still has to pass...and it does "nothing" till that, and that something cannot be broken by interrupt?
    What should be the good method to catch an interrupt, or a motion, without going thru the main loop crazy fast?
    Why cant it sleep?

  • Hardware Contributor

    i think mega2560 can go powerdown, but have not the hardware for testing.
    What you could do for isolting the issue, is removing sleep and check if the interrupt triggers well (with a bool flag)

  • @scalz
    Thanks, I will try it, and report back.

  • Mod

    I think the interrupt is triggered but it is just ignored by the sleep function, if the code I've been advised to use to check the interrupt is correct 😀

  • I have tested it, the interrupt indeed works fine.
    So yes, because there is no sleep, it cannot be interrupted.
    I still don`t understand why it cannot sleep.
    And IMHO the interrupt should break the not-so-sleep-but-wait cycle anyway.
    What do You think?

  • Mod

    I don't have this much deep knowledge on the chip, but my best guess is that there must be something different in the MEGA chip than the other on the at328p on the uno and mini pro. It could very well be that the MEGA is not designed to be a low power device, but I'll let others say something more appropriate :)

  • According to Mr. Nick Gammon, thats not the case (or I`m missing something):

    Running from a 9V battery through the "power in" plug, it draws about 50 mA.
    Running on 5V through the +5V pin, it draws about 49 mA.
    (Note: around 68 mA on a Mega 2560 board)
    Now we'll try putting it to sleep:
    Sketch B

    #include <avr/sleep.h>
    void setup () 
      set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
      sleep_cpu ();  
    }  // end of setup
    void loop () { }

    Now the Uno draws 34.5 mA. A saving, but not a lot.
    (Note: around 24 mA on a Mega 2560 board)

    Also see:

Log in to reply

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.