In this tutorial, we will integrate Surilli WiFi with DS3231 Real Time Clock Module and compute the real-time values of date, time, and year with the results shown on the serial monitor of Arduino IDE.
What Is the DS3231 RTC Module?DS3231 RTC Module is a low-cost real-time clock module that can contain hours, minutes, and seconds as well as days, minutes, and year information. Also, it has automatic compensation for leap-years and for months with fewer than 31 days. The module uses the I2C communication protocol.
The module can work on either 3.3 or 5V, which makes it suitable for many development platforms or microcontrollers. The battery input is 3V and a typical CR2032 3V battery can power the module and maintain the information for more than a year.
1. Surilli WiFi
2. Arduino IDE
3. Connecting wires
4. Breadboard
5. DS3231 RTC Module
Connections:VCC PIN (DS3231 RTC Module) ---> USB PIN (Surilli WiFi)
GND PIN (DS3231 RTC Module) ---> GND PIN (Surilli WiFi)
SDA PIN (DS3231 RTC Module) ---> PIN 4 (Surilli WiFi)
SCL PIN (DS3231 RTC Module) ---> PIN 5 (Surilli WiFi)
There are also two other pins which are the 32K and SQW ones but we will not use them as we get the full functionality through the I2C interface.
Integrating RTC Library Into Arduino IDE:1. Download the "RTC by Makuna" Library.
2. Go to Sketch ---> Include Library ---> Add.zip Library.
3. Select the downloaded zip file.
4. If the library is successfully installed, Arduino IDE will show "Library added to your libraries. Check include Library menu" on the status bar.
Set Up Arduino IDE for Surilli:Make sure you have selected the right port, board and processor for the Surilli as shown in the picture below and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
The Circuitry:The circuitry is very simple. It's mostly the programming. Follow the figure below to set up your hardware.
Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE and hit upload. The results can be viewed on the Serial Monitor.
Arduino Code:First, we include a necessary library for Real Time Clock. Then we create an array for a day of the week. In setup, the Serial Monitor is begun at 9600 Baud and Date and Time are set according to system time if the time of RTC is not set. In the loop, we fetch current date and time from Real Time Clock and it is displayed in the serial monitor. To reset Real Time Clock, remove all wires from Arduino and also battery from Real Time Clock for 10 seconds. Then reassemble the circuit and upload the program again.
// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
void setup ()
{
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
Wire.begin(4, 5); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
// RtcTemperature temp = Rtc.GetTemperature();
// temp.Print(Serial);
// // you may also get the temperature as a float and print it
// // Serial.print(temp.AsFloatDegC());
// Serial.println("C");
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
Play with the program to see how it reacts to different values and logic.
If you make something fun and interesting, do share it with our community.
That’s all for now. If you have any queries, visit surilli.io or contact our support. Stay connected with the Surilli family for more amazing stuff. :-)
Comments
Please log in or sign up to comment.