[SOLVED] Not able to put my core in stop mode (it just wakes up)

Hi,

No matter how I try I am no able to keep my core into stop mode using neither sleep(pin, trigger_mode) nor sleep(pin, trigger_mode, seconds). It seems putting itself in stop mode when requested but, after few seconds it wakes up.

I trimmed my current setup to a spark core plugged by usb with no wire attached to its pins and this code:

#define LED_PIN D7
#define WAKE_UP_PIN D2

void setup() {
    pinMode(LED_PIN, OUTPUT);
    pinMode(WAKE_UP_PIN, INPUT);
}

void loop() {
    digitalWrite(LED_PIN, HIGH);
    delay(4000);
    digitalWrite(LED_PIN, LOW);
    
    Spark.sleep(WAKE_UP_PIN, RISING);
}

I did updated my firmware as indicated here with not problem (apparently).

Any idea or suggestion?

Do you happen to be using a white core? :slight_smile:

Hi kennethlimcp! Thanks for answering back

Sorry but I am not sure what a white core is :laughing: Could you please tell me?

It’s simply the colour of the core you have :smile:

Also, did you place a pull down resistor on the pin you are using to trigger the wake up?

My core pcb is not white but blue!

I did not placed a pull down resistor neither. Actually, there is no pin connected at all in that minimal setup. The problem that I am experimenting is not the core not waking up but waking up constantly against my expectations.

It executes the code in the loop (turning on and off the built in led) then it seems to put itself into stop mode and, after aprox 3 seconds it wakes up again

I am afraid I dont fully understand the logic behind those pull (up/down) resistors.

As far as I understood they mean to stabilize the voltage within a pin under several conditions preventing them to float. Is that right? What conditions are those and why reading from a sensor using that pin works perfectly detecting HIGH and LOW states while using wakeup based on those states dont?

Hi @netropo, if you do pinMode(WAKE_UP_PIN, INPUT_PULLDOWN); you do instruct the Core to attach an internal pull-down resistor to the pin.

Since an INPUT pin does not have any electric potential for itself it just attaches itself to any outside potential - if there isn’t one (e.g. due to open switch) it just floats away and might just float above or below the LOW/HIGH threshold.
While an external sensor does provide this “guide potential” an open switch doesn’t.
To prevent this you use a high value resistor connected to the HIGH or LOW rail of your circuit to fix a potential level for an open pin, but exert least possible interference on the signal you want to detect.

1 Like

Thanks for the explanation @ScruffR :smile:

So I guess in this case I need to use a pull down for that pin so it remains in a LOW state, sounds that reasonable?

I tried again but with no luck. It stills wakes up couple of seconds after putting into stop mode :frowning:

#define WAKE_UP_PIN D2
#define DEBUG_LED_PIN D7

void setup() {
  pinMode(WAKE_UP_PIN, INPUT_PULLDOWN);
  pinMode(DEBUG_LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(DEBUG_LED_PIN, HIGH);
  delay(4000);
  digitalWrite(DEBUG_LED_PIN, LOW);

  Spark.sleep(WAKE_UP_PIN, RISING, 60);
}

Sorry, @kennethlimcp, I don’t mean to push you out, but I just happend to “drive by” :wink:

@netropo, yes this does sound perfectly reasonable!

And I haven’t got an expanation yet, why it doesn’t work but I’m sure @kennethlimcp and I will be lookint into this a bit more.

Stay tuned.

1 Like

Does it wake up after 60 seconds or way earlier? I’m travelling but will get to it later :wink:

Could you just try removing the 60 from your Spark.sleep() to remove some vaialbles from the equation?

@kennethlimcp way earlier, just about 2 seconds after going into sleep

@ScruffR Done, but same result. I’ve even detached the Core from the breadboard it was plugged in just to discard other reasons. Its literally floating in the air now!

@netropo, it is puzzling but I’m now starting to guess around a bit and you might just have to try it out.

Could you try another pin as WAKE_UP_PIN
Could you also try this

#define LED_PIN D7
#define WAKE_UP_PIN D2


void setup()
{
    pinMode(LED_PIN, OUTPUT);
    pinMode(WAKE_UP_PIN, INPUT_PULLDOWN);
    delay(100);

    Spark.function("gotosleep", goToSleep);
    Spark.sleep(WAKE_UP_PIN, RISING);
}

void loop() 
{
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));  // toggle LED
    delay(4000);
}

int goToSleep(String c)
{
    if (digitalRead(LED_PIN))
    {
        pinMode(WAKE_UP_PIN, INPUT_PULLDOWN);
        delay(10);
        Spark.sleep(WAKE_UP_PIN, RISING);
        return 1; // LED is on wake rising
    }
    else
    {
        pinMode(WAKE_UP_PIN, INPUT_PULLUP);
        delay(10);
        Spark.sleep(WAKE_UP_PIN, FALLING);
        return 0; // LED is off wake rising
    }

    return -1;  // never end up here
}

What build environment are you using?
How do you power your Core?

What happens when you call Spark.function() when LED is off?
What happens when you call Spark.function() when LED is on?

Edit: Updated code

@ScruffR give me one minute and I will come to you with the outcome. I guess the first function in your code snippet should be called setup instead of sleep, is that right?

Sorry, there are some compile errors, I’ll fix them quickly.

Updated the code above and I’m trying it on one of my Cores too.

I've flashed my core with the code above but as soon as it gets wifi connection it goes to sleep due to that sleep() call in the setup() method. It wakes up as usual couple of seconds after that. Therefore the led never gets turned on (it seems the loop is not even executed)

Regarding your previous questions I've tried using web ide, command line remote flashing and command line usb flashing. The Core is powered by a USB cable connected to my laptop

If I remove that sleep() call on the setup() function, the loop gets executed, blinking the led in intervals of 4 seconds as expected.

If during one of these periods I call goToSleep via API, it does not return anything. The Core simply goes to sleep and wakes up couple of seconds after, regardless of the led was on or off

That’s very odd, since my Core does just behave as expected - I also have the immediate sleep, but it never wakes up unless I pull D2 HIGH.

Then after removing the sleep from setup() it just blinks away until I call the fn and then it sleeps away till I pull HIGH or LOW depending on the LED state before sleep.

Have you tried another pin?
Can you try some factory resets?

Already tested with D1, D2, D5. Same result. Tried factory reset as well, nothing :cry:

I don’t know if this does play a role here, but my white Cores do work just as expected even with the 60 second auto wake, everything does what it is supposed to do.
Why your Core doesn’t behave propperly is most ominous!!!

Maybe Dave has some idea or knows who else to tag - so I just tagged @Dave.

This is my perfectly working code

#define LED_PIN D7
#define WAKE_UP_PIN D2


void setup()
{
    pinMode(LED_PIN, OUTPUT);
    pinMode(WAKE_UP_PIN, INPUT_PULLDOWN);
    delay(100);

    Spark.function("gotosleep", goToSleep);
    
    delay(5000);
    
    //Spark.sleep(WAKE_UP_PIN, RISING, 60);
}

void loop() 
{
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));  // toggle LED
    delay(4000);
}

int goToSleep(String c)
{
    if (digitalRead(LED_PIN))
    {
        pinMode(WAKE_UP_PIN, INPUT_PULLDOWN);
        delay(10);
        Spark.sleep(WAKE_UP_PIN, RISING, 60);
        return 1; // LED is on wake rising
    }
    else
    {
        pinMode(WAKE_UP_PIN, INPUT_PULLUP);
        delay(10);
        Spark.sleep(WAKE_UP_PIN, FALLING, 60);
        return 0; // LED is off wake rising
    }

    return -1;  // never end up here
}