DS3231 Won't Work On Battery Power [SOLVED]

Hey guys

I’m trying to make an automated dog feeder, and use the ds3231 to sleep the arduino in between feeding, to save as much power as possible.

I successfully managed to make this work while plugging the Vcc of the ds3231 to the arduino 5v. However, i realised that i could save more power if i plugged the vcc into an arduino pin (I did A4) and then manually turn off the ds3231 during the sleep (I put a coin cell battery in it). I managed to find this code online (online the relevant part is here, see attachment for full code):

void loop() { 
       // The alarms are set to wake the arduino every 30 seconds

      interrupt_state = digitalRead(wakeupPin);
     Serial.print(interrupt_state);
     Serial.print("\n");
     
     RTC.alarm(ALARM_1) ; 
     RTC.alarm(ALARM_2) ; 
     delay(50);
     
     attachInterrupt(digitalPinToInterrupt(wakeupPin), wakeUp, FALLING); 
     interrupt_state = digitalRead(wakeupPin);
     Serial.print(interrupt_state);
     Serial.print("\n");
     Serial.print("Sleeping now");
     Serial.print("\n");
     delay(50);

      pinMode(Clock_Power, LOW);
      digitalWrite(Clock_Power, LOW);
      
     
     
     LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

      digitalWrite(Clock_Power, HIGH);
      pinMode(Clock_Power, OUTPUT);
      interrupt_state = digitalRead(wakeupPin); //This should read 0
     Serial.print(interrupt_state);
      Serial.print("Waking up");
      Serial.print("\n");
     

     detachInterrupt(digitalPinToInterrupt(wakeupPin));
     tmElements_t tm;
RTC.read(tm);
Serial.print(tm.Hour, DEC);
Serial.print(':');
Serial.print(tm.Minute,DEC);
Serial.print(':');
Serial.println(tm.Second,DEC);
Serial.print("\n");
    
     playTune();

     

}

I used the interrupt state to read the alarm state of the ds3231, and when i used the old code (which works) with the vcc in the 5v, the alarm would go to 0 (as it should) just as the arduino wakes up. However, when i power using A4, rather than wake up every 30 seconds as it should, the arduino will constantly wake up as soon as function playTune() ends, and basically all I’m left with is a constant loop of the function playTune(), without the arduino sleeping. When I checked the alarm value, it constantly remains at 1, never dropping to 0 (which is when the arduino should wake). This means the arduino should indefinitely sleep, as the alarm is set to trigger on 0 (I am using the arudino pull up resistor) I even experiment by changing the attach interrupt FALLING to HIGH and LOW, and it weirdly makes no difference.

I checked online and some people reported this exact same problem (except there alarm value was constantly 0, which makes sense). I followed there advice and desoldered resistor pack 1, but now this code doesn’t even work anymore, and the arduino just sleeps forever.

I would greatly appreciate some advice :slight_smile: (sorry for the word wall and if i missed anything, first time using the forum and I’m not too experienced with arduino)

Playing_hes_a_pirate.ino (8.46 KB)

Post a link to the DS3231 module you have and describe exactly what you did to it. Post a clear, closeup picture of the modified board.

If you have the one described in this extensive tutorial, there are several modifications you need to make, and you must make sure that there are pullup resistors on the I2C and interrupt lines, to Arduino Vcc.

Hello Jremington,
Thank you for your reply. This is the board I am using:

I removed the charging resistor (I am using a non-rechargeable battery and according to google I’m suppose to stop the charging circuit) as soon as I got the board, which didn’t seem to affect its performance in any way, but it stopped working when I removed resistor pack 1 (See image attached)

I haven’t attached any resistors to my circuit, so the only pullup resistor I believe I am using is the one on the SQW (the interrupt pin). Not fully sure which one the 12C lines are, as am I quite unfamiliar with the rtc and how it operates.

I checked the site you linked and found that the guy connected the rtc vcc directly to the arduino’s power supply. I not sure if I am correct, but I was under the impression that even in sleep, the arduino will continue to supply power anyway. Is my assumption wrong, or will I need to connect the rtc to a pin and manually power it off to save more power?

If the RTC is connected to the Arduino Vcc, it will draw power when the Arduino is sleeping. Remove that connection and the RTC will run on battery power only.

The I2C lines are SDA and SCL, which are used to communicate with the RTC.

If your Arduino board does not have 2.2K to 4.7K resistors connected between SDA/SCL and Vcc (Pro Mini and others), you need to add them. Otherwise communications won't work at all.

Hint on posting links This is all you need: 1PCS DS3231 AT24C32 IIC Precision RTC Real Time Clock Memory Module DS3231SN AT24C32 Mini Real Time Memory Module for Arduino|rtc module|rtc ds3231arduino rtc - AliExpress

All the rest tracks your personal internet activity, so remove it.

I checked the site you linked and found that the guy connected the rtc vcc directly to the arduino's power supply. I not sure if I am correct, but I was under the impression that even in sleep, the arduino will continue to supply power anyway. Is my assumption wrong, or will I need to connect the rtc to a pin and manually power it off to save more power?

The linked article is very extensive, and you need to read it closely and all the way through. He certainly does pin power the RTC.

Addendum 2015-04-07

That last deployment saw several loggers run successfully with pin powered RTC’s so I though I should post the little code snippet I use to do that.

I am using an arduino UNO, and before I desoldered resistor pack 1 (like the website you linked showed), I could communicate fine with the clock, and the interrupts worked fine too (I am not using any resistors).
It's only when I tried to power of the rtc while arduino sleeps that the interrupts wouldn't function properly and the arduino would basically wake instantly from sleep no matter what.

However, once I removed the resistor pack 1, the arduino just sleeps indefinitely, and behaves quite strangely. I run a print code right at the start of the function:

Serial.begin(115200);

    Serial.print("Alive");
    Serial.print("\n");

I even ran a 10 second delay at the end of the setup, and despite the fact that the arduino should sleep in the loop, not the setup, it will instantly go to sleep (I am using the 4 arduino leds to see this) and only print "Aliv" (like its being cut-off due to the sleep). I can read and modify the time on the rtc easily with no problem (which is why I think the problem may not be related to the SDA/SCL pins, but I got could be wrong).

Basically I'm really lost as to why even the arduino board behaves strangely even though I never modified it. I did order a new RTC (same one) so I will try adding resistors to it, but I don't understand why my arduino is behaving so strangely even though I followed what the site you linked said.

Sorry if this is frustrating for you guys

Post ALL your code. Snippets are useless.

What, exactly, is supposed to wake up the Arduino?

What, exactly, do you mean by "the Arduino board behaves strangely"?

Post a clear, closeup picture of your modified RTC board. You may have damaged it, shorted tracks, etc.

Here is the full code I am using:

//Import libraries
#include <Streaming.h>
#include <DS3232RTC.h>
#include <LowPower.h>
#include <Wire.h>
#include <Streaming.h>


//Set up pins
const int b = A0;
const int wakeupPin = 2 ;
const int Clock_Power = 4;

//Define variables
volatile int woken = 0 ;
int interrupt_state = 0;

//Define functions
void wakeUp()
{
    woken = 1 ;
}

void playTune() {
   tone(b,440.00,250);
  delay(150);
  tone(b,523.25,250);
  delay(150);
  tone(b,587.33,250);
  delay(300);
  tone(b,587.33,250);
  delay(300);
  tone(b,587.33,250);
  delay(150);
  tone(b,659.25,250);
  delay(150);
  tone(b,698.46,250);
 
 
  
}


void setup() {
    Serial.begin(115200);

    Serial.print("Alive");
    Serial.print("\n");
    *//Attach all pins
     pinMode(b, OUTPUT); //Buzzer
     pinMode(wakeupPin, INPUT_PULLUP);
     pinMode(LED_BUILTIN, OUTPUT);
     pinMode(Clock_Power, OUTPUT);
     digitalWrite(Clock_Power, HIGH);
   

    //Alarm stuff
    Wire.setClock(400000); 
    
    RTC.setAlarm(ALM1_MATCH_HOURS, 0, 0, 17, 0);
    RTC.setAlarm(ALM2_MATCH_HOURS, 0, 15, 17, 0);
    
    RTC.setAlarm(ALM1_MATCH_SECONDS, 30, 0, 0, 0);
    RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0);
    
    RTC.squareWave(SQWAVE_NONE);
    
    RTC.alarmInterrupt(ALARM_1, true);
    RTC.alarmInterrupt(ALARM_2, true);
    tmElements_t tm;
    RTC.read(tm);
  Serial.print(tm.Hour, DEC);
Serial.print(':');
Serial.print(tm.Minute,DEC);
Serial.print(':');
Serial.println(tm.Second,DEC);
Serial.print("\n");
Serial.print("\n") ;
        Serial.print("Time displayed");
        Serial.print("\n");
        delay(10000);
}
 



void loop() { 
     
      interrupt_state = digitalRead(wakeupPin);
     Serial.print(interrupt_state);
     Serial.print("\n");
     
     RTC.alarm(ALARM_1) ; 
     RTC.alarm(ALARM_2) ; 
     delay(50);
     
     attachInterrupt(digitalPinToInterrupt(wakeupPin), wakeUp, FALLING); //instead of FALLING
     interrupt_state = digitalRead(wakeupPin);
     Serial.print(interrupt_state);
     Serial.print("\n");
     Serial.print("Sleeping now");
     Serial.print("\n");
     delay(50);

      pinMode(Clock_Power, LOW);
      digitalWrite(Clock_Power, LOW);
      
     
     
     //   LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

      digitalWrite(Clock_Power, HIGH);
      pinMode(Clock_Power, OUTPUT);
      interrupt_state = digitalRead(wakeupPin);
     Serial.print(interrupt_state);
      Serial.print("Waking up");
      Serial.print("\n");
     

     detachInterrupt(digitalPinToInterrupt(wakeupPin));
     tmElements_t tm;
RTC.read(tm);
Serial.print(tm.Hour, DEC);
Serial.print(':');
Serial.print(tm.Minute,DEC);
Serial.print(':');
Serial.println(tm.Second,DEC);
Serial.print("\n");
    
     playTune();

     

}

It worked fine on vcc before I removed resistor pack 1, but when I removed resistor pack 1, the arduino will sleep and never wake. However, it behaves strangely because in the setup code I am printing "Alive" and printing the time. The arduino will only print "Aliv", it will not print the time, and even though I entered a 10 second delay, it goes to sleep instantly. It appears to me as if the arduino will go to sleep before it runs the full setup code, which is why it misses out some of the text it should print. Here is how I modded the rtc (I didn't mod the arduino in any way)

It looks like you have solder bridges on the modified RTC board where the resistor pack was, possibly creating a short circuit to ground. Remove all excess solder from those pads, get out your multimeter and check all connections.

If there is a short and you are trying to power the RTC from a port pin, stop doing that. You will damage the Arduino (you may already have damaged the output pin).

Remove these lines until the problem is fixed:

  pinMode(Clock_Power, OUTPUT);
  digitalWrite(Clock_Power, HIGH);

Why do you use Serial.print("\n") instead of Serial.println()?

Thanks for the help, I will definitely do that.
One of the codes I found online used Serial.print("/n"), and since I didn't know about Serial.printIn(), I just used that.

Ok so I removed the excess solder and checked it with a multimeter. Unfortunately it still had the same exact problem.
However, I was able to pinpoint the problem to the line "Wire.setClock(400000) ;". I deleted this and now everything works fine. I have no idea why this line was messing up the arduino, but at least it works now! :slight_smile:

Thanks for all the help, I really appreciate it! I

I just have one final question. I noticed that I don't even need to power the RTC using an arduino pin and then manually powering the clock down when the RTC sleeps, like other posts I saw said. Instead the RTC works without the VCC being plugged in at all, as long as the ground is still connected to the arduino. Is it suppose to work like this/can I build my project like this without any problems?

Once again, thanks for all the time and help you guys put in! :slight_smile:

Edit: On that note, should I change my first post to show the answer? That way anyone who stumbles to this forum with the same problem as I had can easily see the solution?

Is it suppose to work like this/can I build my project like this without any problems?

Presumably it is running off the battery, as designed. The battery will need to be replaced after a couple of years.

You can edit the title of your original post to add [SOLVED]. Glad you got it working!