Sunteți pe pagina 1din 13

This time I will be showing you how to make a module for letting the Arduino find out the

time.
Although there are many tutorials for the Real Time Clock module I wanted to make my version
of this module entirely focused on the step by step soldering. The Instructable will also try to
include as much example code as possible so that the reader will get more than the basic
knowledge of this module.
This module has the integrated time keeping system using a crystal oscillator. So the timing is
accurate with multiple applications. This module also has a battery backup with which the
module can keep track of the time even when the Arduino is programmed or turned off. Lets start
off with the stuff you will learn and the applications of this module.

Stuff You Will Learn:

The code required to access the module and get the current time.

The code for setting the time.

A simple program with which the user can interact with the module.

Uses of The Project:

Once you are done with this Instructable you will be able to make your own Digital
Clock.

You can build on the idea and make an Alarm Clock.

This Module is the timekeeper for many projects like The Propeller Clock, The Nixie
Clock, etc.

Components Required:
Component for the Module:

DS 1307.

8-Pin DIP Socket.

CR 2032 Button Cell.

Button Battery Holder.

4-Pin Connector.

10k Resistor 1/4 Watt.

32.768kHz Oscillator Crystals.

Prototype Board.

Additional Components:

Solder Wire.

Soldering Iron.

Arduino UNO.

16x2 LCD Character Display.

Jumper Wires (Female to Female).

When I designed this module I ignored the square wave pin, Since I didn't find it useful in my
application but you may add an extra pin if the need arises.

Apart from the arduino the DS1307 Integrated Circuit is the core of this project since it acts as
the timekeeper and tells the arduino when it should leave the appliance on. The DS 1307 is
specifically designed for timekeeping, The time is fairly accurate with an error (time drift) of
about 1 minute per month. If u want to eliminate this u can go for the DS3234 which has a time
drift of only 1 minute per year. For our particular application we can settle for the DS1307 itself.
The beauty of the DS1307 is that it has this backup coin cell. This Coin Cell is commonly the
CR2032. This battery is more than enough for the IC since the DS1307 has a fairly low power
consumption the backup battery life of the cell is about a minimum of 9 years of usage.
So now that the specs have been discussed lets talk about the communication. The DS 1307
communicates with the arduino using IC communication. Simply put the chip sends data in
decimal form such that each decimal form is 4 bits of binary data also known as Binary Coded
Decimal System.
Important Pins:

5V Pin: When this pin is high then the ds1307 sends the data and when it is low it runs on the
backup button cell.
GND: This is the ground pin for the module. Both the ground of the battery and the power
supply are tied together.
SCL: It is the i2c clock pin - Which communicates with the RTC.
SDA: It is the i2c data pin - Which communicates with the RTC.

So now that the introductions are done lets get to actually making the module. The instructable
has steps of the soldering and the places of the components in the board. Hope this method is
better to understand than just providing the circuit diagram.

Step 2: Assembly and Soldering - 1

So the module is only 2 x 3 cm in size, which is pretty compact. The module has a wire
connector but you can also use a male or female header so that it can be directly plugged into the
Arduino. Anyway apart from the square wave pin not soldered the module has all the functional
features.
Making The Module:

Step 1:
Cut the prototype board to the size mentioned above and insert the coin cell holder. Then solder
the bottom end to hold the cell in its place.

Step 2:
Insert the DIP socket right next to the cell holder but leave space for the crystal oscillator like in
the diagram. Solder some pins to hold the socket in place.
Step 3:
Now insert the crystal Oscillator near the first and second pin of the DIP Socket as shown in the
figure. Then solder it as shown.
Note 1:
This note is for showing the colour code for my 4 pin connector for reference, since yours may
vary please double check that the proper connections are made.
Note 2:
Now we need to connect the battery terminals using the tracks as shown in the diagram. These
connections provide the backup power.

Step 3: Assembly and Soldering - 2

tep 5:
Now insert the power pins coming from the Arduino. The positive pin goes to the eighth pin of
the DS 1307. Then the ground is common for the IC and the button cell.
Step 6:
In this step we connect the other two data pins coming from the Arduino as shown in the figure.
I'm sorry because I made the module a little small so there was no hole for the 10k resistor. I
inserted both the resistor and the wire in the same pin and then soldered.
Step 7:
So after you inserted the wires next we need to insert 10k resistors each into the two pins. The
other ends of the two resistors goes to the power pin eight of the DS 1307. Then solder up the
wires, a diagram is provided for reference.
Step 8:
Since I use a connector in this module frequent usage will lead to wear and tear of the wires. So i
made a joint using the cut of wire from the resistors. First insert the wire into one end and solder
it to make it sturdy. Then make a U-bend and pass the wires through this U-bend and tighten the
U-bend for a good grip and then solder the other end too to make a joint. The diagram explains
the method better than words.
The module is finally finished so insert both the battery and the DS1307 IC into their sockets.
The final diagram shows the completed module with all intact.

Step 4: Checking and Setting The Module

Once you are done assembling the module. Connect the module to the Arduino, so that the pins
are inserted properly into the Arduino. The program for testing the module is given below. The
code constantly updates the time through the serial port.
In-order to run the module we need two libraries we need two libraries into the Arduino
software. The following steps set up the Arduino software with the libraries needed and the code.

Importing The Libraries:

Download the library "RealTimeClockDS1307" and save it in the desktop.

Open the Arduino and go to Sketch => Import Library => Add Library.

Then select the library saved in the desktop and click add.

Now paste the sample code given below and click compile. If it compiles successfully
then skip the remaining steps.

If it doesn't import the second library "Wire" and repeat the same procedure and compile
the code and it will work.

Code Working:

The code is written by "David H. Brown" I merely use it to give you an introduction to the
DS1307. Anyway the Arduino communicates with the RTC Module and updates the time for
every second through the Serial Monitor. The creator has given us a option to set the time when
we send the command "?". The following menu appears.
Try these:
h## - set Hours [range 1..12 or 0..24]
i## - set mInutes [range 0..59]
s## - set Seconds [range 0..59]
d## - set Date [range 1..31]
m## - set Month [range 1..12]
y## - set Year [range 0..99]
w## - set arbitrary day of Week [range 1..7]
t
- toggle 24-hour mode
a
- set AM
p
- set PM
z
q

- start clock
- SQW/OUT = 1Hz

Z
Q

- stop clock
- stop SQW/OUT

This code will help you to set the time as well as check if the module's backup
battery supply works properly. In the next tutorial I will show you how to setup a
LCD Display and display the time from the RTC module. Here is the code for testing
the module and an attached file of the same.

#include <Wire.h>
#include <RealTimeClockDS1307.h>
//RealTimeClock RTC;//=new RealTimeClock();
#define Display_Clock_Every_N_Seconds 10
#define Display_ShortHelp_Every_N_Seconds 60
help
//#define TEST_Squarewave
//#define TEST_StopStart
//#define TEST_1224Switch

// n.secs to show date/time


// n.secs to show hint for

int count=0;
char formatted[] = "00-00-00 00:00:00x";
void setup() {
// Wire.begin();
Serial.begin(9600);
pinMode(A3, OUTPUT);
//*** pin 16 (Analog pin 2) as OUTPUT
***
digitalWrite(A3, HIGH);
//*** pin 16 (Analog pin 2) set to LOW ***
pinMode(A2, OUTPUT);
//*** pin 17 (Analog pin 3) as OUTPUT
***
digitalWrite(A2, LOW); //*** pin 17 (Analog pin 3) set to HIGH ***

//*** Analog Pin settings to power RTC module ***

void loop() {
if(Serial.available())
{
processCommand();
}
RTC.readClock();
count++;
if(count % Display_Clock_Every_N_Seconds == 0){
Serial.print(count);
Serial.print(": ");
RTC.getFormatted(formatted);
Serial.print(formatted);
Serial.println();
}
if(count % Display_ShortHelp_Every_N_Seconds == 0) {
Serial.println("Send ? for a list of commands.");
}
#ifdef TEST_Squarewave
if(count%10 == 0)
{
switch(count/10 % 6)
{
case 0:
Serial.print("Squarewave disabled (low impedance): ");
RTC.sqwDisable(0);
Serial.println((int) RTC.readData(7));
break;
case 1:
Serial.print("Squarewave disabled (high impedance): ");
RTC.sqwDisable(1);
Serial.println((int) RTC.readData(7));
break;
case 2:
Serial.println("Squarewave enabled at 1 Hz");
RTC.sqwEnable(RTC.SQW_1Hz);
break;
case 3:
Serial.println("Squarewave enabled at 4.096 kHz");
RTC.sqwEnable(RTC.SQW_4kHz);
break;
case 4:
Serial.println("Squarewave enabled at 8.192 kHz");
RTC.sqwEnable(RTC.SQW_8kHz);
break;
case 5:
Serial.println("Squarewave enabled at 32.768 kHz");
RTC.sqwEnable(RTC.SQW_32kHz);
break;
default:
Serial.println("Squarewave test not defined");
}//switch
}

#endif
#ifdef TEST_StopStart
if(count%10 == 0)
{
if(!RTC.isStopped())
{
if(RTC.getSeconds() < 45)
{
Serial.println("Stopping clock for 10 seconds");
RTC.stop();
}//if we have enough time
} else {
RTC.setSeconds(RTC.getSeconds()+11);
RTC.start();
Serial.println("Adding 11 seconds and restarting clock");
}
}//if on a multiple of 10 counts
#endif
#ifdef TEST_1224Switch
if(count%10 == 0)
{
if(count %20 == 0)
{
Serial.println("switching to 12-hour time");
RTC.switchTo12h();
RTC.setClock();
}
else
{
Serial.println("switching to 24-hour time");
RTC.switchTo24h();
RTC.setClock();
}
}
#endif
}
void processCommand() {
if(!Serial.available()) { return; }
char command = Serial.read();
int in,in2;
switch(command)
{
case 'H':
case 'h':
in=SerialReadPosInt();
RTC.setHours(in);
RTC.setClock();
Serial.print("Setting hours to ");
Serial.println(in);
break;
case 'I':
case 'i':
in=SerialReadPosInt();
RTC.setMinutes(in);

RTC.setClock();
Serial.print("Setting minutes to ");
Serial.println(in);
break;
case 'S':
case 's':
in=SerialReadPosInt();
RTC.setSeconds(in);
RTC.setClock();
Serial.print("Setting seconds to ");
Serial.println(in);
break;
case 'Y':
case 'y':
in=SerialReadPosInt();
RTC.setYear(in);
RTC.setClock();
Serial.print("Setting year to ");
Serial.println(in);
break;
case 'M':
case 'm':
in=SerialReadPosInt();
RTC.setMonth(in);
RTC.setClock();
Serial.print("Setting month to ");
Serial.println(in);
break;
case 'D':
case 'd':
in=SerialReadPosInt();
RTC.setDate(in);
RTC.setClock();
Serial.print("Setting date to ");
Serial.println(in);
break;
case 'W':
Serial.print("Day of week is ");
Serial.println((int) RTC.getDayOfWeek());
break;
case 'w':
in=SerialReadPosInt();
RTC.setDayOfWeek(in);
RTC.setClock();
Serial.print("Setting day of week to ");
Serial.println(in);
break;
case 't':
case 'T':
if(RTC.is12hour()) {
RTC.switchTo24h();
Serial.println("Switching to 24-hour clock.");
} else {
RTC.switchTo12h();
Serial.println("Switching to 12-hour clock.");
}

RTC.setClock();
break;
case 'A':
case 'a':
if(RTC.is12hour()) {
RTC.setAM();
RTC.setClock();
Serial.println("Set AM.");
} else {
Serial.println("(Set hours only in 24-hour mode.)");
}
break;
case 'P':
case 'p':
if(RTC.is12hour()) {
RTC.setPM();
RTC.setClock();
Serial.println("Set PM.");
} else {
Serial.println("(Set hours only in 24-hour mode.)");
}
break;
case 'q':
RTC.sqwEnable(RTC.SQW_1Hz);
Serial.println("Square wave output set to 1Hz");
break;
case 'Q':
RTC.sqwDisable(0);
Serial.println("Square wave output disabled (low)");
break;
case 'z':
RTC.start();
Serial.println("Clock oscillator started.");
break;
case 'Z':
RTC.stop();
Serial.println("Clock oscillator stopped.");
break;
case '>':
in=SerialReadPosInt();
in2=SerialReadPosInt();
RTC.writeData(in, in2);
Serial.print("Write to register ");
Serial.print(in);
Serial.print(" the value ");
Serial.println(in2);
break;
case '<':
in=SerialReadPosInt();
in2=RTC.readData(in);
Serial.print("Read from register ");
Serial.print(in);

Serial.print(" the value ");


Serial.println(in2);
break;
default:
Serial.println("Unknown command. Try these:");
Serial.println(" h## - set Hours [range 1..12 or 0..24]");
Serial.println(" i## - set mInutes [range 0..59]");
Serial.println(" s## - set Seconds [range 0..59]");
Serial.println(" d## - set Date [range 1..31]");
Serial.println(" m## - set Month [range 1..12]");
Serial.println(" y## - set Year [range 0..99]");
Serial.println(" w## - set arbitrary day of Week [range 1..7]");
Serial.println(" t
- toggle 24-hour mode");
Serial.println(" a
- set AM
p
- set PM");
Serial.println();
Serial.println(" z
- start clock
Z
- stop clock");
Serial.println(" q
- SQW/OUT = 1Hz
Q
- stop SQW/OUT");
Serial.println();
Serial.println(" >##,### - write to register ## the value ###");
Serial.println(" <##
- read the value in register ##");
}//switch on command
}
//read in numeric characters until something else
//or no more data is available on serial.
int SerialReadPosInt() {
int i = 0;
boolean done=false;
while(Serial.available() && !done)
{
char c = Serial.read();
if (c >= '0' && c <='9')
{
i = i * 10 + (c-'0');
}
else
{
done = true;
}
}
return i;
}

S-ar putea să vă placă și