DS3231_UnixTime.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // DS3231_UnixTime
  2. // Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  3. // web: http://www.RinkyDinkElectronics.com/
  4. //
  5. // A quick demo of how to use my DS3231-library to
  6. // convert date and time to UnixTime
  7. //
  8. // To use the hardware I2C (TWI) interface of the Arduino you must connect
  9. // the pins as follows:
  10. //
  11. // Arduino Uno/2009:
  12. // ----------------------
  13. // DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin
  14. // SCL pin -> Arduino Analog 5 or the dedicated SCL pin
  15. //
  16. // Arduino Leonardo:
  17. // ----------------------
  18. // DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin
  19. // SCL pin -> Arduino Digital 3 or the dedicated SCL pin
  20. //
  21. // Arduino Mega:
  22. // ----------------------
  23. // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin
  24. // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin
  25. //
  26. // Arduino Due:
  27. // ----------------------
  28. // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
  29. // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
  30. //
  31. // The internal pull-up resistors will be activated when using the
  32. // hardware I2C interfaces.
  33. //
  34. // You can connect the DS3231 to any available pin but if you use any
  35. // other than what is described above the library will fall back to
  36. // a software-based, TWI-like protocol which will require exclusive access
  37. // to the pins used, and you will also have to use appropriate, external
  38. // pull-up resistors on the data and clock signals.
  39. //
  40. #include <DS3231.h>
  41. // Init the DS3231 using the hardware interface
  42. DS3231 rtc(SDA, SCL);
  43. Time t;
  44. void setup()
  45. {
  46. // Setup Serial connection
  47. Serial.begin(115200);
  48. // Uncomment the next line if you are using an Arduino Leonardo
  49. //while (!Serial) {}
  50. // Initialize the rtc object
  51. rtc.begin();
  52. }
  53. void loop()
  54. {
  55. // Send Current time
  56. Serial.print("Current Time.............................: ");
  57. Serial.print(rtc.getDOWStr());
  58. Serial.print(" ");
  59. Serial.print(rtc.getDateStr());
  60. Serial.print(" -- ");
  61. Serial.println(rtc.getTimeStr());
  62. // Send Unixtime
  63. // ** Note that there may be a one second difference between the current time **
  64. // ** and current unixtime show if the second changes between the two calls **
  65. Serial.print("Current Unixtime.........................: ");
  66. Serial.println(rtc.getUnixTime(rtc.getTime()));
  67. // Send Unixtime for 00:00:00 on January 1th 2014
  68. Serial.print("Unixtime for 00:00:00 on January 1th 2014: ");
  69. t.hour = 0;
  70. t.min = 0;
  71. t.sec = 0;
  72. t.year = 2014;
  73. t.mon = 1;
  74. t.date = 1;
  75. Serial.println(rtc.getUnixTime(t));
  76. // Wait indefinitely
  77. while (1) {};
  78. }