Arduino Tutorial: Real Time clock with DS3231 module
Hi friends, Its Nick again, welcome to today’s tutorial, In today’s video, we will be building a Real Time Clock with temperature monitoring capabilities using the DS3231 chip, the Arduino Mega and the Arduino LCD shield which will be used to display the information from the DS3231 module.
Arduino Project: Real-time clock (RTC) and temperature monitor using the DS3231 module.
The DS3231 module is made up of the DS3231 chip which is a cheap and accurate RTC chip capable of storing time and date information for years after being set, as long as the chip keeps getting power from an attached coin cell battery like the CR2032 battery. The Module even automatically makes variations for leap years in its memory once it is set. It works with either 5 or 3.3v voltage level and thus can be used easily with any platform provided it is I2c enabled as the module communicates with the microcontroller over I2C.
To add to its impressive list of attributes, the chip also comes with temperature measuring capabilities which makes it really versatile and useful.
Required Components/Parts and Where to Buy
The following components are required to build this project and they can be bought clicking on the link in front of each component.
1. Arduino Mega ▶ https://educ8s.tv/part/ArduinoMega
2. DS3231 RTC ▶ https://educ8s.tv/part/DS3231
3. Keypad display ▶ https://educ8s.tv/part/KeypadShield
4. Small breadboard ▶ https://educ8s.tv/part/SmallBreadboard
5.Wires ▶ https://educ8s.tv/part/Wires
Schematics
Connect the parts listed above as shown in the project schematics below;
The connection between the 1602 LCD shield and the Arduino is very simple as all we need to do is to plug the module directly on the Arduino.
The pin connections between the DS3231 and the Arduino is however detailed below.
Ds3231 ▶ Arduino Mega
VCC ▶ 5v
GND ▶ GND
SDA ▶ D20/SDA
SCL ▶ D21/SCL
Check the connections to ensure everything adds up, then proceed to the code. if you have any difficulty, feel free to reach out to me via the comment section.
Code
The code for this project is fairly easy, we will need the DS3231 library which can be downloaded from the link below for easy communication with the RTC chip. We will also need the liquid crystal library which comes with the Arduino IDE to interact easily with the LCD and the wire.h library for I2C communication.
——————–
LIBRARIES
——————–
DS3231 Library: https://github.com/rodan/ds3231
[adsense]The goals of the project is simple, we want to be able to display the current date and time as reported to us by the DS3231 module on the LCD with the system updating itself automatically as the time and(or) temperature changes.
To begin, the first thing we do in the code, as usual, is to include the libraries that we will be using.
#include <Wire.h> #include "ds3231.h" #include "rtc_ds3231.h" #include <LiquidCrystal.h>
Next, we create an object of the liquid crystal class and declare the pins to which the LCD is connected to on the Arduino. Next, we declare the global variables which will be used later in the code.
LiquidCrystal lcd(8,9,4,5,6,7); #define BUFF_MAX 128 uint8_t time[8]; char recv[BUFF_MAX]; unsigned int recv_size = 0; unsigned long prev, interval = 1000;
Next, we write the void setup function which features the initialization of the serial communication, the I2C communication via the wire.h library and the LCD and the RTC itself.
void setup() { Serial.begin(9600); Wire.begin(); DS3231_init(DS3231_INTCN); memset(recv, 0, BUFF_MAX); Serial.println("GET time"); lcd.begin(16, 2); lcd.clear(); //Serial.println("Setting time"); //parse_cmd("T302911604102014",16); }
We also set the time and date of the RTC under the void setup function using the parse_cmd(“T302911604102014”,16) line of code. Do note that this line should be commented out after the time has been set.The time can be sent by uncommenting the line of code when the code is been uploaded to the Arduino board for the first time. you can take a look at the video to get more about setting the time.
Next, we move to the void loop function, which is responsible for the continuous update of the display with the correct time and temperature as supplied by the RTC.
void loop() { char in; char tempF[6]; 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 parse_cmd("C",1); temperature = DS3231_get_treg(); //Get temperature dtostrf(temperature, 5, 1, tempF); lcd.clear(); lcd.setCursor(1,0); lcd.print(t.mday); printMonth(t.mon); lcd.print(t.year); lcd.setCursor(0,1); //Go to second line of the LCD Screen 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); lcd.print(' '); lcd.print(tempF); lcd.print((char)223); lcd.print("C "); prev = now; } if (Serial.available() > 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 < 48 || 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; } } }
The full code for this project can be downloaded from the link below. Upload the code to your Arduino board and you should see the display come alive as shown in the image below.
——————–
CODE OF THE PROJECT
——————–
That’s it for today’s tutorial, thanks for reading/watching. If you get stuck at any point while building this project, feel free to reach out to me, will be glad to answer whatever questions you might have. Don’t forget to share, like and subscribe on youtube. Thanks!
——————–
SUBSCRIBE ON YOUTUBE
——————–
Never miss a video: Subscribe to educ8s.tv
Thank you. Nice lesson!
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.
Thank you for your comment. In order to open the file you have to use the WinRar software.
I have such resultat of downloading of code:
http://obrazki.elektroda.pl/4245771300_1467378080.jpg
I have this problem too!
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?
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).
How can i Change The Date
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);”
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.
When i desactive the two linz i can compile but the DATE in the LCD IS 1 january 1900, the temperature is correct.
Merci
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)
Also have to shorten the month names to 3 characters for it to fit.
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: https://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;
}
}
#include did not display correctly, refer to original code
for Wire.h and LiquidCrystal.h lines
There are some other mistakes in the code too. Please correct them.
t.wday = inp2toi(cmd, 6)
will also work for day
gli sketck non combaciano e poi a me non funziona non riesco a sincronizzare la data e non esce la temperatura
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
Errore “, commenta la stessa linea e caricare ancora una volta data e l’ora sono impostate ? “
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
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?
I tryed more time, but this cord can not upload (CODE OF THE PROJECT), please help me
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 ?
Please place the file rtc_ds3231.h inside the main program code that you are trying to compile.
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!
Hello, you can make it work with LCD i2c too? Thank you.
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
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.
For those who want to have 12H AM & PM system with day showed, here is my modification code.
The LCD will appear like:
Mon 04 Sep 2017
PM 11:55 26.5°C
The sign (:) will be blinking to give indication of going seconds.
All credits and rights go to the original programmer. I just modified the code only.
Here is the code:
/*
* This code is for DS3231 RTC I2C module with Liquid LCD without I2C.
* This code is modified to have both Day and Month displayed on LCD screem.
* This code is modified to have 12H AM & PM system
*
* Modified by Majdi N. Al-Jasim
*/
#include
#include “ds3231.h”
#include
//Setting liquid LCD pins to arduino:
//LiquidCrystal(RS, E, D4, D5, D6, D7). RS, E, D4, D5, D6, D7 are the LCD pins.
LiquidCrystal lcd(12,11,5,4,3,2);
#define BUFF_MAX 128
int blinking = 0; //stored int for blinking status
uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 500; //Setting blinking interval
void setup()
{
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
memset(recv, 0, BUFF_MAX);
Serial.println(“GET time”);
lcd.begin(16, 2);
lcd.clear();
/*Setting time for the first time:
* Uncomment the followings two lines, pay attention to TssmmhhWDDMMYYYY in which:
* ss: seconds
* mm: minutes
* hh: hours
* W: day number in the week where Monday is 1
* DD: day
* MM: month
* YYYY: year
* After you upload the sketch, comment these lines again and upload the code again
*/
//Serial.println(“Setting time”);
//parse_cmd(“T000414703092017”,16);
// TssmmhhWDDMMYYYY
}
void loop()
{
char in;
char tempF[6];
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
parse_cmd("C",1);
temperature = DS3231_get_treg(); //Get temperature
dtostrf(temperature, 5, 1, tempF);
//##THE FIRST LINE IN THE LCD##
lcd.setCursor(0,0); //first line of the LCD screen from first position. (x,y) x means position & y means line
printDay(t.wday);
if(t.mday<10)
{
lcd.print("0");
}
lcd.print(t.mday);
printMonth(t.mon);
lcd.print(t.year);
//##THE SECOND LINE IN THE LCD##
lcd.setCursor(0,1); //second line of the LCD Screen from first position. (x,y) x means position & y means line
//#Adding AM & PM to time#
if(t.hour12)
{
lcd.print(t.hour-12);
}
else
{
lcd.print(t.hour);
}
//#Adding blinking : sign between minutes and hours#
if( (blinking & 1) == 0 ) {
lcd.print(“:”);
} else {
lcd.print(” “);
}
blinking ++;
//#Adding minutes time#
if(t.min 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);
// TssmmhhWDDMMYYYY aka set time
if (cmd[0] == 84 && cmdsize == 16) {
//T355720619112011
t.sec = inp2toi(cmd, 1);
t.min = inp2toi(cmd, 3);
t.hour = inp2toi(cmd, 5);
t.wday = inp2toi(cmd, 6);
t.mday = inp2toi(cmd, 8);
t.mon = inp2toi(cmd, 10);
t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
DS3231_set(t);
Serial.println("OK");
} 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] == 68 && cmdsize == 1) { // "D" – 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 {
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("Mon ");break;
case 2: lcd.print("Tue ");break;
case 3: lcd.print("Wed ");break;
case 4: lcd.print("Thu ");break;
case 5: lcd.print("Fri ");break;
case 6: lcd.print("Sat ");break;
case 7: lcd.print("Sun ");break;
}
}
as someone said, #include did not display correctly, refer to original code
for Wire.h and LiquidCrystal.h lines please
It appears some syntax are missing while coping and pasting the code from my previous post.
So I uploaded the code on google drive for those interested.
The link:
https://drive.google.com/file/d/0Bz9s2CoYYs2NT0FYOTBLYUJZQm8/view?usp=sharing
The LCD will appear like:
Mon 04 Sep 2017
PM 11:55 26.5°C
The code featured 12H AM & PM system with blinking (:) to indicate seconds running.
Have a nice codings
There was a bug for C symbol appear incorrectly after having single digit.
Also instead of having AM 12:00 there is bug of having AM 0:00 instead.
I upload new version that fixes these bugs.
Here is the link for updated version:
https://drive.google.com/file/d/0Bz9s2CoYYs2NRXBkZDJmTlN5M1E/view?usp=sharing
The LCD will appear like:
Tue, 05 Sep 2017
AM 12:55 26.5°C
Yours;
Thank you very much, my friend, much appreciated!
Awesome Sketch Majdi..I did encounter an issue with the battery backup on the Ds3231. It doesn’t retain the time on your sketch but does retain time on other sketches…Any help would be appreciated…
Thanks
Robert
Neat project! Thank you. I am getting the following error from the updated version Sept 5, 2017.
DS3231_INTCN not declared in scope. I have used some of the example sketches that came with the recommended library, and they compile. Something about this one is weird.
Probably a simple mistake on my part, but want to get my grandson moving on this one.
Thanks
Hello,
I also get “‘DS3231_INTCN’ was not declared in this scope” and don’t get it right, have you succeeded?
Sincerely
Thomas
same problem
delete every cmnd “DS3231_INTCN”
as we don’t need alarm functions
i did this succeeded
ty :)
hi
i have same problem
thanks
DS1307 RTC Module & i configured this module with Arduino Nano to display time in lcd module and i write code for directly access the time configuration in code dump time but after uploading the time was automatically changed to 00:00 how is possible ??????
Hi, I am also interested to realise this project, and as you, I intended to use Nano, and to add some minutes to the real time, do you think it is possible?? I mean when it is for example 12h30 (real time), I would like it to display 12h40.
Any support would be appreciated.
Hi,
I compiled this project for arduino nano with some changes and date, hour and temperature are not read correct (I used a DS3231 probably clone)
How can i solve this problem ?