0

Adafruit feather 32u4 FONA sleep modes
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit feather 32u4 FONA sleep modes

by peakpeak on Sun Oct 08, 2017 6:44 am

I need to put Adafruit to sleep in order to conserve battery. I've searched the net for solutions with
Adafruit feather 32u4 FONA and
Adafruit RTC FeatherWing RTC module
Rumour has it that it should be possible to put the 32u4 to sleep and wake it up with a hardware interrupt from the RTC.
Anyone has any info on this?

Regards
Peter

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by adafruit_support_rick on Tue Oct 10, 2017 11:47 am

External interrupt pins on the 32U4 are 0, 1, 2, 3, and 7. 2 and 3 are used for I2C, and 7 is not brought out on the feather.

Pins 0 and 1 are used for Serial1, but you on't have to use Serial1 for anything, since the FONA is wired for Software Serial on pins 8 and 9.

I would suggest wiring the SQW/INT pad to digital 0. Set up the 32U4 to sleep until interrupted.

This sample sketch shows how to sleep pending an external interrupt. It's set for pin 2, but you can change that to pin 0.

Also note that there is a quirk of the USB serial output of the 32U4. Prints to Serial won't show up ,in Serial Monitor after the 32U4 wakes up, so don't worry if you don't see anything.
Code: Select all | TOGGLE FULL SIZE
// Demonstrates power down mode.
// Sketch will put Arduino to sleep when sleepPin is HIGH
// Arduino will wake when sleepPin goes low
// You can use any external interrupt pin for sleepPin:
// See http://arduino.cc/en/Reference/AttachInterrupt for pin
// and interrupt number information.
// Written by Rick Lesniak for Adafruit Industries

#include <avr/sleep.h>

#define ledPin 13   /* Attach a LED and resistor to this pin. LED is on when Arduino is awake, off when sleeping */
#define sleepPin 2 /*must be an external interrupt pin */

void setup()
{
  while (!Serial);
  Serial.begin(115200);
  Serial.println("starting");
  delay(100);
  pinMode(sleepPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,HIGH);   // turn LED on to indicate awake

}

void loop()
{
  if (HIGH == digitalRead(sleepPin))
  {
    Serial.println("going to sleep now");
    sleepNow();          //sleep until sleepPin goes low
  }
  delay(100);
}

void sleepNow(void)
{
    Serial.println("Going to sleep now");

    delay(100);
    // Set sleepPin as interrupt and attach handler:
    attachInterrupt(digitalPinToInterrupt(sleepPin), pinInterrupt, FALLING);
     delay(100);

    // Choose our preferred sleep mode:
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);

    // Set sleep enable (SE) bit:
    sleep_enable();

    // Put the device to sleep:
    digitalWrite(ledPin,LOW);   // turn LED off to indicate sleep
    sleep_mode();

    // Upon waking up, sketch continues from this point.
    sleep_disable();
    digitalWrite(ledPin,HIGH);   // turn LED on to indicate awake
 
    Serial.println("waking");
}

void pinInterrupt(void)
{
    detachInterrupt(digitalPinToInterrupt(sleepPin));
}

adafruit_support_rick
 
Posts: 35091
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Adafruit feather 32u4 FONA sleep modes

by peakpeak on Thu Oct 12, 2017 2:59 pm

Thanks,

I found all of that out by myself. Any tip how to get Serial.print working after sleep or at least an explanation why it does not work?

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by adafruit_support_rick on Fri Oct 13, 2017 1:31 pm

peakpeak wrote:Any tip how to get Serial.print working after sleep or at least an explanation why it does not work?
No tips. It simply doesn't work. Seems to be a USB problem

adafruit_support_rick
 
Posts: 35091
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Adafruit feather 32u4 FONA sleep modes

by peakpeak on Mon Oct 30, 2017 1:17 am

For anyone looking for an answer to this. This thread has a working solution:
http://www.rocketscream.com/blog/forums ... ial-print/

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by adafruit_support_rick on Mon Oct 30, 2017 2:43 pm

Wow! That's great!
But I wonder why detach isn't implemented? Afe you sure you need to do it?

adafruit_support_rick
 
Posts: 35091
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Adafruit feather 32u4 FONA sleep modes

by bberko on Fri Dec 22, 2017 9:05 pm

I'd am working on a similar problem but not as far along as peakpeak. I am using the similar hardware.

Adafruit feather 32u4 FONA and
Adafruit FeatherWing RTC +SD module with the INT port connected to pin 0 on the feather Fona.

I have the RTC set to send a pulse on the INT every 20 seconds using Timer A. I set up an interrupt counter program and have verified that the interrupt pulses are being sent to pin 0. I then inserted the code that adafruit_support_rick posted and changed the sleepPin to 0. When I run the program my feather Fona will go to sleep but I never see the LED come back on so I am assuming that it is not waking back up. I am probably missing something obvious. Any thoughts?

Code: Select all | TOGGLE FULL SIZE
// Demonstrates power down mode.
// Sketch will put Arduino to sleep when sleepPin is HIGH
// Arduino will wake when sleepPin goes low
// You can use any external interrupt pin for sleepPin:
// See http://arduino.cc/en/Reference/AttachInterrupt for pin
// and interrupt number information.
// Written by Rick Lesniak for Adafruit Industries
// PCF8523 timer code borrowed from https://github.com/radikalbytes/PCF8523/blob/master/PCF8523.cpp


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

// for clock and timer control
#include <PCF8523.h>   //library with Timer A countdown built in
PCF8523 rtc;
//Use pin0 as wake up pine
#define PCF8523_ADDRESS 0x68
#define PCF8523_TMR_CLKOUT_CTRL 0x0F
#define PCF8523_TMR_A_FREQ_CTRL 0x10
#define PCF8523_TMR_A_REG 0x11
#define PCF8523_CONTROL_2 0x01
#define PCF8523_CONTROL_2_CTAF_BIT 6
#define PCF8523_CONTROL_2_CTAIE_BIT 1
#define ledPin 13   /* Attach a LED and resistor to this pin. LED is on when Arduino is awake, off when sleeping */
#define sleepPin 0 /*must be an external interrupt pin */


void setup()
{
  while (!Serial);
  Serial.begin(115200);
  Serial.println("starting");
  delay(100);
  pinMode(sleepPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,HIGH);   // turn LED on to indicate awake

//from RTC timer contol
  Wire.begin();
//#else
//  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
//#endif
  rtc.begin();
if (! rtc.isrunning()) {
  Serial.println("RTC is NOT running");
  rtc.adjust(DateTime(F(__DATE__), F( __TIME__)));  //from PCF8523.h
}
  // put your setup code here, to run once:
//set up timer to count down every 60 seconds
uint8_t value = 30;
start_counter_1(value);   //sets up counter timer forinterrupts
}

void loop()
{
  if (HIGH == digitalRead(sleepPin))
  {
    Serial.println("going to sleep now");
    sleepNow();          //sleep until sleepPin goes low
  }
  delay(100);
}

void sleepNow(void)
{
    Serial.println("Going to sleep now");

    delay(100);
    // Set sleepPin as interrupt and attach handler:
    attachInterrupt(digitalPinToInterrupt(sleepPin), pinInterrupt, FALLING);
     delay(100);

    // Choose our preferred sleep mode:
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);

    // Set sleep enable (SE) bit:
    sleep_enable();

    // Put the device to sleep:
    digitalWrite(ledPin,LOW);   // turn LED off to indicate sleep
    sleep_mode();

    // Upon waking up, sketch continues from this point.
    sleep_disable();
    digitalWrite(ledPin,HIGH);   // turn LED on to indicate awake
  clear_int_flag();  //reset for next interrupt
    Serial.println("waking");

}

void pinInterrupt(void)
{
    detachInterrupt(digitalPinToInterrupt(sleepPin));
   
}
void start_counter_1(uint8_t value)
{   
    // Set timer freq at 1Hz
    Wire.beginTransmission(PCF8523_ADDRESS);
    Wire.write(PCF8523_TMR_A_FREQ_CTRL);
    Wire.write(2);
    Wire.endTransmission();
    // Load Timer value
    Wire.beginTransmission(PCF8523_ADDRESS);
    Wire.write(PCF8523_TMR_A_REG);
    Wire.write(value);
    Wire.endTransmission();
    // Set counter mode TAC[1:0] = 01
    // Disable Clockout
    Wire.beginTransmission(PCF8523_ADDRESS);
    uint8_t tmp_a;
    Wire.requestFrom(PCF8523_TMR_CLKOUT_CTRL,8);
    tmp_a = Wire.read();
    tmp_a |= (1<<7)|(1<<5)|(1<<4)|(1<<3)|(1<<1);
    tmp_a &= ~(1<<2);
    Wire.endTransmission();
    Wire.beginTransmission(PCF8523_ADDRESS);
    Wire.write(PCF8523_TMR_CLKOUT_CTRL);
    Wire.write(tmp_a);
    Wire.endTransmission();
    // Set countdown flag CTAF
    // Enable interrupt CTAIE
    Wire.beginTransmission(PCF8523_ADDRESS);
    Wire.requestFrom(PCF8523_CONTROL_2, 8);
    uint8_t tmp_b;
    tmp_b = Wire.read();
    tmp_b|=_BV(PCF8523_CONTROL_2_CTAF_BIT)|_BV(PCF8523_CONTROL_2_CTAIE_BIT);
    Wire.endTransmission();
    Wire.beginTransmission(PCF8523_ADDRESS);
    Wire.write(PCF8523_CONTROL_2);
    Wire.write(tmp_b);
    Wire.endTransmission();
}
void clear_int_flag()
{   //clear out PCF8523_CONTROL_2 to lower AF bit and allow next interrupt
  Wire.beginTransmission(PCF8523_ADDRESS);
  Wire.requestFrom(PCF8523_CONTROL_2,4);  //read bits 0-3, bit 3 is AF
  uint8_t tmp2;
  tmp2 = Wire.read();
  tmp2 &= 7;   //set AF to 0, 7 = 0B0111
  Wire.write(PCF8523_CONTROL_2);
  Wire.write(tmp2);
  Wire.endTransmission();
 
}

bberko
 
Posts: 22
Joined: Thu Mar 31, 2016 8:02 pm

Re: Adafruit feather 32u4 FONA sleep modes

by peakpeak on Wed Dec 27, 2017 2:51 am

I suggest you use a library to check if everything is working ok with it.
https://learn.adafruit.com/ds1307-real- ... no-library
This library works with the PCF8523 as well.
If everything is working you can revert to your code step by step until you find the part that is failing.

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by bberko on Wed Dec 27, 2017 12:20 pm

I don't think the RTC is the problem. If I comment out the sleep portion and set up a variable to count pulses on Pin 0 I can see the interrupts coming in as planned. I think my issue is the "wakeup" portion of the sleep mode.

On another note, I don't think the RTC library supports the interrupt pin on the PCF8523, at least in my looking through the github files (and limited experience in library functionality). That is why I am using PCF8523.h instead.

Any thoughts on the wakeup errors?

bberko
 
Posts: 22
Joined: Thu Mar 31, 2016 8:02 pm

Re: Adafruit feather 32u4 FONA sleep modes

by peakpeak on Wed Dec 27, 2017 12:41 pm

I don't think the RTC is the problem
Ok, tried to help you, but obviously you are the expert here

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by bberko on Thu Dec 28, 2017 9:23 pm

I can't tell if the last post was sarcastic or not. I was not trying to be dismissive of your suggestions and I am sorry if it came off that way. I dug through RTClib.h and RTClib.cpp again and did find functionality for Pcf8523SqwPinMode but I am unsure if it can go longer than 1 Hz and I am looking for 1 minute as I want the feather fona to sleep for more than a second. I do not see anything setting up the timers but again, I am a relative newb especially when it comes to libraries so maybe I am missing something. Is there an rtc.interrupt_timerA(); or similar function that I missed?

bberko
 
Posts: 22
Joined: Thu Mar 31, 2016 8:02 pm

Re: Adafruit feather 32u4 FONA sleep modes

by peakpeak on Mon Jan 01, 2018 8:37 am

Manually activate the interrupt. Does the Fona awake?
Oscilloscope on pin 0?
What have you done to solve this?

peakpeak
 
Posts: 21
Joined: Sun Oct 08, 2017 6:37 am

Re: Adafruit feather 32u4 FONA sleep modes

by adafruit_support_rick on Mon Jan 08, 2018 11:43 am

You have a pullup on pin 0. The sketch will sleep whenever pin 0 is high. So, your interrupt pulse goes low, the processor wakes up, and the interrupt is reset. Pin 0 goes high again, and you immediately go back to sleep. The LED probably does light up, but it's much too brief to see.

adafruit_support_rick
 
Posts: 35091
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Adafruit feather 32u4 FONA sleep modes

by bberko on Sat Jan 20, 2018 9:33 am

That did it! Thanks for the assistance.I knew I was missing something.

bberko
 
Posts: 22
Joined: Thu Mar 31, 2016 8:02 pm

Please be positive and constructive with your questions and comments.