Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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
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 ) ;
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" ) ;
if ( switch1 == 0 )
{
hr = hr + 1 ;
if ( hr >= 13 )
hr = 0 ;
}
switch2 = digitalRead ( minPin ) ;
if ( switch2 == 0 )
{
sec = 0 ;
mins = mins + 1 ;
}
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.
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.