Arduino

Arduino Tutorial #25 How to make Arduino Alarm Clock

How to make arduino alarm clock

In this tutorial we are going to see how to make arduino alarm clock. To set alarm you don’t need to go on Arduino IDE software. You can set alarm just by pressing pushbuttons. So let’s get started.

For this you will need

  1. Arduino,
  2. Real time clock (DS1302),
  3. Piezo Buzzer,
  4. LCD,
  5. I2C,
  6. 4 pushbuttons,
  7. Four 1K ohms resistors,
  8. Jumper wires,
  9. Breadboard.

Do connection as shown in diagram.

 

Circuit diagram for arduino alarm clock
Circuit diagram for arduino alarm clock

 

Sketch for arduino alarm clock
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include <DS1302.h>
int Hour;
int Min;
int pset = 8; // pushbutton for setting alarm
int phour = 9; // pushbutton for hour
int pmin = 10; // pushbutton for minutes
int pexit = 11; // pushbutton for exit of set alarm
int buzzer = 6;
int h;
int m;
int buttonforset = 0; // pushbutton state for setting alarm
int buttonforhour = 0; // pushbutton state for hour
int buttonformin = 0;// pushbutton state for minutes
int buttonforexit = 0; // pushbutton state for exit of set alarm
int activate=0;
 
Time t;

// Init the DS1302
DS1302 rtc(2, 3, 4);



void setup()
{
 pinMode(pset, INPUT);
 pinMode(phour, INPUT);
 pinMode(pmin, INPUT);
 pinMode(pexit, INPUT);
 // Set the clock to run-mode, and disable the write protection
 rtc.halt(false);
 rtc.writeProtect(false);


 // Setup LCD to 16x2 characters
 lcd.begin();

// The following lines can be commented out to use the values already stored in the DS1302
 //rtc.setDOW(SATURDAY); // Set Day-of-Week to FRIDAY
 //rtc.setTime(10, 0, 0); // Set the time to 12:00:00 (24hr format)
 //rtc.setDate(11, 11, 2017); // Set the date to August 6th, 2010
}

void loop()
{
 if (activate == 0) {

// Display time on the right conrner upper line
 lcd.setCursor(0, 0);
 lcd.print("Time: ");
 lcd.setCursor(6, 0);
 lcd.print(rtc.getTimeStr());
 
 // Display abbreviated Day-of-Week in the lower left corner
 //lcd.setCursor(0, 1);
 //lcd.print(rtc.getDOWStr(FORMAT_SHORT));
 
 // Display date in the lower right corner
 lcd.setCursor(0, 1);
 lcd.print("Date: ");
 lcd.setCursor(6, 1);
 lcd.print(rtc.getDateStr());
 t = rtc.getTime();
 Hour = t.hour;
 Min = t.min;
 buttonforset = digitalRead(pset);
 } // setting button pressed
 if (buttonforset == HIGH) {
 activate =1;
 lcd.clear(); }
 while(activate== 1){
 lcd.setCursor(0,0);
 lcd.print("Set Alarm");
 lcd.setCursor(0,1);
 lcd.print("Hour= ");
 lcd.setCursor(9,1);
 lcd.print("Min= ");
 buttonforhour = digitalRead(phour); // set hour for alarm
 if (buttonforhour == HIGH){
 h++;
 lcd.setCursor(5,1);
 lcd.print(h);
 if (h>23){
 h=0;
 lcd.clear(); }
 delay(100); 
 }
 buttonformin = digitalRead(pmin); // set minutes for alarm
 if (buttonformin == HIGH){
 m++;
 lcd.setCursor(13,1);
 lcd.print(m);
 if (m>59){
 m=0; 
 lcd.clear();}
 delay(100); 
 }

lcd.setCursor(5,1);
 lcd.print(h);
 lcd.setCursor(13,1);
 lcd.print(m);
 buttonforexit = digitalRead(pexit); // exit from set alarm mode
 if (buttonforexit == HIGH){
 activate = 0;
 lcd.clear();
 }
 }
 
 if (Hour== h && Min== m) {
 tone(6,400,300);}
 delay (500);
 }

 

Include I2C and real time clock library. Define pushbutton and buzzer pin number. Define pushbutton states. Define pin mode for pushbuttons. You can set the correct time from here.

// The following lines can be commented out to use the values already stored in the DS1302
 //rtc.setDOW(SATURDAY); // Set Day-of-Week to FRIDAY
 //rtc.setTime(10, 0, 0); // Set the time to 12:00:00 (24hr format)
 //rtc.setDate(11, 11, 2017); // Set the date to August 6th, 2010

 

If you want to know more about real time clock with Arduino. You can see Arduino clock on my previous tutorial. In loop function, initially we already set activate equal to zero. It means it will show date and time. If we pressed setting button it will activate 1. By this we enter in set alarm mode.  Now we can press button to set hours and minutes for alarm. If hour is greater than 23 it will make hours equal to zero, similarly if minutes is greater than 59 it will make minutes equal to zero. If we press exit button it will activate 0 and show current time. If Hours and Minutes matched with what we set, buzzer make sound through pin number 6 at 400 frequency for 300 millisecond. Now we can see I am setting alarm by pressing button. When it reaches its time buzzer make sound.

 

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2gRFSiv (RTC DS1302)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2vmSK8l  (Resistor)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

TILL THEN KEEP LEARNING KEEP MAKING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *