Digital Clock with timer, stop watch and alarm using Arduino UNO

 


This project is built with Arduino UNO as the brain, a DS1307 RTC module to keep track of the time and a DHT11 sensor to get the room temperature and humidity. DS1307 is attached with an external button cell which keeps the time tickling after even the power goes off. And this is the reason why the time is accurate after being powered off in any digital system that tracks records or logs data with time.

This system is having four modes of operation. (Please refer to the circuit diagram strictly recommended)

Default Mode: You may call this the default of the initial screen which displays time, temperature, and humidity by default. And after completing any of the other mode tasks it redirects to this screen. So, this is the default mode or screen.



Alarm Mode: In this mode, the user can set the alarm. Press SW4 to change the mode to SET ALARM. Then press SW1 to change the position of the cursor. Every time you press SW1 it will change the position from the hour to the minute, to the second, and then exit. If you want to change the hour change it directly as it is in the hour by default. Press SW2 to increase the value of that position and SW3 to decrease the value. After setting the desired value press SW1 to exit the setting. Now you can see that you are at the main screen displaying the current time and alarm set time.



Timer Mode: In this mode, the user can set a countdown timer. Again pressing SW4 changes the mode and pressing SW3 will initialize the counter or timer. Pressing SW2 will change the minute section and increase the value. Now press SW4 to start the timer. And then after this, you can see it redirect to the main screen and the timer will start automatically. Press SW4 & SW2 simultaneously to exit from the mode.



Stopwatch Mode: In this mode, the user can start, pause, stop, and reset using this mode. Pressing SW4 will change the mode set to STOPWATCH. Pressing SW3 will initialize the stopwatch. Press SW1 to start, press SW2 to pause, and press SW3 to reset the stopwatch.



Hardware Requirements:

  • Arduino UNO
  • DHT11 sensor
  • RTC module-DC1307
  • 16×2 LCD
  • 10K POT for LCD contrast
  • 5v Buzzer
  • BC 547 transistor
  • 1K ohm resisters -7pcs
  • Push to ON switch-4pcs
  • Voltage regulator -7805
  • LED -5mm RED- 2pcs

Software Requirements:

  • Arduino IDE (You can download it from HERE).
  • Arduino RTC Library (Download it from HERE).
  • LCD Library (inbuilt within the IDE)
  • Wire Library (inbuilt within the IDE)
  • EEPROM Library (inbuilt within the IDE)
  • DHT Library (Download it from HERE).

Block Diagram:



Power Supply Circuit: The whole system works on the 5v DC supply. You can use the external power supply as shown in the circuit diagram. The main purpose is to step down the 12v to 5v DC by using a 7805 voltage regulator which gives you an output of 5v DC. Pin 1 of 7805 is fed with the input voltage of 12v of the power supply and pin 2 of 7805 is a common ground that is connected to the GRND line of the entire circuit. Pin 3 of 7805 gives you the regulated 5v DC which further we will use in our project.

DS1307 RTC module: Please visit this article to know how to interface the DS1307 RTC module with Arduino UNO. As we know the RTC is powered with a built-in button cell which helps to track the real-time. Please refer to the below table.

DS1307 RTCArduino UNO
SDAA4 (SDA)
SCLA5 (SCL)
VCC5v
GNDGND

 

DHT11 Sensor: This is a single-wire communicating digital sensor which is packed with a capacitive humidity sensor and a thermistor that gives the temperature. Follow the below table of connections.

DHT11Arduino UNO
PIN1 (Vcc)A0
PIN2 (OUT)5v
PIN3NC
PIN4 (GND)GND

 

16x2 Alphanumeric LCD: Please follow the below table and make the connection correctly.

16x2 LCDArduino UNO
RS13
RWGRND
E12
D7,D6,D5,D48,9,10,11 respectively

 

Switches Connection:  Here we are using four 1K resistors to provide a default state to the PINs D6, D5, D4, and D3 and that is HIGH. This means we have connected the PINS to VCC through a 1K resistor. By doing this we are setting the PINS state to HIGH as a default value. And when the press the switch it goes to a LOW value and will be detected at the PIN hence we get to know the switch is being pressed.  Follow the below table and connect the switches accordingly.

SWITCHESArduino UNO
SW1D6
SW2D5
SW3D4
SW4D3

 

Buzzer Connection: The buzzer is connected to PIN 7 of Arduino through a common emitter NPN transistor (BC547). The base of the BC547 is connected to a 1K ohm resistor and one end of the resistor is connected to PIN 7 of Arduino. The emitter of BC547 is connected to a common GND. The Collector of BC547 is connected to the negative terminal of the buzzer whereas the positive terminal of the buzzer is connected to a 5v DC supply.

How Circuit works: When the circuit initializes it enters the default mode with some initial messages while fetching the current time, temperature and humidity. As soon as it enters it starts displaying the current time, temperature, and humidity on a 16x2 LCD.

 

Now user can change the mode as per the requirement by pressing the SW4 and changing the mode. Following the mode of operation explained above in this article, the user can test its respective modes.

Circuit Diagram: Please follow the circuit diagram and the below table to build the connection correctly.



Full Source Code

#include 
#include 
#include 
#include
#include 
 
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
RTC_DS1307 RTC;
int temp,inc,hours1,minut,addR=11,temp1=3,mode=0;
int tSeconds=0, tMinutes=0, hours=0;  
//this line, along with another line in void timerFunction(), is where you can adjust the amount of time that is counted down in                                                                    //the timer function
int centiseconds=0, sSeconds=0, sMinutes=0;
int next=6;    
int INC=5;
int set_mad=4;
int shw_dat=3;
int buzzer=7;
int HOUR, MINUT, SECOND=0;
int tempe=0, humi=0;
dht DHT;

#define DHT11_PIN A0

void setup(){

   Wire.begin();
   RTC.begin();
   lcd.begin(16,2);
   
   pinMode(INC, INPUT);
   pinMode(next, INPUT);
   pinMode(set_mad, INPUT);
   pinMode(shw_dat, INPUT);
   pinMode(buzzer, OUTPUT);
   digitalWrite(next, HIGH);
   digitalWrite(set_mad, HIGH);
   digitalWrite(INC, HIGH);
   digitalWrite(shw_dat, HIGH);
 
   lcd.setCursor(0,0);
   lcd.print("RTC BASED ALARM ");
   lcd.setCursor(0,1);
   lcd.print("YT CHANNEL *MES*");
   delay(2000);  
   lcd.setCursor(0,0);
   lcd.print("      TIMER     ");
   delay(2000);  
   lcd.setCursor(0,0);
   lcd.print("   STOP WATCH   ");
   delay(5000);
   if(!RTC.isrunning())
	{
	 RTC.adjust(DateTime(__DATE__,__TIME__));
	}
}

void loop()
{
  
  int temp=0,val=1,temp4;
  DateTime now = RTC.now();
  DHT.read11(DHT11_PIN);
  humi = DHT.humidity;
  tempe =  DHT.temperature;
   
  if(digitalRead(shw_dat) == 0)
   {mode++;}

  if(digitalRead(shw_dat) == 0 && mode==1)      //set Alarm time
   { 
     lcd.setCursor(0,0);
     lcd.print("   SET ALARM    ");
     lcd.setCursor(0,1);
     lcd.print("                ");
    delay(2000);
    //defualt();int temp,inc,hours1,minut,add=11;
   while(1)
   {   
    
    if(digitalRead(set_mad) == 0)
    {
    alarm();
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" ALARM TIME SET ");
     lcd.setCursor(9,1);
     lcd.print(HOUR=hours1,DEC); 
     lcd.print(":");
     lcd.print(MINUT=minut,DEC);
     lcd.print(":");
     lcd.print(SECOND=now.second(),DEC);
     CheckTime();
     delay(1000);
    }
    if(digitalRead(INC) == 0)
    break;
   }
   }

 if(digitalRead(shw_dat) == 0 && mode==3)      //set Timer
   {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    SET TIMER     ");
    while(1)
    {
    if(digitalRead(set_mad) == 0)
    {
    timerFunction();
    }
   if(digitalRead(INC) == 0)
   break;
    }
   }
if(digitalRead(shw_dat) == 0 && mode==4)      //set Stopwatch
   {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("    STOPWATCH     ");
    
    while(1)
    {
    if(digitalRead(set_mad) == 0)
    {
    stopwatchFunction();
    }
   if(digitalRead(INC) == 0)
   break;
    }
   
    }
 if(mode>=5)
    {mode=0;}
 lcd.clear();
 lcd.setCursor(0,0);
 printDigits(HOUR=now.hour()); 
 lcd.print(":");
 printDigits(MINUT=now.minute());
 lcd.print(":");
 printDigits(SECOND=now.second());
 lcd.setCursor(0,1);
 printDigits(now.day());
 lcd.print("/");
 printDigits(now.month());
 lcd.print("/");
 lcd.print(now.year(),DEC);
  lcd.setCursor(10,0); 
  lcd.print("T:");
  lcd.print(tempe);
  lcd.print((char)223);
  lcd.setCursor(15,0); 
  lcd.print("C");
  lcd.setCursor(11,1);
  lcd.print("H:");
  lcd.print(humi);
  lcd.print("%");
  
  delay(1000);
}
/*Function to set alarm time and feed time into Internal eeprom*/
void alarm()                             
{
  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(addR++,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(addR++, minut);
       temp=0;
       while(digitalRead(next)==0);
      }
     
    }
    delay(1000);
}
/* Function to check medication time */
void CheckTime()
{
  int tem[17];
  while(1)
  {
    DateTime now = RTC.now();
  lcd.setCursor(0,1);
 lcd.print(HOUR=now.hour(),DEC); 
 lcd.print(":");
 lcd.print(MINUT=now.minute(),DEC);
 lcd.print(":");
 lcd.print(SECOND=now.second(),DEC);
  for(int i=11;i<17;i++)
  {
    tem[i]=EEPROM.read(i);
  }
  if(HOUR == tem[11] && MINUT == tem[12]) 
  {
   for(int j=0;j<5;j++)
   {
    digitalWrite(buzzer, HIGH);
    delay(500);
    digitalWrite(buzzer, LOW);
    delay(500);
    
    }
   hours1=0;
    minut=0;
    addR=11;
    return;
    
   }
 }
}
void timerFunction()  //the timer function was made with the help of this post: http://pastebin.com/f57045830
{
  int set=0;
    lcd.setCursor(4,1);
    //lcd.setCursor(0, 1);
    lcd.print("00:00:00");
  while(1)
  {


    while(digitalRead(shw_dat)==1)
    {
     set=1;
     if(digitalRead(set_mad)==0)  //if "Start/Stop" is pressed, the timer counts down
      {
       tSeconds++;
       lcdOutput();
       delay(300);
       if(tSeconds==60)
        {
         tMinutes++;
         tSeconds=0;
        }
        }
      if(digitalRead(INC)==0)  //if "Start/Stop" is pressed, the timer counts down
       {
        tMinutes++;
        lcdOutput();
        delay(300);
        if(tMinutes==60)
         {
          hours++;
          tMinutes=0;
         }
        }
      if(digitalRead(next)==0 )  //if "Start/Stop" is pressed, the timer counts down
       {
        hours++;
        lcdOutput();
        delay(300);
        if(hours==24)
         {
          hours=0;
         }
        }}
  if(digitalRead(shw_dat)==0 && set==1)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    //lcd.setCursor(0, 1);
    lcd.print(" TIMER SET FOR  ");
    lcd.setCursor(4,1);
    //lcd.setCursor(0, 1);
    lcd.print("00:00:00");
            while(digitalRead(INC)==1)
            {
            static unsigned long lastTick = 0;
              if (tSeconds > 0)
                {
                if (millis() - lastTick >= 1000)
                {
                lastTick = millis();
                tSeconds--;
                lcdOutput();
                }
                }
            if (tMinutes > 0)
               {
              if (tSeconds <= 0)
              {
                tMinutes--;
                tSeconds = 60;
              }
              }
            if (hours > 0)
              {
              if (tMinutes <= 0)
              {
                hours--;
                  tMinutes = 60;
                }
              } 
            
              if(hours == 00 && tMinutes == 00 && tSeconds == 00)  //when timer ends, the alarm goes on
                {
             // while(digitalRead(shw_dat) == 1)  //the alarm will only go off until "Restart" is pressed
             // {
                set=2;
                lcd.setCursor(4, 1);
                lcd.print("00:00:00");
                  for(int i=0;i<5;i++)
                  {
                  digitalWrite(buzzer, HIGH);
                  delay(500);
                  digitalWrite(buzzer, LOW);
                  delay(500);
                  }
               // }
                }
                
           }
  }
  if(digitalRead(shw_dat) == 0)
               {
                set=0;
                mode=0;
                break;
                } 
  }
  
  }

void lcdOutput()  //this is just used to display the timer on the LCD
{
  lcd.setCursor(4, 1);
  printDigits(hours);
  lcd.setCursor(7, 1);
  printDigits(tMinutes);
  lcd.setCursor(10, 1);
  printDigits(tSeconds);
  delay(100);
}

void printDigits(int digits)  //this void function is really useful; it adds a "0" to the //beginning of the number, so that 5 minutes is displayed as "00:05:00", rather than "00:5 :00"
{
  if(digits < 10) 
  {
    lcd.print("0");
    lcd.print(digits);
  }
  else
  {
    lcd.print(digits);
  }
}
void stopwatchFunction()
{
 int count=1,sMin,sSec,sCen;
 
    lcd.setCursor(4,1);
    //lcd.setCursor(0, 1);
    lcd.print("00:00:00");
    
  while(1)
  {
  if(digitalRead(shw_dat) == LOW )
    {
      count=0;
      loop();
     
    }    
  if(digitalRead(next) == LOW)  //if the "Start/Stop" button is pressed, the time begins running
  {
    
  while(1)
    {
      int count=1;
      delay(6);
  lcd.setCursor(4, 1);
  printDigits(sMinutes);
  lcd.setCursor(7, 1);
  printDigits(sSeconds);
  lcd.setCursor(10, 1);
  printDigits(centiseconds);
    centiseconds++;
    sCen=centiseconds;
    if(centiseconds==100)
    {
      sSeconds++;
      sSec=sSeconds;
      centiseconds=0;
      if(sSeconds==60)
      {
        sMinutes++;
        sMin=sMinutes;
        sSeconds=0;        
        }
    }
    if(digitalRead(set_mad) == 0)
      {
        centiseconds = 0;
        sSeconds = 0;
        sMinutes = 0;
        break;
      }
if(digitalRead(INC) == LOW && count ==1)
    {
      while(1)
      {
     lcd.setCursor(4, 1);
  printDigits(sMinutes);
  lcd.setCursor(7, 1);
  printDigits(sSeconds);
  lcd.setCursor(10, 1);
  printDigits(centiseconds); 
     
      if(digitalRead(set_mad) == 0)
      {
        count=2;
        centiseconds = 0;
        sSeconds = 0;
        sMinutes = 0;
        break;
      }
      }
      }
      
      }
    }
  }
}

Youtube video tutorial:

 


Post a Comment

0 Comments