Fortunately, we can also get temperature from this module. What a lucky!
Real time clock is essential part for some application such as data acquisition.
DS3231 uses I2C communication, so the connection circuit with Arduino Pro Mini looks very simple
Before using code with DS3231, the library for it needed to be installed
Code works
#include <Wire.h> #include "DS3231.h" RTClib RTC; DS3231 Clock; byte Year; byte Month; byte Date; byte DoW; byte Hour; byte Minute; byte Second; int tempC; void GetDateStuff(byte& Year, byte& Month, byte& Day, byte& DoW, byte& Hour, byte& Minute, byte& Second) { // Call this if you notice something coming in on // the serial port. The stuff coming in should be in // the order YYMMDDwHHMMSS, with an 'x' at the end. boolean GotString = false; char InChar; byte Temp1, Temp2; char InString[20]; byte j=0; while (!GotString) { if (Serial.available()) { InChar = Serial.read(); InString[j] = InChar; j += 1; if (InChar == 'x') { GotString = true; } } } Serial.println(InString); // Read Year first Temp1 = (byte)InString[0] -48; Temp2 = (byte)InString[1] -48; Year = Temp1*10 + Temp2; // now month Temp1 = (byte)InString[2] -48; Temp2 = (byte)InString[3] -48; Month = Temp1*10 + Temp2; // now date Temp1 = (byte)InString[4] -48; Temp2 = (byte)InString[5] -48; Day = Temp1*10 + Temp2; // now Day of Week DoW = (byte)InString[6] - 48; // now Hour Temp1 = (byte)InString[7] -48; Temp2 = (byte)InString[8] -48; Hour = Temp1*10 + Temp2; // now Minute Temp1 = (byte)InString[9] -48; Temp2 = (byte)InString[10] -48; Minute = Temp1*10 + Temp2; // now Second Temp1 = (byte)InString[11] -48; Temp2 = (byte)InString[12] -48; Second = Temp1*10 + Temp2; } void setup () { Serial.begin(9600); Wire.begin(); } void loop () { delay(1000); //--------------------set clock------------------------- // If something is coming in on the serial line, it's // a time correction so set the clock accordingly. if (Serial.available()) { GetDateStuff(Year, Month, Date, DoW, Hour, Minute, Second); Clock.setClockMode(false); // set to 24h //setClockMode(true); // set to 12h Clock.setYear(Year); Clock.setMonth(Month); Clock.setDate(Date); Clock.setDoW(DoW); Clock.setHour(Hour); Clock.setMinute(Minute); Clock.setSecond(Second); } //-------------------show clock--------------------------- DateTime now = RTC.now(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.print(' '); //-------------------show temperature--------------------------- tempC = Clock.getTemperature(); Serial.print(tempC); Serial.println("dgC"); }
Seems no matter what clock mode you try 12 or 24 hour it stays at 24 hour format
ReplyDeleteyeah, 24hr format is easy to know AM and PM. From 24hr format, we can sort out it for AM and PM
ReplyDelete