Arduino Zero / M0 pro in sleep mode.

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.

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?

No idea. I'll move this to the Zero section.

okay no problem thank you.

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?

Well sense the Due is closer to the M0 pro what aobut putting that to sleep or power down mode?

josephchrzempiec:
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

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:
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

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.

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.

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.

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.

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.

/*

 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.

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?

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.

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?

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.

/*

 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);
}

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:

// 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.

Hello MartinL Thank you i will try it as soon as i get home.

The code above works, thanks! And there's also a library for Arduino Zero called ArduinoLowPower: Arduino Low Power - Arduino Libraries

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.