Hi,
I'm using the trick proposed by ShermanP to reduce power consumption of a battery powered project.
For those whose do not know the trick, it consists to cut the wire between the SQW pin and the pullup resistor; and then it is possible to power the RTC module only when you want to communicate with it.
I did everything adviced on the post (october 2020 - sorry I don't know how-to re add a reference to this post) but once I stop the power of the RTC, it will not wakeup.
If I do not stop the power (by calling "return;" at the very first line of the rtcPowerOff function, the program works correctly..
I checked with ohm-meter and the wire between the SQW pin and the resistor is cut
Below is the program (testing code)
any idea what you have a look at ? or did I do an error ?
thanks
Vincent
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Wire.h> // https://arduino.cc/en/Reference/Wire
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
#include <Wire.h>
#include <LowPower.h>
#include <avr/power.h>
#include "RTClib.h"
#include "DS3231Functions.h"
// LEDs and switchs
const byte ledPin1 = LED_BUILTIN ;
const byte buttonPin1 = 12;
// Booleans for input states
volatile bool D12_state = LOW;
volatile bool rtcAlarm {false}; // ISR flag
DS3232RTC myRTC;
constexpr uint32_t ledInterval {10}; // seconds to leave the LED on after an alarm
// set current date time to RTC
// year is assumed to be tenths only (i.e. 20 means 2020)
const uint8_t do_rtcPower=6;
void rtcPowerOn()
{
pinMode(do_rtcPower, OUTPUT);
delay(100);
digitalWrite(do_rtcPower,HIGH);
delay(100);
myRTC.begin();//Wire.begin();
}
void rtcPowerOff()
{
//return;
Wire.end();
digitalWrite(do_rtcPower,LOW);
delay(100);
pinMode(do_rtcPower, INPUT); // high-z, turn off power to the rtc
}
void setup()
{
Serial.begin(115200);
Serial << F("> Setup()\n");
// Set LEDs as outputs
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1,LOW);
// Set switches as inputs with pullups
pinMode(buttonPin1, INPUT_PULLUP);
// Enable PCIE2 Bit0 = 1 (Port B) & Bit2 (Port D)
PCICR |= B00000001;
// Enable interrupt on D12 (D12-PB4-PCINT4)
PCMSK0 |= B00010000;
rtcPowerOn();
Serial << F("Sketch start ");
printTime(myRTC.get());
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
myRTC.setAlarm(DS3232RTC::ALM1_MATCH_DATE, 0, 0, 0, 1);
myRTC.alarm(DS3232RTC::ALARM_1);
myRTC.alarmInterrupt(DS3232RTC::ALARM_1, false);
myRTC.squareWave(DS3232RTC::SQWAVE_NONE); // assert INT/SQW pin on alarm
Serial << F("now= "); printTime(myRTC.get());
Serial << F("< Setup()\n");
rtcPowerOff();
}
const uint8_t wakeInterval=10;
void loop()
{
rtcPowerOn();
time_t wake = myRTC.get()+wakeInterval;
Serial << F("Setting alarm for ");
printTime(wake);
myRTC.setAlarm(DS3232RTC::ALM1_MATCH_DATE, second(wake), minute(wake), hour(wake), day(wake));
myRTC.alarmInterrupt(DS3232RTC::ALARM_1, true);
Serial << F("Going to sleep at ");
printTime(myRTC.get()); Serial.flush();
// interrupts();
rtcPowerOff();
goToSleep();
//noInterrupts();
Serial << F("Alarm at ");
rtcPowerOn();
printTime(myRTC.get());
myRTC.alarm(DS3232RTC::ALARM_1); // clear alarm flag
myRTC.alarmInterrupt(DS3232RTC::ALARM_1, false);
}
// RTC interrupt handler
ISR (PCINT0_vect)
{
rtcAlarm= (digitalRead(buttonPin1)==LOW);
}
// format and print a time_t value
void printTime(time_t t)
{
char buf[25];
memset(buf,0,25);
sprintf(buf, "%.4d.%.2d.%.2d-%.2d:%.2d:%.2d",
year(t),month(t), day(t) ,hour(t), minute(t), second(t));
Serial.println(buf);
}
void goToSleep()
{
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
power_all_enable();
}