Tag Archives: clock

How To Build A Battery Backup Real-Time Clock

Previously, we looked at how to build a simple DIY Arduino clock. Unfortunately, if power to the clock is disconnected, the whole thing has to be reset. To solve that problem, we’re going to add an Arduino battery backup real time clock module based on the DS1307 RTC package. Instead of purchasing a pre-made part, we’ll walk through how to build a DIY Arduino RTC module from scratch and add it to our clock project.

PARTS/TOOLS:

(Most of these can be found in the Arduino Starter Kit available here)

Arduino UNO

1602 LCD

Solderless breadboard

Assorted resistors (220 and 10K, specifically)

Jumper (Dupont) Wire

10K Potentiometer

Tactile switches

DS1307 Real Time Clock

32.768kHz Crystal Oscillator

Coin Cell Holder

CR2032 Coin Cell Battery

RESOURCES:

DS1307 Datasheet

RealTimeClockDS1307 Arduino Library

THE CIRCUIT:

Assemble the LCD circuit as described in previous projects. Connect the DS1307 pins as indicated in the following illustration:

THE SKETCH:

#include
#include

//RealTimeClock RTC;//=new RealTimeClock();

#define Display_Clock_Every_N_Seconds 10 // n.secs to show date/time
#define Display_ShortHelp_Every_N_Seconds 60 // n.secs to show hint for help
//#define TEST_Squarewave
//#define TEST_StopStart
//#define TEST_1224Switch

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;
}

How To Build An Arduino Clock

Clocks are a rite of passage for hardware hackers, and with this video you can start working on your DIY Clock merit badge using the Arduino platform to build a basic Arduino clock. This project builds on the Arduino Fortune Teller project from the “Arduino For Kooks” basic series and teaches programming concepts like timing and actively updating a display, so you can use it as a springboard for many more complicated projects!

PARTS/TOOLS:

(Most of these can be found in the Arduino Starter Kit available here)

Arduino UNO

1602 LCD

Solderless breadboard

Assorted resistors (220 and 10K, specifically)

Jumper (Dupont) Wire

10K Potentiometer

Tactile switches

The Circuit:

Connect the LCD module as described in the Liquid Crystal Ball project. You can use the breadboard to create buses for +5V and GND. Connect one side of one tactile switch to D8 and the other to a 10K resistor to ground. Connect one side of the other tactile switch to D9 and the other side to a 10K resistor to ground.

The Sketch:

//Projet ColorTyme
//Phase 1: Simple LCD Clock
//CC-BY-SA Matthew Eargle
//AirborneSurfer.com
//element14 Presents

#include "LiquidCrystal.h"

// Define LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// initial Time display is 00:00:00 (24hr clock)
int h=00;
int m=00;
int s=00;

// Time Set Buttons
int button1;
int button2;
int hs=8;// pin 8 for Hours Setting
int ms=9;// pin 9 for Minutes Setting

// Digital LCD Constrast setting
int cs=6;// pin 5 for contrast PWM
static int contrast=100;// default contrast

//Define current time as zero
static uint32_t last_time, now = 0;

void setup()
{
lcd.begin(16,2);
pinMode(hs,INPUT_PULLUP);
pinMode(ms,INPUT_PULLUP);

now=millis(); // read RTC initial value
analogWrite(cs,contrast);
}

void loop()
{
// Update LCD Display
// Print TIME in Hour, Min, Sec
lcd.setCursor(0,0);
lcd.print("Time ");
if(h<10)lcd.print("0");// always 2 digits
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);


lcd.setCursor(0,1);// for Line 2
lcd.print("SURF STD TIME");

while ((now-last_time) < 1000 ) // wait1000ms
{
now=millis();
}

last_time=now; // prepare for next loop
s=s+1; //increment sec. counting


/-------Time setting-------/
button1=digitalRead(hs);
if(button1==0)
{
s=0;
h=h+1;
}

button2=digitalRead(ms);
if(button2==0){
s=0;
m=m+1;
}

analogWrite(cs,contrast); // update contrast

/* ---- manage seconds, minutes, hours am/pm overflow ----*/
if(s==60){
s=0;
m=m+1;
}
if(m==60)
{
m=0;
h=h+1;
}
if (h==25)
{
h=0;
}

}

Day-O // ShaunInman.com

Great little replacement for the default OSX menubar clock.

The glasshouse of designer Shaun Inman featuring insights on websites, css and design accompanied by original music.

Source: Day-O // ShaunInman.com