Arduino Sleep and Wakeup Test With DS3231 RTC

18,161

20

32

I made this circuit for testing the sleep and wakeup features of Arduino using an external interrupt produced by a DS3231 real time clock with a programmed alarm. This feature is very useful when you want your Arduino project to run on batteries because sending the Arduino to sleep mode greatly reduces power consumption and extends the battery life.

For doing this I used a custom DS3231 library for Arduino (that I called RTClibExtended) that I produced modifying an existing library RTClib by Adafruit, which did not include the functionalities for programming alarms. You can download this library from https://github.com/FabioCuomo/FabioCuomo-DS3231/.

In my sketch I also use the Low-Power library for programming the sleep mode, that you can download from https://github.com/rocketscream/Low-Power/.

Parts list:

- Arduino (I used the Nano version for this project)

- A 9 Volts battery

- LM2596S Step-down voltage regulator, for reducing the power voltage to 5 Volts

http://www.ebay.it/itm/111691596889

- DS3231 module

https://www.amazon.it/dp/B01H5NAFUY/ref=pe_386201_124256341_TE_dp_1

Step 1: Connect All Parts

Connect all parts as indicated in the figure.

The Voltage regulator is a variable one: adjust the output voltage to 5 Volts before connecting the power to other components.

Step 2: Install the Libraries

Download the RTClibExtended and Low-Power libraries and install them in your Arduino library.

Step 3: The Sketch

Copy and past or download the sketch.

It programs an alarm on the DS3231 clock at a given time every day. Change the time for your test, compile the sketch and load it. The sketch is fully commented to explain what is done.

The test uses the on-board led on Arduino for evidencing the activity status.

When the sketch starts, the led should lit up for 1 second, to signal that it started correctly, than Arduino should enter the sleep mode: the led is set to OFF for indicating this.

At the programmed time, the DS3231 will produce an interrupt on the SQW pin connected to the Arduino D2 pin (which goes from the HIGH to the LOW level) which will wake up the Arduino from the sleep state, and the led should start to blink to indicate this.

You may use this feature in your projects.

Enjoy!

Share

    Recommendations

    • PCB Contest

      PCB Contest
    • Toys Contest

      Toys Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    32 Discussions

    0
    None
    Tvixen

    6 months ago

    Thanks for the Lib.... seem to work ok for me. Thumps-up from here.

    1 reply
    0
    None
    TvixenTvixen

    Reply 6 months ago

    Theres an error in LowPower if you have a Mega2560, and Compiler 1.8.x

    But that's not your fault ..but a guy called "rocketscream" who's not updating his LowPower code.

    0
    None
    PierreL14PierreL14

    Answer 10 months ago

    DateTime now = RTC.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    0
    None
    PierreL14

    10 months ago

    RTC.setAlarm(ALM1_MATCH_HOURS, 33, 18, 0); //set your wake-up time here
    isn't it 0:18:33 ? instead of 18:33

    1 reply
    0
    None
    ErikA45

    1 year ago

    How do you get this back to sleep after the wake-up?

    4 replies
    0
    None
    fabiocuomoErikA45

    Reply 1 year ago

    You have to set arduino in sleep mode again.

    0
    None
    ErikA45fabiocuomo

    Reply 1 year ago

    And how do you achieve that in your code?

    0
    None
    fabiocuomoErikA45

    Reply 1 year ago

    You can use the LowPower library and the powerDown method. You can find lots of examples on internet.

    0
    None
    PierreL14PierreL14

    Reply 10 months ago

    need to rename it as '-' are not accepted

    0
    None
    PierreL14

    10 months ago

    LowPower lib cannot be added

    0
    None
    cacaodev

    10 months ago

    Thanks for sharing this.
    Why do you connect the 5v output of the regulator to the nano Vin to and not the 5V input ? Officially, Vin accepts from 7v to 12v.

    0
    None
    ErikA45

    1 year ago

    1. how can this be set to trigger every hour?

    2. why does it not compile for Atmega168 but ok for Atmega 328?

    4 replies
    0
    None
    JamesN161ErikA45

    Reply 1 year ago

    Seeing as it appears to be very similar to the one here, I think change the

    RTC.setAlarm(ALM1_MATCH_HOURS, 33, 18, 0); //set your wake-up time here

    line to

    RTC.setAlarm(ALM1_MATCH_MINUTES, 18, 0); //set your wake-up time here

    where the 18 and 0 are minutes and seconds respectively.

    0
    None
    ErikA45JamesN161

    Reply 1 year ago

    Correction to your code:

    RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 18, 0, 0); //set your wake-up time here

    where first enter seconds, then minutes, then hours, then days. These must be entered if not you get compilation error.

    0
    None
    ErikA45JamesN161

    Reply 1 year ago

    Wrong: first seconds then minutes.

    RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 18); //set your wake-up time here

    where the 18 and 0 are minutes and seconds respectively

    0
    None
    ErikA45JamesN161

    Reply 1 year ago

    Hi, thank you for that! I will try that code.

    About not compiling using Atmega168, I found out that the library does not recognise the deep sleep function for Atmega168. This is sleep_mode_extended_standby not recognised by LowPower.h which is solved by

    https://github.com/rocketscream/Low-Power/issues/4...

    Atmega168P does use this extended standby, using above solution it does compile correctly for Atmega168P.