Arduino Project: Real time clock with DS3231 module

Home/Arduino Projects, Design, Robot Projects/Arduino Project: Real time clock with DS3231 module

Arduino Project: Real time clock with DS3231 module

Arduino Tutorial: Real Time clock with DS3231 module

Arduino Project: Real time clock (RTC) and temperature monitor using the DS3231 module.

In this video we build a Real Time Clock using the DS3231 chip.

——————–
DS3231 Real Time Clock Parts  – WHERE TO BUY
——————–

1. Arduino Mega: http://bit.ly/ArduinoMega

2. DS3231 RTC: http://bit.ly/Ds3231

3. Keypad display: http://bit.ly/ArduinoKeypadDisplay

4. Small breadboard: http://bit.ly/SmallBreadboard

5.Wires: http://bit.ly/WiresArduino

——————–
LIBRARIES
——————–

DS3231 Library: https://github.com/rodan/ds3231

——————–
CODE OF THE PROJECT
——————–

DOWNLOAD THE CODE

 

 

——————–
SUBSCRIBE ON YOUTUBE
——————–

Never miss a video: Subscribe to educ8s.tv

2017-03-31T12:45:54+00:00

28 Comments

  1. Sattva November 18, 2015 at 11:44 am

    Thank you. Nice lesson!

  2. Ayinde November 24, 2015 at 1:14 am

    Very good and explanatory lesson. However, I have difficulty downloading the code. I cannot open the .rar file. Could you please suggest how to open the file? Thank you.

    • Nick November 24, 2015 at 7:47 am

      Thank you for your comment. In order to open the file you have to use the WinRar software.

  3. Marcin December 29, 2015 at 3:07 pm

    I’ve tried to use rodan’s library and it worked – data has been shown in the serial monitor. Unfortunately using your code project all I see is weird signs … After copying code from rodan’s example that shows data from the structure in the serial monitor I found out it shows the right data – structure keep right data. Only LCD doesn’t work properly.
    I’ve changed only pins that I have connected to my LCD to Arduino Leonardo (7,6,5,4,3,2) and that’s it.

    LCD is fine – other of my projects works well. Do you possibly know why I have such a problem?

    • dazza April 22, 2016 at 12:25 pm

      Hi just seen your question, some examples have different baud rates set, have you set your serial baud rate in your code to match that in the serial monitor( Serial.begin(9600); 9600 baud, bottom right in the serial monitor).

  4. Marc Perschke January 27, 2016 at 12:37 pm

    How can i Change The Date

  5. dimitrisd February 11, 2016 at 10:04 pm

    Bravo very good job, i have an error i can find why, i get it when i try to program arduino (arduino nano), i didnt change anything from the code, this is the mesage
    “C:\Users\jim\Documents\ARDUINO\rtc\rtc.ino: In function ‘void parse_cmd(char*, int)’:

    rtc:136: error: cannot convert ‘boolean* {aka bool*}’ to ‘const uint8_t* {aka const unsigned char*}’ for argument ‘5’ to ‘void DS3231_set_a1(uint8_t, uint8_t, uint8_t, uint8_t, const uint8_t*)’

    DS3231_set_a1(time[0], time[1], time[2], time[3], flags);”

  6. bouar February 23, 2016 at 10:19 pm

    Arduino : 1.6.4 (Windows 8.1), Carte : “Arduino Uno”

    rtc_ds3231-lcd.ino: In function ‘void parse_cmd(char*, int)’:
    rtc_ds3231-lcd:150: error: cannot convert ‘boolean* {aka bool*}’ to ‘const uint8_t* {aka const unsigned char*}’ for argument ‘5’ to ‘void DS3231_set_a1(uint8_t, uint8_t, uint8_t, uint8_t, const uint8_t*)’
    cannot convert ‘boolean* {aka bool*}’ to ‘const uint8_t* {aka const unsigned char*}’ for argument ‘5’ to ‘void DS3231_set_a1(uint8_t, uint8_t, uint8_t, uint8_t, const uint8_t*)’

    I can’t compile with arduino UNO.
    so i made // before line : DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
    and before line : DS3231_set_a2(time[0], time[1], time[2], flags);
    Can you have time to read.

    Merci.

  7. bouar February 23, 2016 at 10:21 pm

    When i desactive the two linz i can compile but the DATE in the LCD IS 1 january 1900, the temperature is correct.

    Merci

  8. George March 24, 2016 at 12:14 pm

    There is a problem displaying day of week.
    “t.wday = inp2toi(cmd, 7);” should be “t.wday = cmd[7] – 48;

    For other interested add:
    “printDay(t.wday);” in the date section

    and

    “void printDay(int day)
    {
    switch(day)
    {
    case 1: lcd.print(“Sun “);break;
    case 2: lcd.print(“Mon “);break;
    case 3: lcd.print(“Tue “);break;
    case 4: lcd.print(“Wed “);break;
    case 5: lcd.print(“Thu “);break;
    case 6: lcd.print(“Fri “);break;
    case 7: lcd.print(“Sat “);break;
    } ”

    below void printMonth(int month)

    • George March 24, 2016 at 12:35 pm

      Also have to shorten the month names to 3 characters for it to fit.

    • George April 2, 2016 at 9:05 am

      My modified code.
      All time setting is done via the serial console.

      Display example:

      23:59:59 25.25°C
      Sat 02 Apr 2016

      /* LCD 16×2 DS3231 Digital Clock and Thermometer
      To set DS3231 connect via Serial Monitor 9600 bps with Carriage return

      Original code: http://educ8s.tv/arduino-project-real-time-clock-with-ds3231-module/ */

      #include
      #include “ds3231.h”
      #include

      LiquidCrystal lcd(12,11,5,4,3,2); // (RS,E,D4,D5,D6,D7) 16×2 Keypad Shield
      //LiquidCrystal lcd(8,9,4,5,6,7); // (RS,E,D4,D5,D6,D7) 16×2 LCD display
      #define BUFF_MAX 128

      uint8_t time[8];
      char recv[BUFF_MAX];
      unsigned int recv_size = 0;
      unsigned long prev, interval = 1000;

      void setup()
      {
      Serial.begin(9600);
      Wire.begin();
      DS3231_init(DS3231_INTCN);
      memset(recv, 0, BUFF_MAX);
      Serial.println(“Connect: OK”);
      Serial.println(” H – help.”);
      Serial.println(“”);
      lcd.begin(16, 2);
      lcd.clear();
      }

      void loop()
      {
      char in;
      char tempC[6]; // Temp Sensor [Ditial Input]
      float temperature;
      char buff[BUFF_MAX];
      unsigned long now = millis();
      struct ts t;

      // show time once in a while
      if ((now – prev > interval) && (Serial.available() <= 0)) {
      DS3231_get(&t); //Get time
      temperature = DS3231_get_treg(); // Get temperature
      dtostrf(temperature, 5, 2, tempC);

      // Print Time
      // lcd.clear();
      lcd.setCursor(0,0);
      if(t.hour<10)
      {lcd.print("0");}
      lcd.print(t.hour);
      lcd.print(":");
      if(t.min<10)
      {lcd.print("0");}
      lcd.print(t.min);
      lcd.print(":");
      if(t.sec<10)
      {lcd.print("0");}
      lcd.print(t.sec);

      // Print Temp
      lcd.setCursor(9,0);
      lcd.print(tempC);
      lcd.print((char)223);
      lcd.print("C");

      // Print Date
      lcd.setCursor(0,1);
      printDay(t.wday);
      lcd.setCursor(4,1);
      if(t.mday 0) {
      in = Serial.read();

      if ((in == 10 || in == 13) && (recv_size > 0)) {
      parse_cmd(recv, recv_size);
      recv_size = 0;
      recv[0] = 0;
      } else if (in 122) {; // ignore ~[0-9A-Za-z]
      } else if (recv_size > BUFF_MAX – 2) { // drop lines that are too long
      // drop
      recv_size = 0;
      recv[0] = 0;
      } else if (recv_size < BUFF_MAX – 2) {
      recv[recv_size] = in;
      recv[recv_size + 1] = 0;
      recv_size += 1;
      }
      }
      }

      void parse_cmd(char *cmd, int cmdsize)
      {
      uint8_t i;
      uint8_t reg_val;
      char buff[BUFF_MAX];
      struct ts t;

      //snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
      //Serial.print(buff);

      if (cmd[0] == 100 && cmdsize == 10) { // dWDDMMYYYY Set Date
      t.wday = cmd[1] – 48;
      t.mday = inp2toi(cmd, 2);
      t.mon = inp2toi(cmd, 4);
      t.year = inp2toi(cmd, 6) * 100 + inp2toi(cmd, 8);
      DS3231_set(t);
      Serial.println("Date Set");
      } else if (cmd[0] == 84 && cmdsize == 16) { // ThhmmssWDDMMYYYY Set Date & Time
      t.hour = inp2toi(cmd, 1);
      t.min = inp2toi(cmd, 3);
      t.sec = inp2toi(cmd, 5);
      t.wday = cmd[7] – 48;
      t.mday = inp2toi(cmd, 8);
      t.mon = inp2toi(cmd, 10);
      t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
      DS3231_set(t);
      Serial.println("Date & Time Set");
      } else if (cmd[0] == 116 && cmdsize == 7) { // thhmmss Set Time
      t.hour = inp2toi(cmd, 1);
      t.min = inp2toi(cmd, 3);
      t.sec = inp2toi(cmd, 5);
      DS3231_set(t);
      Serial.println("Time Set");
      } else if (cmd[0] == 49 && cmdsize == 1) { // "1" get alarm 1
      DS3231_get_a1(&buff[0], 59);
      Serial.println(buff);
      } else if (cmd[0] == 50 && cmdsize == 1) { // "2" get alarm 1
      DS3231_get_a2(&buff[0], 59);
      Serial.println(buff);
      } else if (cmd[0] == 51 && cmdsize == 1) { // "3" get aging register
      Serial.print("aging reg is ");
      Serial.println(DS3231_get_aging(), DEC);
      } else if (cmd[0] == 65 && cmdsize == 9) { // "A" set alarm 1
      DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
      //ASSMMHHDD
      for (i = 0; i < 4; i++) {
      time[i] = (cmd[2 * i + 1] – 48) * 10 + cmd[2 * i + 2] – 48; // ss, mm, hh, dd
      }
      byte flags[5] = { 0, 0, 0, 0, 0 };
      DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
      DS3231_get_a1(&buff[0], 59);
      Serial.println(buff);
      } else if (cmd[0] == 66 && cmdsize == 7) { // "B" Set Alarm 2
      DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
      //BMMHHDD
      for (i = 0; i < 4; i++) {
      time[i] = (cmd[2 * i + 1] – 48) * 10 + cmd[2 * i + 2] – 48; // mm, hh, dd
      }
      byte flags[5] = { 0, 0, 0, 0 };
      DS3231_set_a2(time[0], time[1], time[2], flags);
      DS3231_get_a2(&buff[0], 59);
      Serial.println(buff);
      } else if (cmd[0] == 67 && cmdsize == 1) { // "C" – get temperature register
      Serial.print("temperature reg is ");
      Serial.println(DS3231_get_treg(), DEC);
      } else if (cmd[0] == 62 && cmdsize == 1) { // "R" – reset status register alarm flags
      reg_val = DS3231_get_sreg();
      reg_val &= B11111100;
      DS3231_set_sreg(reg_val);
      } else if (cmd[0] == 70 && cmdsize == 1) { // "F" – custom fct
      reg_val = DS3231_get_addr(0x5);
      Serial.print("orig ");
      Serial.print(reg_val,DEC);
      Serial.print("month is ");
      Serial.println(bcdtodec(reg_val & 0x1F),DEC);
      } else if (cmd[0] == 71 && cmdsize == 1) { // "G" – set aging status register
      DS3231_set_aging(0);
      } else if (cmd[0] == 83 && cmdsize == 1) { // "S" – get status register
      Serial.print("status reg is ");
      Serial.println(DS3231_get_sreg(), DEC);
      } else if (cmd[0] == 72 && cmdsize == 1) { // "H" – Help
      Serial.println("Help");
      Serial.println(" ");
      Serial.println("If setting Date and time induvidually, date must be set first");
      Serial.println("hour hour minute minute second second");
      Serial.println("Weekday (1-7 Sun-Sat) Day Day Month Month Year Year Year Year");
      Serial.println(" ");
      Serial.println("d – Date: dWDDMMYYYY");
      Serial.println("t – Time: thhmmss 24 hour format");
      Serial.println("T – Date & Time: ThhmmssWDDMMYYYY");
      Serial.println(" ");
      Serial.println("C – display current temperature register");
      Serial.println(" ");
      Serial.println("1 – Display Alarm 1");
      Serial.println("A – Set Alarm 1 – ASSMMHHDD");
      Serial.println(" ");
      Serial.println("2 – Display Alarm 2");
      Serial.println("B – Set Alarm 2 – BMMHHDD");
      Serial.println(" ");
      Serial.println("R – Reset status register alarm flags");
      } else {
      Serial.print("unknown command prefix ");
      Serial.println(cmd[0]);
      Serial.println(cmd[0], DEC);
      }
      }

      void printMonth(int month)
      {
      switch(month)
      {
      case 1: lcd.print("Jan");break;
      case 2: lcd.print("Feb");break;
      case 3: lcd.print("Mar");break;
      case 4: lcd.print("Apr");break;
      case 5: lcd.print("May");break;
      case 6: lcd.print("Jun");break;
      case 7: lcd.print("Jul");break;
      case 8: lcd.print("Aug");break;
      case 9: lcd.print("Sep");break;
      case 10: lcd.print("Oct");break;
      case 11: lcd.print("Nov");break;
      case 12: lcd.print("Dec");break;
      default: lcd.print("Err");break;
      }
      }

      void printDay(int day)
      {
      switch(day)
      {
      case 1: lcd.print("Sun");break;
      case 2: lcd.print("Mon");break;
      case 3: lcd.print("Tue");break;
      case 4: lcd.print("Wed");break;
      case 5: lcd.print("Thu");break;
      case 6: lcd.print("Fri");break;
      case 7: lcd.print("Sat");break;
      }
      }

      • George April 2, 2016 at 9:09 am

        #include did not display correctly, refer to original code
        for Wire.h and LiquidCrystal.h lines

        • Martin September 15, 2016 at 4:40 pm

          There are some other mistakes in the code too. Please correct them.

  9. pietro March 28, 2016 at 10:23 pm

    gli sketck non combaciano e poi a me non funziona non riesco a sincronizzare la data e non esce la temperatura

    • George April 2, 2016 at 8:07 am

      Ha togliendo il commento alla data e ora impostazione quando prima compilazione e il caricamento impostare l’ora e poi rimuovere il commento la stessa linea e caricare ancora una volta data e l’ora sono impostate ?

      Si potrebbe connettersi ad esso attraverso il monitor di serie , fine linea deve essere impostato su ” ritorno a capo ”
      e impostare l’ora e la data inserendo
      TssmmhhWDDMMYYYY

      T secondo giorno della settimana secondo minuto minuto ora ore (Domenica = 1 , Lunedi = 2 , Martedì = 3 , Mercoledì = 4 , Giovedi = 5 , Venerdì = 6 , Sabato = 7) Giorno Giorno Mese Mese Anno Anno Anno Anno

      ad esempio : 20:15 Sabato 2 aprile 2016 sarebbe T002015702042016

      • George April 2, 2016 at 8:23 am

        Errore “, commenta la stessa linea e caricare ancora una volta data e l’ora sono impostate ? “

  10. lino July 9, 2016 at 1:47 pm

    ho seguito il video e ho fatto tutto quello che hai detto!!!
    quando lancio il serial monitor vedo solo la temperatura registrata…
    siccome non ho al momento un display lcd, come faccio a vedere se ho settato bene la data e l’ora, minuti e secondi sul serial monitor?

    grazie

  11. lino July 9, 2016 at 2:22 pm

    scusami ma mi sono procurato un display lcd 16 x 2 con un adattatore con 4 uscite:
    GND,VCC,SDA,SCL….
    L’ho collegato con rtc ma non vedo nulla sul display…..
    come cambia lo sketch?

  12. udith August 3, 2016 at 11:00 pm

    I tryed more time, but this cord can not upload (CODE OF THE PROJECT), please help me

  13. Kito Silva August 24, 2016 at 9:29 pm

    running your code I face problem with

    sketch_aug24a.ino:8:24: fatal error: rtc_ds3231.h: No such file or directory
    compilation terminated.
    Erro ao compilar.

    what should I do to fix that ?

    • Nick August 26, 2016 at 5:56 am

      Please place the file rtc_ds3231.h inside the main program code that you are trying to compile.

  14. Martin September 15, 2016 at 4:49 pm

    Hi there. Is there already a solution to the problem with the flawed conversion from boolean to const uint8_t? Some feedback would be highly appreciated! Muchas gracias in advance!

  15. Carlos October 19, 2016 at 8:04 pm

    Hello, you can make it work with LCD i2c too? Thank you.

  16. Wim December 2, 2016 at 4:59 pm

    Hey

    Question when I removed the USB or power adapter than the clock do not walk stop
    What can be the problem, the RTC module works fine in another arrangement

    greeting william

  17. Laszlo Kosa January 23, 2017 at 10:41 pm

    Hello there 🙂

    Is there a code with Nokia 5110 lcd? 🙁

    I could not find a working code. I use robotdyn uno R3 and ds3231 module.

Comments are closed.

Close