# Use a RTC to wake up an ESP8266

I'm designing an alarm clock with an ESP8266 (Nodemcu) and a RTC module (DS3231). My system doesn't have any battery, it will be constantly powered through USB. I'd like to keep my ESP866 in deepsleep and waking it up at a specific time thanks to the RTC.

I first created a software alarm with the TimeAlarms library and it works fine. Now I'm trying to do the same thing directly with the hardware. My idea is that the RTC would send an impulse to the ESP8266 at a specific time set in one of the two alarms available on the DS3231 that would wake it up.

I'm using RCTlib.h, the RTC library from Adafruit that works perfectly but there isn't any example of alarm setting.

How do I use an RTC to wake up an ESP8266?

• Dig through the Datasheet, you just have to access the alarm registers using I2C or TWI bus – MaNyYaCk Feb 11 '17 at 14:22

I'm not sure of the logic of your design.
If your Alarm clock has no battery then obviously it won't work if not plugged into a USB port (be that a USB power supply or a computer port).
(I hope in your design your are allowing for the fact the NodeMCU is 3.3 V only)

So unplugged it is OFF, plugged in ....why does the ESP8266 ever need to sleep? What advantage do you gain having the MCU go to sleep when power is available?

If you want to implement one of the deep sleep modes, there are dozens of sample code projects (Arduino and NodeMCU) where you use an internal timer (or a pin change interrupt) in the MCU to wake from deep sleep after a timed period (set by the MCU).
In your case you could simply have the MCU power up say every 55 seconds and compare an alarm value kept in the MCU with the RTC time from the DS3231. If the time is not close to the alarm time (by more than 1 minute) then reactivate the timer and go back to sleep. You can leave the DS3231 programmed to a fixed reference time zone in 24 hr mode and do everything else in the MCU.

This avoids having to change (add to) the RTCLib to allow setting the alarm registers in the external DS 3231. There is little difference between using SQW to trigger a Pin change to wake up the MCU and using an MCU timer to wake the MCU. Side Note: The RTClib already supports setting the SQW to 1 Hz, so another way to implement would be to input this to a timer in the MCU and set a wake on timer (number of seconds) as the alarm.

If you keep the alarms in the MCU then you have the potential to create multiple, complex day (12/24hr) and time zone relationship alarms easier (in terms of software) than if you try to program only the two DS3231 alarm registers.

If you want to truly write your own code, then use the Wire library directly to address the DS3231 registers and simply fork the Adafruit libray, adding your functionality to the library.

I am assuming you have access to the INT/SQW pin on your real time clock. This pin can be set to output a high signal when the RTC time matches the time set in some of the other RTC registers. I cannot tell if this is included in the Adafruit Library, as I still have some trouble understanding C.

You might need to implement this without a library (excluding the Wire.h library). This website has a good guide to setting the time on the RTC, so you should begin to understand basic I2C.

This is the datasheet for the DS3231, so you can see what registers you need to send the messages to to program the chip.