Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. [SOLVED] Sleep dont run

[SOLVED] Sleep dont run

Scheduled Pinned Locked Moved Troubleshooting
51 Posts 8 Posters 11.4k Views 7 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • YveauxY Yveaux

    This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

    @AWI Ok, last reply :simple_smile:

    I'll have to dwell a little to explain how the AVR works and what its limitations are regarding sleeping, and how the MySensors library handles it.

    For AVR architecture, the MySensors library uses the 'Power-Down mode' when sleeping.
    I'll focus on ATMega328P here, for which the datasheet states the possible wake-up sources:

    0_1490722068118_upload-b2cbd80d-2c88-4e52-8bae-c46adb341c4f

    So in our case that's INT and WDT (TWI Address match is for i2c slave implementations).
    When a timeout parameter is passed to a sleep() function of the MySensors library the watchdog (WDT) will be used to wake after the specified timeout. If timeout is set to 0 (and wake-up from interrupts is specified) the watchdog will be completely disabled to save some more power.
    When an interrupt source is passed to a sleep() function of the library it will configure INT0 and/or INT1 to wake up the ATMega328.
    Note point 3, as only level interrupts (more precise LOW in case of ATMega328, thanks @AWI for reminding me) can be used as a wake-up source.
    Many posts here use RISING/FALLING/CHANGE as wake-up source for ATMega328 which is not supported by the ATMega328P and thus not supported by the MySensors library. Although people claim it is working for them you are on your own when using the chip out of spec and can expect strange behavior!

    The datasheet continues in detail on the power-down mode:

    0_1490721842267_upload-e4216667-02ab-4505-989f-e2b14c8edfaf

    The MySensors library disables brown-out to save some power. Serial interface address match and pin change interrupt are not used by the library.
    Pay special attention to the note: Waking the AVR from a INT0/INT1 interrupt will require the LOW level to remain for the startup-time, or the interrupt will not trigger. This means that only when the level is held long enough the library will be able to detect it woke from the external interrupt. If the level is not held at least the startup-time, it will assume it woke because of the total sleep time expired, and return MY_WAKE_UP_BY_TIMER (value -1).

    This start-up time depends on the clock frequency and fuse bits, which for e.g. an 8MHz Arduino Pro Mini comes down to 2ms.

    So just remember: In the MySensors library, only use LOW level interrupts to wake an ATMega328 from sleep and assure the interrupt level remains constant for at least the start-up time!

    @AWI Your third example is out-of spec (CHANGE interrupt) and behavior is therefore undefined. If it seems to work, you're lucky...

    This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

    mar.conteM Offline
    mar.conteM Offline
    mar.conte
    wrote on last edited by
    #38

    @Yveaux
    thanks for the lesson, really complete, tomorrow I will try to put into practice the advice then I put "high" and will default interrupt with a 10k resistor input pir sull'interrupt 1. Thanks'll let you know

    M.C.

    1 Reply Last reply
    0
    • YveauxY Yveaux

      This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

      @AWI Ok, last reply :simple_smile:

      I'll have to dwell a little to explain how the AVR works and what its limitations are regarding sleeping, and how the MySensors library handles it.

      For AVR architecture, the MySensors library uses the 'Power-Down mode' when sleeping.
      I'll focus on ATMega328P here, for which the datasheet states the possible wake-up sources:

      0_1490722068118_upload-b2cbd80d-2c88-4e52-8bae-c46adb341c4f

      So in our case that's INT and WDT (TWI Address match is for i2c slave implementations).
      When a timeout parameter is passed to a sleep() function of the MySensors library the watchdog (WDT) will be used to wake after the specified timeout. If timeout is set to 0 (and wake-up from interrupts is specified) the watchdog will be completely disabled to save some more power.
      When an interrupt source is passed to a sleep() function of the library it will configure INT0 and/or INT1 to wake up the ATMega328.
      Note point 3, as only level interrupts (more precise LOW in case of ATMega328, thanks @AWI for reminding me) can be used as a wake-up source.
      Many posts here use RISING/FALLING/CHANGE as wake-up source for ATMega328 which is not supported by the ATMega328P and thus not supported by the MySensors library. Although people claim it is working for them you are on your own when using the chip out of spec and can expect strange behavior!

      The datasheet continues in detail on the power-down mode:

      0_1490721842267_upload-e4216667-02ab-4505-989f-e2b14c8edfaf

      The MySensors library disables brown-out to save some power. Serial interface address match and pin change interrupt are not used by the library.
      Pay special attention to the note: Waking the AVR from a INT0/INT1 interrupt will require the LOW level to remain for the startup-time, or the interrupt will not trigger. This means that only when the level is held long enough the library will be able to detect it woke from the external interrupt. If the level is not held at least the startup-time, it will assume it woke because of the total sleep time expired, and return MY_WAKE_UP_BY_TIMER (value -1).

      This start-up time depends on the clock frequency and fuse bits, which for e.g. an 8MHz Arduino Pro Mini comes down to 2ms.

      So just remember: In the MySensors library, only use LOW level interrupts to wake an ATMega328 from sleep and assure the interrupt level remains constant for at least the start-up time!

      @AWI Your third example is out-of spec (CHANGE interrupt) and behavior is therefore undefined. If it seems to work, you're lucky...

      This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

      gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #39

      @Yveaux how do you handle a pir sensor if you can't use the Change interrupt?

      YveauxY 1 Reply Last reply
      0
      • gohanG gohan

        @Yveaux how do you handle a pir sensor if you can't use the Change interrupt?

        YveauxY Offline
        YveauxY Offline
        Yveaux
        Mod
        wrote on last edited by
        #40

        @gohan If you sensor signal goes HIGH the moment it detects movement one would sleep until the interrupt has HIGH level.
        After sending a message it is important to wait until the signal goes low again before sleeping, otherwise the sensor will wake immediately.

        http://yveaux.blogspot.nl

        gohanG 1 Reply Last reply
        0
        • YveauxY Yveaux

          @gohan If you sensor signal goes HIGH the moment it detects movement one would sleep until the interrupt has HIGH level.
          After sending a message it is important to wait until the signal goes low again before sleeping, otherwise the sensor will wake immediately.

          gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #41

          @Yveaux sure, but I would like to avoid having a sleeping node that wakes only with motion and then reports "no motions" only when sleep timeout is reached

          YveauxY 1 Reply Last reply
          0
          • YveauxY Yveaux

            This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

            @AWI Ok, last reply :simple_smile:

            I'll have to dwell a little to explain how the AVR works and what its limitations are regarding sleeping, and how the MySensors library handles it.

            For AVR architecture, the MySensors library uses the 'Power-Down mode' when sleeping.
            I'll focus on ATMega328P here, for which the datasheet states the possible wake-up sources:

            0_1490722068118_upload-b2cbd80d-2c88-4e52-8bae-c46adb341c4f

            So in our case that's INT and WDT (TWI Address match is for i2c slave implementations).
            When a timeout parameter is passed to a sleep() function of the MySensors library the watchdog (WDT) will be used to wake after the specified timeout. If timeout is set to 0 (and wake-up from interrupts is specified) the watchdog will be completely disabled to save some more power.
            When an interrupt source is passed to a sleep() function of the library it will configure INT0 and/or INT1 to wake up the ATMega328.
            Note point 3, as only level interrupts (more precise LOW in case of ATMega328, thanks @AWI for reminding me) can be used as a wake-up source.
            Many posts here use RISING/FALLING/CHANGE as wake-up source for ATMega328 which is not supported by the ATMega328P and thus not supported by the MySensors library. Although people claim it is working for them you are on your own when using the chip out of spec and can expect strange behavior!

            The datasheet continues in detail on the power-down mode:

            0_1490721842267_upload-e4216667-02ab-4505-989f-e2b14c8edfaf

            The MySensors library disables brown-out to save some power. Serial interface address match and pin change interrupt are not used by the library.
            Pay special attention to the note: Waking the AVR from a INT0/INT1 interrupt will require the LOW level to remain for the startup-time, or the interrupt will not trigger. This means that only when the level is held long enough the library will be able to detect it woke from the external interrupt. If the level is not held at least the startup-time, it will assume it woke because of the total sleep time expired, and return MY_WAKE_UP_BY_TIMER (value -1).

            This start-up time depends on the clock frequency and fuse bits, which for e.g. an 8MHz Arduino Pro Mini comes down to 2ms.

            So just remember: In the MySensors library, only use LOW level interrupts to wake an ATMega328 from sleep and assure the interrupt level remains constant for at least the start-up time!

            @AWI Your third example is out-of spec (CHANGE interrupt) and behavior is therefore undefined. If it seems to work, you're lucky...

            This information is outdated. An error in the ATMega328P datasheet has been confirmed. See https://forum.mysensors.org/topic/6572/sleep-with-interrupt-only-works-with-level-low

            tonnerre33T Offline
            tonnerre33T Offline
            tonnerre33
            Hardware Contributor
            wrote on last edited by
            #42

            @Yveaux said in [SOLVED] Sleep dont run:

            @AWI Your third example is out-of spec (CHANGE interrupt) and behavior is therefore undefined. If it seems to work, you're lucky...

            Are you saying that the example posted here https://www.mysensors.org/build/motion contains mistakes ?

            YveauxY 1 Reply Last reply
            0
            • tonnerre33T tonnerre33

              @Yveaux said in [SOLVED] Sleep dont run:

              @AWI Your third example is out-of spec (CHANGE interrupt) and behavior is therefore undefined. If it seems to work, you're lucky...

              Are you saying that the example posted here https://www.mysensors.org/build/motion contains mistakes ?

              YveauxY Offline
              YveauxY Offline
              Yveaux
              Mod
              wrote on last edited by
              #43

              @tonnerre33 yes

              http://yveaux.blogspot.nl

              1 Reply Last reply
              0
              • gohanG gohan

                @Yveaux sure, but I would like to avoid having a sleeping node that wakes only with motion and then reports "no motions" only when sleep timeout is reached

                YveauxY Offline
                YveauxY Offline
                Yveaux
                Mod
                wrote on last edited by
                #44

                @gohan I don't get your problem...
                I suppose my suggestion will solve both waking from motion and sleep timeout.

                http://yveaux.blogspot.nl

                1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #45

                  If you have a motion sensor it will immediately wake when motion is detected but it will report pir sensor change to "no motion" only at sleep timeout, and if timeout is like 30 minutes then the controller will have "motion detected" on for 30 minutes. So unless you make an if statement with two sleep function, one with short sleep if pir sensor is on and one with long sleep if pir sensor is off, you will not be quickly notified of pir state changes

                  YveauxY 1 Reply Last reply
                  0
                  • gohanG gohan

                    If you have a motion sensor it will immediately wake when motion is detected but it will report pir sensor change to "no motion" only at sleep timeout, and if timeout is like 30 minutes then the controller will have "motion detected" on for 30 minutes. So unless you make an if statement with two sleep function, one with short sleep if pir sensor is on and one with long sleep if pir sensor is off, you will not be quickly notified of pir state changes

                    YveauxY Offline
                    YveauxY Offline
                    Yveaux
                    Mod
                    wrote on last edited by Yveaux
                    #46

                    @gohan How about this ?
                    I think it covers the regular use-case (untested :baby:):

                    #define MY_DEBUG
                    #define MY_RADIO_NRF24
                    #include <MySensors.h>
                    
                    unsigned long SLEEP_TIME = 10000;
                    #define PIN_PIR (3)
                    
                    MyMessage msg(CHILD_ID, V_TRIPPED);
                    
                    void setup()
                    {
                        pinMode(PIN_PIR, INPUT);
                        // Configure what to send in case the PIR trips
                        msg.set("1");
                    }
                    
                    void presentation()
                    {
                        sendSketchInfo("Motion Sensor", "1.0");
                        present(CHILD_ID, S_MOTION);
                    }
                    
                    void loop()
                    {
                        // Sleep until woken by PIR motion, or timeout
                        switch( sleep(digitalPinToInterrupt(PIN_PIR), LOW, SLEEP_TIME) )
                        {
                            case MY_WAKE_UP_BY_TIMER:
                                Serial.println("Woke up by timer -- do something smart ;-)")
                                break;
                            case MY_SLEEP_NOT_POSSIBLE:
                                Serial.println("Unable to sleep ;-(")
                                break;
                            default:
                                Serial.println("Woke up by PIR -- sound the alarm!")
                                send(msg);
                                // Wait until PIR signal goes low again; poll every 100ms.
                                while( digitalRead(DIGITAL_INPUT_SENSOR) == HIGH )
                                {
                                    sleep(100);
                                }
                                break;
                        }
                    }
                    

                    Note the LOW level to wake the ATMega. Regular PIRs will generate a HIGH level on motion.

                    http://yveaux.blogspot.nl

                    1 Reply Last reply
                    1
                    • mar.conteM Offline
                      mar.conteM Offline
                      mar.conte
                      wrote on last edited by
                      #47

                      I can not solve the problem !!
                      I interrupt stabilized with 10k resistor, rewired everything but if my atmga + hcr501 +rfm69 with ftdi sleep in the consumption is 30 micro, if power with aa battery consumption is 420 micro help

                      M.C.

                      AWIA 1 Reply Last reply
                      0
                      • mar.conteM mar.conte

                        I can not solve the problem !!
                        I interrupt stabilized with 10k resistor, rewired everything but if my atmga + hcr501 +rfm69 with ftdi sleep in the consumption is 30 micro, if power with aa battery consumption is 420 micro help

                        AWIA Offline
                        AWIA Offline
                        AWI
                        Hero Member
                        wrote on last edited by
                        #48

                        @mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.

                        mar.conteM 2 Replies Last reply
                        0
                        • AWIA AWI

                          @mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.

                          mar.conteM Offline
                          mar.conteM Offline
                          mar.conte
                          wrote on last edited by
                          #49

                          @AWI
                          I have order 3,6 aa battery 2600 mah and try , i think what you say infact i have imagined this Tanks

                          M.C.

                          1 Reply Last reply
                          0
                          • AWIA AWI

                            @mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.

                            mar.conteM Offline
                            mar.conteM Offline
                            mar.conte
                            wrote on last edited by
                            #50

                            @AWI
                            But I was thinking if it is true this should not works rather well the system works fine without false alarms and detection pir is precisely why?

                            M.C.

                            1 Reply Last reply
                            0
                            • mar.conteM Offline
                              mar.conteM Offline
                              mar.conte
                              wrote on last edited by
                              #51

                              SOLVED
                              HI
                              FOR 3volt power i have insert jumper in the pin H of hcr501 and my project in sleep mode consumption is 30 micro Tanks for you suggesrion
                              By

                              M.C.

                              1 Reply Last reply
                              2
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              13

                              Online

                              11.7k

                              Users

                              11.2k

                              Topics

                              113.1k

                              Posts


                              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • MySensors
                              • OpenHardware.io
                              • Categories
                              • Recent
                              • Tags
                              • Popular