Arduino Based Digital Clock with Alarm

Arduino

BySaddam 51

Arduino Alarm Clock

This Arduino based Real time clock is a digital clock to display real time using a RTC IC DS1307 which works on I2C protocol. Real time clock means it runs even after power failure. When power is reconnected, it displays the real time irespective to the time and duration it was in off state. In this project we have used a 16x2 LCD module to display the time in - (hour, minute, seconds, date, month and year) format. An Alarm option is also added and we can set up the alarm time. Once alarm time it saved in internal EEPROM of arduino, it remains saved even after reset or electricity failure. Real time clocks are commonly used in our computers, houses, offices and electronics device for keeping them updated with real time.

 

I2C protocol is a method to connect two or more devices using two wires to a single system, and so this protocol is also called as two wire protocol. It can be used to communicate 127 devices to a single device or processor. Most of I2C devices run on100Khz frequency.

 

Steps for data writing master to slave (slave receiving mode)

  1. Sends START condition to slave.
  2. Sends slave address to slave.
  3. Send write bit (0) to slave.
  4. Received ACK bit from slave
  5. Sends words address to slave.
  6. Received ACK bit from slave
  7. Sends data to slave.
  8. Received ACK bit from slave.
  9. And last sends STOP condition to slave.

 

Steps for data reading from slave to master (slave transmitting mode)

  1. Sends START condition to slave.
  2. Sends slave address to slave.
  3. Send read bit (1) to slave.
  4. Received ACK bit from slave
  5. Received data from slave
  6. Received ACK bit from slave.
  7. Sends  STOP condition to slave.

 

Circuit Diagram and Description

Arduino Based Alarm Clock Circuit Diagram

In this Arduino based digital clock circuit, we have used three major components which are IC DS1307, Arduino Pro Mini Board and 16x2 LCD module.  

Here arduino is used for reading time from ds1307 and display it on 16x2 LCD. DS1307 sends time/date using 2 lines to arduino. A buzzer is also used for alarm indication, which beeps when alarm is activated. A block diagram is shown below to understand the working of this Real Time Clock.

Arduino based Alarm Clock Project Block Diagram

As you can see in the circuit diagram, DS1307 chip pin SDA and SCL are connected to arduino pins SDA and SCL with pull up resistor that holds default value HIGH at data and clock lines. 32.768KHz crystal oscillator is connected with DS1307chip for generating exact 1 second delay, and a 3 volt battery is also connected to pin 3rd (BAT) of DS1307 which keeps time running after electricity failure.16x2 LCD is connected with arduino in 4-bit mode. Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3. And data pin D0-D7 is connected to 4, 5, 6, 7 of arduino. A buzzer is connected with arduino pin number 13 through a NPN BC547 transistor having a 1 k resistor at its base.

 

Three buttons namely set, INC and Next are used for setting alarm to pin 12, 11 and 10 of arduino in active low mode. When we press set, alarm set mode activates and now we need to set alarm by using INC button and Next button is used for moving to digit. The complete breadboard setup of this real time clock with alarm is shown in below image. You can also check a detailed tutorial on digital alarm clock with AVR microcontroller.

Breadboard setup of arduino based alarm clock

 

Program Description

To program for this real time clock, we have used some libraries for extracting time/date from DS1307 and for displaying on LCD, which are given below:

library

And initialization of RTC, LCD and input output are performed in setup loop.

Initialize

Rest of things like reading time, setting alarm is performed in void loop section.

reading time

Code

/* ----- C Program for Arduino based Alarm Clock ---- */

#include <Wire.h>
#include<EEPROM.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(3, 2, 4, 5, 6, 7); 
RTC_DS1307 RTC;
int temp,inc,hours1,minut,add=11;

int next=10;    
int INC=11;
int set_mad=12;

#define buzzer 13

int HOUR,MINUT,SECOND;

 
void setup()
{
 Wire.begin();
 RTC.begin();
 lcd.begin(16,2);
 pinMode(INC, INPUT);
 pinMode(next, INPUT);
 pinMode(set_mad, INPUT);
 pinMode(buzzer, OUTPUT);
 digitalWrite(next, HIGH);
 digitalWrite(set_mad, HIGH);
 digitalWrite(INC, HIGH);
 
   lcd.setCursor(0,0);
   lcd.print("Real Time Clock");
   lcd.setCursor(0,1);
   lcd.print("Circuit Digest ");
    delay(2000);
    
 if(!RTC.isrunning())
 {
 RTC.adjust(DateTime(__DATE__,__TIME__));
 }
}
 
void loop()
{
   int temp=0,val=1,temp4;
   DateTime now = RTC.now();
   if(digitalRead(set_mad) == 0)      //set Alarm time
   { 
     lcd.setCursor(0,0);
     lcd.print("  Set Alarm  ");
    delay(2000);
    defualt();
    time();
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("  Alarm time ");
    lcd.setCursor(0,1);
    lcd.print(" has been set  ");
    delay(2000);
 }
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Time:");
 lcd.setCursor(6,0);
 lcd.print(HOUR=now.hour(),DEC); 
 lcd.print(":");
 lcd.print(MINUT=now.minute(),DEC);
 lcd.print(":");
 lcd.print(SECOND=now.second(),DEC);
 lcd.setCursor(0,1);
 lcd.print("Date: ");
 lcd.print(now.day(),DEC);
 lcd.print("/");
 lcd.print(now.month(),DEC);
 lcd.print("/");
 lcd.print(now.year(),DEC);
 match();
 delay(200);
}

void defualt()
{
  lcd.setCursor(0,1);
  lcd.print(HOUR);
  lcd.print(":");
  lcd.print(MINUT);
  lcd.print(":");
  lcd.print(SECOND);
}

/*Function to set alarm time and feed time into Internal eeprom*/

void time()                             
{
  int temp=1,minuts=0,hours=0,seconds=0;
    while(temp==1)
    {
     if(digitalRead(INC)==0)
     {
      HOUR++;
      if(HOUR==24)
      {
       HOUR=0;
      }
      while(digitalRead(INC)==0);
     }
     lcd.clear();
      lcd.setCursor(0,0);
    lcd.print("Set Alarm Time ");
   //lcd.print(x); 
    lcd.setCursor(0,1);
    lcd.print(HOUR);
    lcd.print(":");
    lcd.print(MINUT);
    lcd.print(":");
    lcd.print(SECOND);
    delay(100);
    if(digitalRead(next)==0)
    {
      hours1=HOUR;
      EEPROM.write(add++,hours1);
     temp=2;
     while(digitalRead(next)==0);
    }
    }
    
    while(temp==2)
    {
     if(digitalRead(INC)==0)
     {
      MINUT++;
      if(MINUT==60)
      {MINUT=0;}
      while(digitalRead(INC)==0);
     }
     // lcd.clear();
    lcd.setCursor(0,1);
    lcd.print(HOUR);
    lcd.print(":");
    lcd.print(MINUT);
    lcd.print(":");
    lcd.print(SECOND);
    delay(100);
      if(digitalRead(next)==0)
      {
       minut=MINUT;
       EEPROM.write(add++, minut);
       temp=0;
       while(digitalRead(next)==0);
      }
    }
    delay(1000);
}

/* Function to chack medication time */

void match()
{
  int tem[17];
  for(int i=11;i<17;i++)
  {
    tem[i]=EEPROM.read(i);
  }
  if(HOUR == tem[11] && MINUT == tem[12]) 
  {
   beep();
   beep();
   beep();
   beep();
   lcd.clear();
   lcd.print("Wake Up........");
   lcd.setCursor(0,1);
   lcd.print("Wake Up.......");
   beep();
   beep();
   beep();
   beep();
  }

}

/* function to buzzer indication */

void beep()
{
   digitalWrite(buzzer,HIGH);
   delay(500);
   digitalWrite(buzzer, LOW);
   delay(500);
}

Video

Comments

  • dimas's picture
    dimas
    Sep 04, 2015

    Thanks it's usefull

  • Bazil's picture
    Bazil
    Dec 28, 2015

    Can i set more than one laram at a time...?

  • Marko's picture
    Marko
    Jan 10, 2016

    I am using Arduino uno and Arduino IDE 1.6.6 compailer, and when i compile the code he say that beep was not declared, any help?

  • Jayant's picture
    Jayant
    Jan 14, 2016

    Define the beep() function properly, check the code above.

  • saddam khan's picture
    saddam khan
    Jan 21, 2016

    Please add a close curly bracket before beep function.

  • KENNETH's picture
    KENNETH
    Jan 28, 2016

    if i set the alarm it can alarm everyday?
    what is the purpose of set alarm 2,3,4,5??

  • SALIHU's picture
    SALIHU
    Feb 03, 2016

    thanks.
    pls how can i use this code to switch on relay at a set time for 8 seconds and delay for 30 minutes

  • angelserra's picture
    angelserra
    Mar 31, 2016

    there are many errors in the 1.65 version of arduino

  • Muhammad Waqas's picture
    Muhammad Waqas
    Apr 11, 2016

    I am using arduino uno. While compiling error appears like following.
    {RTC_DS1307 does not name a type.}
    Any help ....

  • Abhishek's picture
    Abhishek
    Apr 17, 2016

    @angelserra @Muhammad Include RTClib.h Library and use latest version of Arduino Software.

  • nikki's picture
    nikki
    Apr 28, 2016

    is there a snooze button?

  • Junel's picture
    Junel
    Apr 29, 2016

    please help it has error on the RTC_DS1307 RTC;

  • Diploma Student's picture
    Diploma Student
    May 01, 2016

    Is there any printed circuit board diagram that can you show ?
    Can you explain what is arduino ?
    Are this project cost really expensive ?
    Can this project be done in 4 month or less than that ?

  • Motty's picture
    Motty
    May 08, 2016

    I am using Arduino: 1.6.8 (Windows 10), Board: "Arduino/Genuino Uno"

    I put the same code but I got this error message:

    Library can't use both 'src' and 'utility' folders. Double check C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire
    Error compiling for board Arduino/Genuino Uno.

    Plz help !!

  • Maddy's picture
    Maddy
    May 21, 2016

    Please install the Wire.h library properly.

  • erneste's picture
    erneste
    Jun 04, 2016

    usefull!, but please can you help me to set more than one alarm

  • Vishwa's picture
    Vishwa
    Jun 19, 2016

    Can you please suggest how to use Adafruit trinket 3.3V in place of Uno and still get a digital clock?

  • skiller's picture
    skiller
    Aug 18, 2016

    i'm simualate on protues . i see after a period lcd be blink !
    can you hepl me ? thank you very much ?

  • mohamed's picture
    mohamed
    Aug 24, 2016

    thank you
    when alarm on i must rest arduino to active another alarm

  • shiva's picture
    shiva
    Aug 30, 2016

    the project seems to work fine but the date and time are wrong. How should I correct it?

  • shiva's picture
    shiva
    Aug 31, 2016

    the date and time are wrong how should we set them peoperly?

  • Abhishek's picture
    Abhishek
    Sep 17, 2016

    Current Code already has provision to set the time to 'now' (DateTime now = RTC.now();). Further check this article for set the current time: arduino-info(dot)wikispaces(dot)com/DS1307_RealTime_Clock_Brick (replace dot with .)

  • Ron's picture
    Ron
    Sep 05, 2016

    exit status 1
    'beep' was not declared in this scope

  • Atul Mishra's picture
    Atul Mishra
    Oct 12, 2016

    I am using arduino uno. While compiling error appears like following.
    {RTC_DS1307 does not name a type.}
    plz do needful

  • Mike's picture
    Mike
    Oct 13, 2016

    For each include like the following, you will need to have installed the library. Google how to install arduino libraries.
    #include <RTClib.h>

  • rahul jalare's picture
    rahul jalare
    Oct 21, 2016

    good

  • Samrat Dutta's picture
    Samrat Dutta
    Oct 21, 2016

    Can i use arduino uno for making this project..please reply ..thank you..

  • Prashant's picture
    Prashant
    Oct 26, 2016

    I want to set alarm date wise. for example I want to set 3 month future date and time. Can I be possible ?

  • Mark 's picture
    Mark
    Oct 26, 2016

    Can this be used to set the alarm for multiple intervals throughout the day and week?

  • Maddy's picture
    Maddy
    Oct 27, 2016

    Currently you can set only one Time for alarm.

  • wajhi 's picture
    wajhi
    Nov 13, 2016

    can we replace arduino mini with arduino uno . while by doing this does we need to change the code or circuit

  • Abhishek's picture
    Abhishek
    Nov 30, 2016

    You can use same code and circuit, just take care of pin no in circuit and in code.

  • Arjun Oberai's picture
    Arjun Oberai
    Nov 23, 2016

    Right now the alarm works just once. To make it right, we have to just reset the value of the add to 11 int he void match().
    In all just write this part (mentioned below) before the ending of the void match()
    add = 11;
    Thanks for the code Saddam!!

  • Lan's picture
    Lan
    Jan 13, 2017

    Hi we are conducting a Thesis Project "Real Time Clock alarm with buzzer using arduino device.
    we are having a hard time for the set up but the video given was useful for us, we ask if You guys/admins can help us for the codes, tutorials for setting the kit and diagrams so our Thesis Project will function successfully, We are graduating students of Palawan State University, Puerto Princesa City, Palawan. we are hoping for the response Thank you. :)

  • Gaurav's picture
    Gaurav
    Feb 05, 2017

    Sir,plz gave list of equipment used in that project.which arduino can i use po mini or uno.

  • santosh's picture
    santosh
    Feb 12, 2017

    if(HOUR == tem[11] && MINUT == tem[12])
    i want to know what is tem(11 )and tem( 12) stands for ?

  • shweta's picture
    shweta
    Mar 09, 2017

    I am not getting the output on LCD in proteus. the LCD is just on but the msg is not displayed.what shall
    i do??

  • Ramesh's picture
    Ramesh
    Mar 17, 2017

    Sir,i have loaded your code in to Arduino Uno,it compiled successfully without any error, but it not displaying anything in LCD. Only block comes n goes.
    Pl tell me etc module required any previous string time before your code loading,if any pl tell me procedure to set RTc modules before loading your codes.
    Thanks n pl reply,
    Ramesh

  • hussnain's picture
    hussnain
    Apr 18, 2017

    I found the solution in code where liquid crystal(3,2,4,5,6,7) replace it by liquid crystal(2,3,4,5,6,7).

  • Shreya Roy's picture
    Shreya Roy
    Apr 06, 2017

    what is the use of RTClib.h function?and why it gives an error in arduino uno ide software? any help

  • Abhishek's picture
    Abhishek
    Apr 08, 2017

    RTClib library has functions related to RTC, which we have used in our program. Install the Library properly.

  • hussnain's picture
    hussnain
    Apr 14, 2017

    Sir,i have loaded your code in to Arduino Uno,it compiled successfully without any error, but it not displaying anything in LCD. Only block comes and goes. but time is working good and alarm also working but on display blocks comes and goes and nothing displaying correctly.

    What the error is plz tell me.
    Thanks in advance.

  • hussnain's picture
    hussnain
    Apr 18, 2017

    I found the solution in code where liquid crystal(3,2,4,5,6,7) replace it by liquid crystal(2,3,4,5,6,7).

  • iqu's picture
    iqu
    Jul 16, 2017

    Dear sir, admin,
    thank you for the project , i am a beginner and i have a request to make alarm in every 30 miniuts ,
    please help me to do a write a code for it.

    thanks and regards

  • james's picture
    james
    Dec 18, 2017

    can this program work for atmega128?

  • Joezzeth Mampusti's picture
    Joezzeth Mampusti
    Jan 16, 2018

    How can i use 4x4 keypad instead of push button ?

  • Abdul Muneeb's picture
    Abdul Muneeb
    Apr 13, 2018

    R/Sir
    How can I interface jammer with this circuit plz help me........

  • Sebastian's picture
    Sebastian
    Jun 20, 2018

    Hi!

    I see that we can set multiple alarms given that once you set the alarms they go up to 9. What part of the code should i change to set only 1 alarm?

Leave a Comment