Sitemap / Advertise

Introduction

To generate code for Arduino automatically to obtain data from a DS3231 RTC module, enter SDA and SCL pins depending on the pinout.


Tags

Share

Arduino | DS3231 RTC Code Generator

Advertisement:


read_later

Read Later



read_later

Read Later

Introduction

To generate code for Arduino automatically to obtain data from a DS3231 RTC module, enter SDA and SCL pins depending on the pinout.

Tags

Share





Advertisement

Advertisement





Description

To generate code for Arduino automatically to obtain data from a DS3231 RTC module, enter SDA and SCL pins depending on the pinout. And, if you are a novice to use the DS3231 RTC module with Arduino, do not worry this tool provides you with the proper and concise code, including the required library, for the mentioned module.

DS3231 RTC Module(1)

The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperaturecompensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line. The DS3231 is available in commercial and industrial temperature ranges, and is offered in a 16-pin, 300-mil SO package.

The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially through an I2C bidirectional bus.

A precision temperature-compensated voltage reference and comparator circuit monitors the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the RST pin is monitored as a pushbutton input for generating a μP reset.


Code Generator

Download the DS3231 library from here.

Define the SDA and SCL pins and connect the 5V and GND pins to Arduino according to the given pinout.


SDA :

SCL :


// Arduino:           
//                          DS3231 RTC Module
// {{SDA_PIN}} -------------------- SDA  
// {{SCL_PIN}} -------------------- SCL
// 5V -------------------- 5V
// GND ------------------- GND

// Include this library for DS3231 RTC module and init the DS3231 using SDA and SCL pins.
#include 
DS3231  rtc({{SDA_PIN}}, {{SCL_PIN}});

// Define a time variable to get data from DS3231 properly.
Time t;

void setup(){

Serial.begin(9600);

// Initialize the rtc object
rtc.begin();

// The following lines can be uncommented to set the date and time
//  rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to Wednesday
//  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
//  rtc.setDate(17, 10, 2018);   // Set the date to October 17st, 2018

} 

void loop(){
  // Get the current hour and minute from DS3231. t.hour; and t.min;
  t = rtc.getTime();

  // Write Dow
  Serial.println("DOW:");
  Serial.println(rtc.getDOWStr(FORMAT_SHORT));
  // Write Date
  Serial.println("DATE:");
  Serial.println(rtc.getDateStr(FORMAT_SHORT));
  // Write Time
  Serial.println("TIME:");
  Serial.println(rtc.getTimeStr());
  // Write Temperature
  Serial.println("TEMPERATURE:");
  Serial.println(rtc.getTemp());
  Serial.print(" C");

}


References

(1) https://datasheets.maximintegrated.com/en/ds/DS3231.pdf