Go Down

Topic: Arduino Zero / M0 pro in sleep mode. (Read 13231 times) previous topic - next topic

josephchrzempiec

Jul 22, 2015, 02:23 am Last Edit: Jul 22, 2015, 02:24 am by josephchrzempiec
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

nickgammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

josephchrzempiec

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?

nickgammon

No idea. I'll move this to the Zero section.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

josephchrzempiec


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?

josephchrzempiec

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

Dirk67

#7
Jul 22, 2015, 09:33 am Last Edit: Jul 22, 2015, 09:43 am by Dirk67
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:
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


arduino powered car relais / car "micro PLC" / with USB --> http://goo.gl/ofWFW3

josephchrzempiec

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.

josephchrzempiec

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.

josephchrzempiec

#10
Jul 24, 2015, 06:16 am Last Edit: Jul 24, 2015, 08:04 am by josephchrzempiec
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.

josephchrzempiec

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.

josephchrzempiec

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.

josephchrzempiec

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?

GabrielNotman

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.

Go Up