Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 18 of 18

Thread: Sleep code - Teensy 3.2

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Sleep code - Teensy 3.2

    I am using Teensy 3.2 and I need to program sleep code to save battery power.
    I tried to use avr/sleep.h but it is not compatible with it.
    Thanks to help me.
    Best regards.
    AIREL46

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    https://github.com/duff2013/Snooze for Teensy LC/3.2/3.5/3.6

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Red face Snooze Teensy 3.2

    Quote Originally Posted by Theremingenieur View Post
    https://github.com/duff2013/Snooze for Teensy LC/3.2/3.5/3.6
    Bonjour,
    Merci de ta réponse qui correspond effectivement à mon besoin.
    J'ai lu le fichier README, c'est pas très simple à comprendre.
    Je voudrai trouver un exemple qui s'adapte à mon cas.
    Mon programme consiste en la capture périodique (30 secondes) de 2 températures.
    Le Teensy est alimenté par une batterie LI-ION.
    Le déroulement du programme prend environ 2s, il reste donc 28s pour passer en "Snooze".
    Donc, quel exemple choisir ?
    AIREL46

  4. #4
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    First of all, with respect to the majority of users here, we should keep that conversation in English.

    Everything depends on how your application will wake up. I think that in your case, the LPTMR is probably the simplest way to go. So you would declare a snoozeTimer driver, attach it to a snoozeBlock and configure it with setTimer(30000) to trigger a wake up every 30 seconds. In the loop() you would write your code to capture the temperatures before sending the Teensy back to sleep with deepSleep(). No need for an example, your use case seems so simple, you might go immediately live.

  5. #5
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Sleep code - Teensy 3.2

    Sorry for french language and thank you for your answer.
    I did write a small programme without my code to capture the temperatures (test.ino) but I meet new problem after checking with the Arduino IDE, I have un error message:
    "SnoozeTimer" does not name a type

    Best regards.
    Attached Files Attached Files

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    Make sure that you have the latest Teensyduino 1.4.2 version installed, which includes the version 6.3.1 of the Snooze library. Then, you can compile your sketch without the error you mentioned, but it will give another error for your deepSleep() line. Replace it by Snooze.deepSleep(config) and it will work.

    Code:
    #include <Snooze.h>
    
    
    const int led_pin_v = 13;//Led verte
    // Load timer driver
    SnoozeTimer timer;
    
    
    // install timer driver to a SnoozeBlock
    SnoozeBlock config(timer);
     
    void setup()
    {
      
      Serial.begin(9600);
      pinMode(led_pin_v, OUTPUT);
      timer.setTimer(30000);
        
    }
     
    void loop()
    {
      digitalWrite(led_pin_v, HIGH);
      Snooze.deepSleep(config);
      digitalWrite(led_pin_v, LOW);
    }
    The compile result looks then like

    Code:
    Using library Snooze at version 6.3.1 in folder: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Snooze 
    Sketch uses 13336 bytes (5%) of program storage space. Maximum is 262144 bytes.
    Global variables use 5044 bytes (7%) of dynamic memory, leaving 60492 bytes for local variables. Maximum is 65536 bytes.

  7. #7
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Red face Sleep mode - Teensy

    After updating Snooze and copy your program, It is now OK.
    The result is good: 46 mA in normal operation and 1.5 mA in sleep mode.
    Thanks a lot for your help.
    AIREL46

  8. #8
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Sleep mode and serial monitor

    The sleep mode operates properly but interrupts the console monitor.
    The USB serial connexion operates with the /dev/ttyACM0 port.
    Without the sleep mode this port is identified with lsusb command.
    With the sleep mode this port is not identified with lsusb command.
    The Teensy 3.2 works with Ubuntu 16.04 LTS.
    Thank you to help me.
    Airel46
    Attached Files Attached Files

  9. #9
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    That's normal and intended behavior. The internal USB engine in the Teensy is disabled (as are most the internal peripherals) when it goes to sleep.

    I also do not talk when I sleep...

  10. #10
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Sleep mode and serial monitor

    OK, I understand that during sleep mode (28 secondes) the Teensy does not do anything.
    But I do not understand that during normal mode (2 secondes) the teensy can not manage serial communication ?

  11. #11
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    Ah... you didn't write explicitly that the USB connection would also not be reactivated after the Teensy waking up. Normally it should come online when it is reactivated.

    Try to add a Serial.begin(9600); after Snooze.deepSleep(config);

    If that does not help, you'll probably have to add the usb_serial driver to your snooze config.

  12. #12
    Junior Member
    Join Date
    Jul 2018
    Posts
    9

    Sleep mode and serial monitor

    Sorry because my English is not very good !

    Exactly, the USB connection is not be reactivated after the Teensy waking up.
    The question is how to reactivate the USB connection after the Teensy waking up?

  13. #13
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,453
    Read the documentation of the Snooze library on GitHub. There is even a specific example with USB serial communication which shows how to add the SnoozeUSBSerial driver to your snooze configuration.

  14. #14
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    961
    Quote Originally Posted by AIREL46 View Post
    Sorry because my English is not very good !

    Exactly, the USB connection is not be reactivated after the Teensy waking up.
    The question is how to reactivate the USB connection after the Teensy waking up?
    Yes look at the deepSleep_usb_serial example sketch. Also it takes sometimes a couple of seconds for the Teensy and your computer to establish usb communications again so rapid sleeping and waking just won't work.

    Another option is to get Uart <-> USB converter, the Uart is available pretty much after waking if you already have it configured before hand.

  15. #15
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    Hi there! First of all, thank you very much for your activities!
    This post has been interesting to me since I need a reliable way to put my teensy3.2 to sleep.
    I used Ariels code (thx!) and updated it a little bit:

    #include <Snooze.h>


    const int led_pin_v = 13;//Led verte
    // Load timer driver
    //SnoozeTimer timer;
    SnoozeDigital digital;
    SnoozeUSBSerial usb;

    // install timer driver to a SnoozeBlock
    //SnoozeBlock config(timer);
    SnoozeBlock config(usb, digital);

    float j;

    void setup()
    {

    Serial.begin(9600);
    pinMode(led_pin_v, OUTPUT);
    //timer.setTimer(30000);
    digital.pinMode(7, INPUT_PULLUP, RISING);//pin, mode, type
    }

    void loop()
    {

    j=1+j;
    Serial.print("j= ");
    Serial.println(j);

    digitalWrite(13, HIGH);
    delay (20);
    digitalWrite(13,LOW);
    delay (20);

    if (j>200)
    {
    delay(1000);
    digitalWrite(led_pin_v, LOW);
    delay(1000);
    Serial.println("Bonne nuit SCAO");
    Snooze.deepSleep( config );
    delay(10000);
    Serial.begin(9600);
    Serial.println("Bonjour SCAO");
    digitalWrite(led_pin_v, HIGH);
    j=0;

    }
    }

    It does:
    1. count j to 200
    2. go deepsleep
    3. wake up on digital PIN 7
    4. count again to 2000 (the LED is blinking the way it is supposed to:=))

    BUT the serial monitor is strange:
    1. Does not show Bonne nuit SCAO
    2. Does not show Bonjour SCAO
    3. After wake up does not show the counting any more

    Per your suggestion I already gave it some seconds after waking up, I put an additional Serial.begin in there, added SnoozeUSBSerial usb. Any other ideas?

    Your suggestions are greatly appreciated!

    jani

  16. #16
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    961
    Can you try the "deepSleep_usb_serial" example and make sure your "USB_Type:" in Arduino is "Serial".

  17. #17
    Junior Member
    Join Date
    Mar 2019
    Posts
    2
    It actually works, thanks!

    I melted the example down to what I (teensy3.2, Button on PIN7) really need:

    /***************************************
    This shows the use of the Snooze USB Serial driver when using deepSleep.
    The USB Serial driver does not wake the processor it only aids in using the USB
    Serial in sleeping appilcations.
    ****************************************/
    #include <Snooze.h>
    // Load drivers
    SnoozeDigital digital;
    SnoozeUSBSerial usb;
    /************************************************** *********
    Install drivers, timer to wake and USB Serial Driver to
    fix printing to serial monitor after sleeping.
    ************************************************** *********/
    SnoozeBlock config_teensy32(usb, digital);
    void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    while (!Serial);
    delay(100);
    Serial.println("Starting...");
    delay(100);
    /************************************************** ******
    Define digital pins for waking the teensy up. This
    combines pinMode and attachInterrupt in one function.
    ************************************************** ******/
    digital.pinMode(7, INPUT_PULLUP, RISING);//pin, mode, type
    }
    void loop() {
    /************************************************** ******
    feed the sleep function its wakeup parameters. Then go
    to deepSleep.
    ************************************************** ******/
    Snooze.deepSleep( config_teensy32 );
    // wait for serial monitor
    elapsedMillis time = 0;
    while (!Serial && time < 1000) {
    Serial.write(0x00);// print out a bunch of NULLS to serial monitor
    digitalWriteFast(LED_BUILTIN, HIGH);
    delay(30);
    digitalWriteFast(LED_BUILTIN, LOW);
    delay(30);
    }
    // normal delay for Arduino Serial Monitor
    delay(200);
    Serial.println("Hi");
    delay(1000);
    }

    Does that seem right to u?



    The previous script worked finally fine, IF I manually restart the serial monitor after the first time pressing the button. Than after the second press the counting started again. Do you know

    1. how I can avoid having to restart the monitor manually
    2. why do I have to press the button twice?

    Again, thx for your super quick help!

  18. #18
    Senior Member duff's Avatar
    Join Date
    Jan 2013
    Location
    Las Vegas
    Posts
    961
    Can you please put your code between code brackets in this forum, it makes reading your code much easier. Just to be sure you are using the Arduino Serial Monitor and what OS?

    I don't know why you have to push the button twice, you haven't shown or explained how your button is wired to the teensy but I can say that the digital wakeup has been tested many many times to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •