Wednesday, May 18, 2016

DS1307 Real-time Clock

Using DS1307 Real-time clock is discussed. A module in AliExpress costs only about $1. It even consists of a 32k EEPROM called AT24C32.


Figure. Setup.


Its schematic diagram is shown below.


Figure. Schematic circuit for the DS1307 module. (From Emartee)
Connecting Arduino and that RTC module is quite simple. 2 wires for Power and another 2 for communication.


Figure. Wire connection.


An example ,using DS107 Real-time clock with Arduino on Github, is listed here again.

#include 
int y,m,d,wd,h,n,s;//year, month, day, week day, hour, minute, second
char* wds[]={"SAT","SUN","MON","TUE","WED","THU","FRI"};
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(115200);  // start serial for output

  if(0){ //To change settings
    Wire.beginTransmission(0x68); // transmit to device #104
    Wire.write(0x00);//Set address to 0
    Wire.endTransmission();// stop transmitting
    
    Wire.requestFrom(0x68, 7);    // request 7 bytes from slave device #104
    s = Wire.read();//second
    n = Wire.read();//minute
    h = Wire.read();//hour
    wd= Wire.read();//week day
    d = Wire.read();//day
    m = Wire.read();//month
    y = Wire.read();//year

    if(0){ //To set date/time
      s=0x00;
      n=0x26;
      h=0x12;
      wd=0x03;
      d=0x17;
      m=0x05;
      y=0x16;
    }
    
    Wire.beginTransmission(0x68); // transmit to device #104
    Wire.write(0x00);//Set address to 0
    Wire.write(s & 0x7F);//Enable oscillator
    Wire.write(n);//minute
    Wire.write(h & 0x3F);//hour to 25hr mode
    Wire.write(wd);//weekday
    Wire.write(d);//day
    Wire.write(m);//month
    Wire.write(y);//year
    Wire.endTransmission();// stop transmitting
  }
}

void loop()
{
  delay(1000);
  
  Wire.beginTransmission(0x68); // transmit to device #104
  Wire.write(0x00);//Set address to 0
  Wire.endTransmission();// stop transmitting
  
  Wire.requestFrom(0x68, 7);    // request 7 bytes from slave device #104
  s = Wire.read();//second
  n = Wire.read();//minute
  h = Wire.read();//hour
  wd= Wire.read();//week day
  d = Wire.read();//day
  m = Wire.read();//month
  y = Wire.read();//year

  Serial.print(0x20,HEX);
  Serial.print(y,HEX);
  Serial.print("/");
  Serial.print(m,HEX);
  Serial.print("/");
  Serial.print(d,HEX);
  Serial.print(" ");
  Serial.print(h,HEX);
  Serial.print(":");
  Serial.print(n,HEX);
  Serial.print(":");
  Serial.print(s,HEX);
  Serial.print(" ");
  Serial.print(wds[wd%7]);
  Serial.print("\n");
  Serial.print("\n");
}


No comments:

Post a Comment

Comments are moderated and don't be surprised if your comment does not appear promptly.