DS3231_Serial_Hard.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // DS3231_Serial_Hard
  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. // retrieve time- and date-data for you to manipulate.
  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. // Init a Time-data structure
  44. Time t;
  45. void setup()
  46. {
  47. // Setup Serial connection
  48. Serial.begin(115200);
  49. // Uncomment the next line if you are using an Arduino Leonardo
  50. //while (!Serial) {}
  51. // Initialize the rtc object
  52. rtc.begin();
  53. // The following lines can be uncommented to set the date and time
  54. //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
  55. //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
  56. //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
  57. }
  58. void loop()
  59. {
  60. // Get data from the DS3231
  61. t = rtc.getTime();
  62. // Send date over serial connection
  63. Serial.print("Today is the ");
  64. Serial.print(t.date, DEC);
  65. Serial.print(". day of ");
  66. Serial.print(rtc.getMonthStr());
  67. Serial.print(" in the year ");
  68. Serial.print(t.year, DEC);
  69. Serial.println(".");
  70. // Send Day-of-Week and time
  71. Serial.print("It is the ");
  72. Serial.print(t.dow, DEC);
  73. Serial.print(". day of the week (counting monday as the 1th), and it has passed ");
  74. Serial.print(t.hour, DEC);
  75. Serial.print(" hour(s), ");
  76. Serial.print(t.min, DEC);
  77. Serial.print(" minute(s) and ");
  78. Serial.print(t.sec, DEC);
  79. Serial.println(" second(s) since midnight.");
  80. // Send a divider for readability
  81. Serial.println(" - - - - - - - - - - - - - - - - - - - - -");
  82. // Wait one second before repeating :)
  83. delay (1000);
  84. }