Sei sulla pagina 1di 5

DIGITAL CLOCK

The Experiment is about creating a simple digital clock using LCD display. The idea is
inspired the way in which a battery operated clock (or analog clock) requires setting of
time when we start using it. Various time settings can be made using the pushbutton
switches.

Hardware
This Experiment needs Application board, LCD display panel and Arduino Uno board.
These are shown in the Figures 1.1, 1.2 and 1.3.

Figure 1.1 Application Board

Figure 1.2 LCD panel


Figure 1.3 Arduino Uno

LCD panel should be connected with the Application board as shown in


the Figure 1.4. The LCD panel with pins is inserted in the socket strip of
Application board.

Figure 1.4 LCD panel connections with Application board

The external power adapter (or insert batteries in the battery holder and connect)
should be used to supply power to Arduino board. This is required as text on LCD
panel may appear dim without external power adapter.
Pin connections shown in Figure 1.5 should be made.
Arduino Pins Application Board Pins

+5V +5V
GND GND
Digital pin 8 SW 1
Digital pin 9 SW 2
Digital pin 10 SW 3
Digital pin 11 EN (E)
Digital pin 12 RS
Digital pin 5 D4
Digital pin 4 D5
Digital pin 3 D6
Digital pin 2 D7

Figure 1.5 Pin connections

Sketch
// Basic Digital Clock

#include <LiquidCrystal.h>
LiquidCrystal lcd ( 12, 11, 5, 4, 3, 2 ) ;
int hr = 0 ;
int mins = 0 ;
int sec = 0 ;
const int hourPin = 8 ;
const int minPin = 9 ;
const int ampmPin = 10 ;
// Flag to set AM or PM
bool ampmFlag = true ;
int switch1 ;
int switch2 ;
int switch3 ;

void setup( )
{
Serial.begin ( 9600 ) ;
lcd.begin ( 16, 2 ) ;
}

void loop( )
{
lcd.setCursor( 0, 0 ) ;
// increment seconds
sec = sec + 1 ;
lcd.print ( "TIME:" ) ;
lcd.print( hr ) ;
lcd.print( ":" ) ;
lcd.print( mins ) ;
lcd.print( ":" ) ;
lcd.print( sec ) ;

// show AM or PM based on flag value


if ( ampmFlag )
lcd.print ( "AM" ) ;
else
lcd.print( "PM" ) ;

delay ( 1000 ) ;
lcd.clear( ) ;

// increment mins
if (sec == 60)
{
sec = 0 ;
mins = mins + 1 ;
}
// increment hour
if(mins == 60)
{
mins = 0 ;
hr = hr + 1 ;
}
if ( hr == 13 )
hr = 0 ; // set to 0

lcd.setCursor ( 0, 1 ) ;
lcd.print ( "SET: SW1,SW2,SW3" ) ;

// Set Time using SW1, SW2, SW3


switch1 = digitalRead ( hourPin ) ;

if ( switch1 == 0 )
{
hr = hr + 1 ;
if ( hr >= 13 )
hr = 0 ;
}
switch2 = digitalRead ( minPin ) ;
if ( switch2 == 0 )
{
sec = 0 ;
mins = mins + 1 ;
}

switch3 = digitalRead ( ampmPin ) ;


if ( switch3 == 0 )
{
ampmFlag = !ampmFlag ;
}
}
Result
The digital clock is shown on LCD panel with Time as 0:0:0 AM.

 Press the SWITCH-1 and hour will change. Hold the switch till the desired hour is
shown on the LCD panel.

 Press the SWITCH-2 and minutes will change. Hold the switch till the desired
minutes is shown on the LCD panel.

 Press the SWITCH-3, it toggles AM / PM. Use this to se AM or PM.

This behavior is inspired when you put a battery to analog clock and you are required to
set the time.

Functions
The important functions used in this Experiment's sketch are given below.

LiquidCrystal begin ( cols, rows )


Initializes the interface to the LCD screen. Specifies the dimensions
(width and height) of the display
begin() needs to be called before any other LCD library commands
cols: the number of columns that the display has
rows: the number of rows that the display has
LiquidCrystal print ( data )
Prints text to the LCD
data: the data to print (char, byte, int, long, or string)
LiquidCrystal clear( )
Clears the LCD screen and positions the cursor in the upper-left
corner
LiquidCrystal setCursor( col, row )
It position’s the LCD cursor. Set the location at which subsequent
text written to the LCD will be displayed
col: column at which to position the cursor (0 is first column)
row: row at which to position the cursor (0 is first row)

Potrebbero piacerti anche