User Tools

Site Tools


amc2020:group_n:ds3231rtc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
amc2020:group_n:ds3231rtc [2020/07/26 12:12] – [4.2 The Code Explained] jonas001amc2020:group_n:ds3231rtc [2021/08/24 17:35] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +<html>
 +  <left>
 +    <a href="https://wiki.eolab.de/doku.php?id=amc2020:group_n:start"; onmouseover="style.color='green'";>
 +      <span style="color:#2E71B8"; onmouseover="style.color='green'"; onmouseout="style.color='#2E71B8'";>
 +        &#8617 Back to the main page
 +      </span>
 +    </a>
 +  </left>
 +</html>
 +
 ====== DS3231 Real Time Clock ====== ====== DS3231 Real Time Clock ======
  
Line 86: Line 96:
 There are a number of libraries available for the DS3231 RTC module, multiple of which were tested out. However, most of them are rather poorly documented, most have different features and some of them did not include the necessary function for the scope of the project. There are a number of libraries available for the DS3231 RTC module, multiple of which were tested out. However, most of them are rather poorly documented, most have different features and some of them did not include the necessary function for the scope of the project.
  
-In the end, after being inspired by the approach of {{https://github.com/RalphBacon/DS1307-DS3231-Real-Time-Clocks/blob/master/DS1307_DS3231_Reference_Code.ino|Ralph Bacon}}, I programmed the DS3231 only using the <html><span><font face="Courier New"><font style="color:#CD5307"><b>Wire</b></font>.h</font></span></html> library for I2C communication without a dedicated DS3231 library.+In the end, after being inspired by the approach of {{https://github.com/RalphBacon/DS1307-DS3231-Real-Time-Clocks/blob/master/DS1307_DS3231_Reference_Code.ino|Ralph Bacon}}, I programmed the DS3231 only using the <html><span><font face="Courier New"><font style="color:#CD5307"><b>Wire</font>.h</font></b></span></html> library for I2C communication without a dedicated DS3231 library.
  
 The sketch by Ralph Bacon only included setting and reading the time from the module and printing the result to the serial monitor. Most of this was used with only minor changes in this sketch. The implementation of the interrupt and the setting and clearing of alarm 1 was done by me. The sketch by Ralph Bacon only included setting and reading the time from the module and printing the result to the serial monitor. Most of this was used with only minor changes in this sketch. The implementation of the interrupt and the setting and clearing of alarm 1 was done by me.
 +
 +<WRAP center round info 100%>
 +For anyone interested, I also made my own library for the DS3231 RTC module. The library allows setting and reading the time as well as using alarm 1 and 2 and also allows to read and change the status and control registers. There are also functions included for directly printing the time and the register settings to the serial monitor for an easy setup.
 +
 +The library can be downloaded as a zip file from {{ :amc2020:group_n:ds3231lib.zip |here}}. 
 +
 +To understand what is done by the functions, I recommend checking the cpp file in the library folder and the [[https://datasheets.maximintegrated.com/en/ds/DS3231.pdf|DS3231 Datasheet]]. Example sketches will soon be included as well.
 +</WRAP>
 +
 ==== 4.1 The Code ==== ==== 4.1 The Code ====
  
Line 247: Line 266:
   Serial.print(Year);   Serial.print(Year);
   Serial.print("   Day of the week: ");   Serial.print("   Day of the week: ");
-  switch(Day){                                    //37+  switch(Day){                                    
     case 1:     case 1:
       Serial.println("Monday");       Serial.println("Monday");
Line 405: Line 424:
     </ol>     </ol>
     </font>     </font>
 +</html>
 +
 +==== 4.3 Results ====
 +
 +When uploading the sketch to the Arduino after connecting it according to the setup, the time of the module is set to Friday, 24th of July 2020, 10:15:00 which can be checked by calling the <html><b><span><font face="Courier New">displaySerialTime()</font></span></b></html> function. The alarm register is set to 12:30:00 and day/date 12 is chosen. Here it only makes sense if the alarm is not configured to match the days because a week does not have 12 days.
 +
 +In the example the setting <html><b><span><font face="Courier New">ALARM_ONCE_PER_SECOND</font></span></b></html> is chosen which means that non of the alarm registers are matched to the time registers, it just gives a signal every second which can be seen by the LED switching on and off in 1 second intervals.
 +
 +The 1 second intervals are of course only chosen to check whether the alarm works properly. As said, the ESP32 is to wake up once an hour, take measurements, transmit the data and go back to sleep afterwards. Therefore, for the final application, the setting <html><b><span><font face="Courier New">ALARM_SECONDS_MINUTES_MATCH </font></span></b></html>  will be chosen. The minutes and seconds from the alarm register match exactly once per hour, so the alarm rate is correct.
 +
 +Furthermore, the time in the alarm register will be set to 0 minutes and 0 seconds, such that the interrupt occurs with the beginning of every new hour, if the time registers are configured correctly.
 +
 +After programming the alarm and time registers, most of the code will not be needed anymore and can be left out of the final sketch. The ESP32 will only need the I2C library, an interrupt service routine to wake it up, and the <html><b><span><font face="Courier New">clearAlarm1()</font></span></b></html> function to reset the alarm flag.
 +
 +<html>
 +  <center>
 +    <span>
 +    <a href="javascript:self.scrollTo(0,0)"; onmouseover="style.color='green'";>
 +      <span style="color:#2E71B8"; onmouseover="style.color='green'"; onmouseout="style.color='#2E71B8'";>
 +        Back to the top &#10548
 +      </span>
 +    </a>
 +    </span>
 +  </center>
 </html> </html>
amc2020/group_n/ds3231rtc.txt · Last modified: 2021/08/24 17:35 by 127.0.0.1