Arduino Forum

Products => Arduino Zero => Topic started by: josephchrzempiec on Jul 22, 2015, 02:23 am

Title: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 02:23 am
Hello i have asked a question in zero part of the forum but i feel this is really a programming question and problem. sorry if i double post wasn't sure what to do.

The question i have is that i got a sketch from nick gammon site to put the uno/mega board into sleep mode and that works great but i was wondering if  this will work for my arduino zero / m0 Pro? I don't want to try this into I'm sure of it just trying to be careful this time. Sketch is below.



Code: [Select]

#include <avr/sleep.h>
#include <avr/wdt.h>

const byte LED = 9;

void flash ()
  {
  pinMode (LED, OUTPUT);
  for (byte i = 0; i < 10; i++)
    {
    digitalWrite (LED, HIGH);
    delay (50);
    digitalWrite (LED, LOW);
    delay (50);
    }
   
  pinMode (LED, INPUT);
   
  }  // end of flash
 
// watchdog interrupt
ISR (WDT_vect)
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect
 
void setup () { }

void loop ()
{
 
  flash ();
 
  // disable ADC
  ADCSRA = 0; 

  // clear various "reset" flags
  MCUSR = 0;     
  // allow changes, disable reset
  WDTCSR = bit (WDCE) | bit (WDE);
  // set interrupt mode and an interval
  WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay
  wdt_reset();  // pat the dog
 
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
  noInterrupts ();           // timed sequence follows
  sleep_enable();
 
  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS);
  interrupts ();             // guarantees next instruction executed
  sleep_cpu (); 
 
  // cancel sleep as a precaution
  sleep_disable();
 
  } // end of loop
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: nickgammon on Jul 22, 2015, 02:29 am
It's really a Zero question, because you are asking about the Zero registers. Almost certainly any register fiddling won't work because it is a totally different processor. Macros like "set_sleep_mode" should work (if they compile at all) because they would be adapted to that processor.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 02:39 am
Hello Nick thank you.. Not sure how to change it but you are right it will not compile at all.  that is why I'm asking how i can change it to make it work with the Zero board?
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: nickgammon on Jul 22, 2015, 02:58 am
No idea. I'll move this to the Zero section.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 03:00 am
okay no problem thank you.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 04:54 am
Am i wrong to say sleep mode or is it that i need to say power saving mode is there a such thing and if so what is the difference between power saving mode and sleep mode?
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 04:59 am
Well sense the Due is closer to the M0 pro  what aobut putting that to sleep or power down mode?
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: Dirk67 on Jul 22, 2015, 09:33 am
Am i wrong to say sleep mode or is it that i need to say power saving mode is there a such thing and if so what is the difference between power saving mode and sleep mode?
everything is described well in the datasheet
--> http://www.atmel.com/images/atmel-42181-sam-d21_datasheet.pdf
Quote
The SAM D21 devices have two software-selectable sleep modes, idle and standby. In idle mode
the CPU is stopped while all other functions can be kept running. In standby all clocks and
functions are stopped expect those selected to continue running. The device supports
SleepWalking. This feature allows the peripheral to wake up from sleep based on predefined
conditions, and thus allows the CPU to wake up only when needed, e.g. when a threshold is
crossed or a result is ready. The Event System supports synchronous and asynchronous events,
allowing peripherals to receive, react to and send events even in standby mode.
The sleep mode controller is described within section 15.6.2.8 on page 124 and following.
There's the STANDBY mode an three different IDLE modes...

I think the arduino IDE does not support this modes yet (?)
I think you have to set the registers manually as described in the datasheet,
if you want to use this modes ...

there are lots of documentation from Atmel if you browse their ASF documentation (http://www.atmel.com/search.aspx?filter=0&btnG=Search&client=search_frontend&proxystylesheet=search_frontend&output=xml_no_dtd&getfields=*&oe=UTF-8&ie=UTF-8&ud=1&ulang=&sort=date:D:L:d1&wc=200&wc_mc=1&exclude_apps=1&site=en_collection&ip=10.120.2.11&access=p&entqr=3&entqrm=0&q=samd21+inmeta:asset_type%3DApplication%2520Notes&dnavs=inmeta:asset_type%3DApplication%2520Notes):
examples:
-->http://www.atmel.com/images/atmel-42411-ultra-low-power-techniques-at06549_application-note.pdf
--> http://www.atmel.com/Images/Atmel-42412-Understanding-Performance-Levels-and-Power-Domains-AT04296_Application-Note.pdf


Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 09:42 am
hello Dirk67 i know there is a sleep and wake up mode i read it in the datasheet what I'm having problem is  that i do now know how to code it to tell it to go to sleep and wake it up on a push of a button. That is what I'm having problems with at the moment.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 22, 2015, 09:53 am
Still trying to learn how to use the Mo board. so use to the uno board and mega and this is 32bit board I'm still trying.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 24, 2015, 06:16 am
Hello all I'm still stuck in writing this sleep mode for the M0 board sense it is a 32bit board I'm not advance in writing it. I really need help for someone to write this for me.  can someone please help? Thanks.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Jul 29, 2015, 08:44 am
I tried to figure this out by reading some forum pages and other websites to see if for me it is possible to put the zero into sleep mode and well it is a little over my head i can't not figure it out. I know it is there but I can not program it. I'm not that skilled of a programmer to code something like this. I really need help. Can someone please help me? Thank you.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Aug 12, 2015, 10:40 pm
Hello all a friend of mine did kind of figure out how to get the zero to go to sleep. the thing is that after uploading it does go to sleep and wake up with a push of a button no problem. But if main power is taking away and main power is turn back on it does go to sleep However  when push of the button nothing happens it doesn't want to wake up. Sketch is below.


Code: [Select]


/*

 Arduino ZERO PRO low-power sleep mode with wakeup upon external interrupt
 (example sketch)

 Add a button on digital pin 0, with an additional pull-up resistor.
 Add an LED on digital pin 3 (don't forget resistor)
 
 NOTE: LED might not appear to toggle, or it might flash, that is because of switch bounce.
 (Electrically noisy contacts)
 I think there is a filtering option to digitally filter external interrupts.
  Might check that out in the future.

*/

bool ledState = true;

void setup()
{
  Serial.begin(9600);
  pinMode(3, OUTPUT);  // Output for an LED that is toggled on/off upon interrupt
  pinMode(13, OUTPUT);  // Flashing LED pin
  
  // I could actually use the ARM macro thingies and set registers
  // and what not to do the same exact thing,
  // But I'm lazy so I just cheated and used this arduino function
  
  attachInterrupt(0, onInt, RISING);
  
  SCB->SCR |= 1<<2; // Enable deep-sleep mode
  
  // Set the EIC (External Interrupt Controller) to wake up the MCU
  // on an external interrupt from digital pin 0. (It's EIC channel 11)
  EIC->WAKEUP.reg = EIC_WAKEUP_WAKEUPEN11;
}

void loop()
{
  Serial.println("Sleeping in 3");
  toggleAndDelay();
  Serial.println("Sleeping in 2");
  toggleAndDelay();
  Serial.println("Sleeping in 1");
  toggleAndDelay();
  __WFI(); // This is the WFI (Wait For Interrupt) function call.
}

// Called upon interrupt of digital pin 0.
void onInt()
{
  ledState = !ledState;
  digitalWrite(3, ledState);
}

// This just toggles the LED on pin 13, and delays.
// Used in between the sleep countdown Serial.println()
void toggleAndDelay()
{
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}




he can not figure out why this happens or how to fix it can someone please help.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Aug 13, 2015, 11:15 pm
Hey Nick Gammon my friend got the sleep mode to work and it wakes up with a button on D0  with no problem but if main power is taking away and reapplied power to it. then it does to sleep but it will not wake up i do not know what to do or why this happens can you please help?
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: GabrielNotman on Aug 17, 2015, 10:52 am
One thing you should note is that the edge event interrupts require a specific clock and that clock is disabled in deep sleep mode. For your wake interrupts you should use HIGH or LOW which will work correctly in deep sleep mode.

When testing out sleep mode it can be helpful to put a ten second delay at the start of the sketch. This will give you time to upload a new sketch before the board enters sleep.

Also I find it useful to briefly ground the reset pin on the Serial Wire Debug header in order to reset the board when it is unresponsive [sometimes two resets in quick succession is required]. However, I'm not sure what this is triggering exactly so do this at your own risk.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: ClumsyPilot on Nov 19, 2016, 10:14 am
I just tested your code, and replaced

attachInterrupt(0, onInt, RISING);


with:

attachInterrupt(0, onInt, LOW);

In that case it appears that Arduino restarts code from the beginging, it does not call the interrupt function. Anyone has a working example of DeelSleep?
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Dec 30, 2018, 07:55 am
Hello i know is this is a old project of mine And i didn't finish it. I actually just came back to it and wanted to see If I can finish it and see where it goes.


To answer  ClumsyPilot question for some unknown reason that didn't work. I couldn't even get the Led to turn on when power is applied. But if i change 13 to LED_BUILTIN That did turn on the led blinking it before it went to sleep.

however when i change from attachInterrupt(0, onInt, RISING); to attachInterrupt(0, onInt, LOW); Nothing changed. Still wouldn't wake up.




New updated sketch based off the old one.  




Code: [Select]



/*

 Arduino ZERO PRO low-power sleep mode with wakeup upon external interrupt
 (example sketch)

 Add a button on digital pin 0, with an additional pull-up resistor.
 Add an LED on digital pin 3 (don't forget resistor)
 
 NOTE: LED might not appear to toggle, or it might flash, that is because of switch bounce.
 (Electrically noisy contacts)
 I think there is a filtering option to digitally filter external interrupts.
  Might check that out in the future.

*/

bool ledState = true;

void setup()
{
  Serial.begin(9600);
  pinMode(3, OUTPUT);  // Output for an LED that is toggled on/off upon interrupt
  pinMode(LED_BUILTIN, OUTPUT);  // Flashing LED pin
  
  // I could actually use the ARM macro thingies and set registers
  // and what not to do the same exact thing,
  // But I'm lazy so I just cheated and used this arduino function
  
  attachInterrupt(0, onInt, LOW);
  
  SCB->SCR |= 1<<2; // Enable deep-sleep mode
  
  // Set the EIC (External Interrupt Controller) to wake up the MCU
  // on an external interrupt from digital pin 0. (It's EIC channel 11)
  EIC->WAKEUP.reg = EIC_WAKEUP_WAKEUPEN11;
}

void loop()
{
  Serial.println("Sleeping in 3");
  toggleAndDelay();
  Serial.println("Sleeping in 2");
  toggleAndDelay();
  Serial.println("Sleeping in 1");
  toggleAndDelay();
  __WFI(); // This is the WFI (Wait For Interrupt) function call.
}

// Called upon interrupt of digital pin 0.
void onInt()
{
  ledState = !ledState;
  digitalWrite(3, ledState);
}

// This just toggles the LED on pin 13, and delays.
// Used in between the sleep countdown Serial.println()
void toggleAndDelay()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: MartinL on Dec 30, 2018, 12:45 pm
Hi josephchrzempiec,

The following code puts the CPU core into deep sleep and awakens it with a logic HIGH interrupt on digtial pin D12. The interrupt sets the LED on D13 high, returns to the main loop(), waits for one second, switches off the LED and enters deep sleep oncemore:

Code: [Select]
// Put the CPU core into deep sleep and awake with a logic HIGH interrupt on D12
void setup()
{
  pinMode(13, OUTPUT);                          // Set digital pin 13 to an output
  pinMode(12, INPUT_PULLDOWN);                  // Set digital pin D12 to an input with an internal pull-down resistor
  attachInterrupt(12, interrupt, HIGH);         // Set up an interrupt on digital pin D12 on a logic HIGH level
  SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;            // Set up the CPU to enter low power STANDBY mode on running the WFI() function (Wait For Interrupt)
}

void loop()
{
  delay(1000);                                  // Wait for 1 second
  digitalWrite(13, LOW);                        // Set D13 output to logic LOW
  __WFI();                                      // Put the CPU core into deep sleep
}

void interrupt()                                // Interrupt Service Routine (ISR)
{
  digitalWrite(13, HIGH);                       // Set D13 output to logic HIGH
}

Note that putting the processor core into deep sleep requires a double tap on the reset button (bootloader mode), in order to load the next sketch, as internally it stops the clock that drives the native USB port.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: josephchrzempiec on Dec 31, 2018, 06:10 am
Hello MartinL Thank you i will try it as soon as i get home.
Title: Re: Arduino Zero / M0 pro in sleep mode.
Post by: evilkos on Jul 27, 2019, 09:50 pm
The code above works, thanks! And there's also a library for Arduino Zero called ArduinoLowPower: https://www.arduinolibraries.info/libraries/arduino-low-power

It has the sleep() function defined and working perfectly for my device. Not a whole lot simpler than the code above, but has other handy functions, and also depends on an otherwise useful RTCZero lib.