[SOLVED] Sleep dont run
-
@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. -
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:
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:

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
@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 ?
-
@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 ?
-
@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
-
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
-
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
@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.
-
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@mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.
-
@mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.
-
@mar.conte The hcr501 will become unstable below 3V. So if you are powering it with 2AA you can expect problems.