Introduction: Arduino Sleep and Wakeup Test With DS3231 RTC

Picture of Arduino Sleep and Wakeup Test With DS3231 RTC

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

In my sketch I also use the Low-Power library for programming the sleep mode, that you can download from

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

- DS3231 module

Step 1: Connect All Parts

Picture of 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.



ErikA45 (author)2017-11-03

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

fabiocuomo (author)ErikA452017-11-04

You have to set arduino in sleep mode again.

ErikA45 (author)fabiocuomo2017-11-04

And how do you achieve that in your code?

fabiocuomo (author)ErikA452017-11-05

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

ErikA45 (author)2017-10-11

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

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

JamesN161 (author)ErikA452017-10-23

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.

ErikA45 (author)JamesN1612017-11-02

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.

ErikA45 (author)JamesN1612017-11-02

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

ErikA45 (author)JamesN1612017-10-23

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

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

JamesN161 (author)ErikA452017-10-23

Oops. I should have read the page more thoroughly! It's actually seconds and then minutes, not the other way around.

ErikA45 (author)JamesN1612017-10-23

Indeed, I checked the RTClibExtended.h he uses and yes: for the alarms it is seconds first. However, for setting the time it is year first. What a mess!

bypass1606 (author)2017-07-28

Hi there,

Im just wondering is there anyway that I can set multiple alarm in the code? I try to do something that trigger every 7 days.

acecombat (author)bypass16062017-08-02

When the Arduino wakes you need to set the alarm time in the RTC again, you simply add 7 days to the current time to wake it again in a week.

ErikA45 (author)acecombat2017-11-02

Wrong: when the Arduino wakes, no need to change anything; next time Arduino will wake again at 12:00 on Sunday.

bypass1606 (author)acecombat2017-09-14

Hi acecombat,

Im kindda confused in here. Can you please help me with a little example code of that? Thank you.

JamesN161 (author)bypass16062017-10-24

Hello, I know this is a bit old, but I'm here to provide the requested example code. As I told ErikA45, it appears to be this library, renamed. So according to here, you should change the

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

line to

RTC.setAlarm(ALM1_MATCH_DAY, 0, 0, 12, dowSunday); //set your wake-up time here

where you input the seconds, minutes, hours, and day of week in 24H format. So the example I provided would trigger at precisely 12:00 on every Sunday. For Monday it would be dowMonday, Tuesday dowTuesday, etc.

afl007 (author)2017-09-13

Thank you for great tutorial. My question how to set alarm to every hour of the day?

bailod (author)2017-07-25

how to change the set alarm in hour to minute or second

ŽanH1 (author)2017-06-20

Hi. While trying to do this project I run into a problem. I can't upload the code to the Arduino. I think there is a problem in the library but I am pretty new in programming so it would be helpful if anyone would help my out.


KevinM183 (author)2017-06-04

Thanks is very usefull for Low Power project

Swansong (author)2016-12-29

Great idea to save energy :)

About This Instructable




More by fabiocuomo:Arduino sleep and wakeup test with DS3231 RTC
Add instructable to: