How to Use a Real-Time Clock Module (DS3231)

How to Use a Real-Time Clock Module (DS3231)

This project will show you how to use a real-time clock module (DS3231). We'll display the values on the serial monitor.

  • 6,540 views
  • 6 comments
  • 12 respects

Components and supplies

Apps and online services

About this project

Operation

The Time Clock Module ( or DS3231 ) is a module that measures the time, dependently or independently of his Arduino card through of his cell.

The Arduino card measures the elapsed time since the module was turned on ( in ms ).

The module comes assembled ready-to-use, with battery ( supplied ).

Code

The code need the " Wire " Library, which is already in the card. So you don't have to download it.

Connections

Arduino --> Module

SCL --> SCL

SDA --> SDA

VCC --> 5V ( or 3.3V )

GND --> GND

Code

DS3231Arduino
This code will displays values on the serial monitor.
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68

byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
 
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
 
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0);
  Wire.write(decToBcd(second)); 
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour)); 
  Wire.write(decToBcd(dayOfWeek)); 
  Wire.write(decToBcd(dayOfMonth)); 
  Wire.write(decToBcd(month)); 
  Wire.write(decToBcd(year)); 
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); 
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  
  Serial.print(hour, DEC);
 
  Serial.print("h");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print("min");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print("sec  ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" ");
  Serial.print(" Day : ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}
void loop()
{
  displayTime(); 
  delay(1000);
}

Comments

Similar projects you might like

Amazing Binary Clock using LED Matrix Module

Project tutorial by LAGSILVA

  • 9,775 views
  • 12 comments
  • 55 respects

Big Real-Time Clock Using Arduino

Project tutorial by the lonely programmer

  • 1,648 views
  • 1 comment
  • 14 respects

Analog Clock with LED Matrix and Arduino

Project tutorial by LAGSILVA

  • 12,999 views
  • 9 comments
  • 45 respects

LCD with DS3231 Real Time Clock Module

Project showcase by Tan See Youu

  • 41,068 views
  • 0 comments
  • 33 respects

A Very Compact Alarm with Card Reader and Real-Time Clock

Project tutorial by Simone Adobs

  • 6,542 views
  • 3 comments
  • 31 respects

Digital Clock with Arduino, RTC and Shift Register 74HC595

Project tutorial by LAGSILVA

  • 22,604 views
  • 16 comments
  • 50 respects
Add projectSign up / Login