Arduino Project - Digital Clock - APC
Arduino Project - Digital Clock - APC
Arduino Project - Digital Clock - APC
Tweet
Arduino is the worlds favourite microcontroller board and despite its low 16MHz/8-bit computing
power, it can drive robots, connect to Twitter and even power 3D printers and quadcopters.
One of the many reasons for its success is the number of expansion boards or shields available.
Weve already looked at a few including the W5100 Ethernet Shield, along with two motor drive
shields one for DC motors (used in our Rolly Robot project and free-roaming Attacknid hack)
and one for servo motors (powering our Stompy walking robot project). But its the ability to
make Arduino handle human interaction that makes it so versatile.
Weve already looked at how you can connect your Arduino to your TV showing weather
information and playing Space Invaders, but there is a low-cost shield that can display data in a
far more compact way.
One of the most popular shields is the 1602 LCD Shield. You can find clones of these
everywhere on eBay selling for as little as $6 and that includes shipping. The shield itself delivers a smaller daughterboard containing an alphanumeric LCD panel consisting of two lines
with 16 characters per line, typically driven by the popular Hitachi HD44780 LCD controller. Throw in five user-programmable push-button switches plus on-board master reset button and
you have the 1602 LCD Shield.
The shields rear has no components so it cant short against the Arduino board.
These little 162 LCD panels have been around for over 20 years I created a simple project in Silicon Chip magazine back in 1993 that connected one to a PCs parallel printer port.
But its only with the introduction of Arduino that theyve become so ridiculously cheap. And by integrating the LCD panel into a shield, theres no soldering involved you just slot it into
place using the Arduinos Shield header pins.
The LCD panel is backlit, lighting as soon as power is applied, but it can be turned off by programming. While green-backlit panels are common, theres a growing number of blue-backlit
options as well. The six push-button switches are located on the bottom left side with four in a plus-pad arrangement, the outer switch on the left designated as Select and a master-reset
button on the far right. Press the reset button and your sketch will restart. You can adjust the LCD contrast by the small potentiometer (or pot for short) at the top-left. Adjust it so the blank
pixels are just beginning to darken and back it off a little. There are many different brands of 1602 Shield on the market but they all conform to this basic layout.
So not only do you have a compact display device but you can also handle user feedback, which makes this shield an ideal project companion to any compatible Arduino microcontroller
board.
Applications
Again, this is one of those shields whose applications are only limited by your imagination. Normally when demonstrating something for the first time, you do the equivalent of Hello, world
program or sketch, but to show you how the interaction works, we decided to go a bit further by creating our own simple digital clock. Weve taken our clock the extra step by 3D printing a
case designed for this particular shield, complete with push-button actuators.
The push-buttons allow for user feedback, but only use one analog input.
The tiny 10kohm potentiometer (pot) sets the LCD screen contrast.
Libraries
The 1602 LCD Shield relies on two specific software libraries LiquidCrystal and LCDKeypad. The very first thing you have to do is initial the LCD Shield so that the Arduino
microcontroller knows what youre playing with. And its here you may find initial problems. While there are many different 1602 Shield clones available, not all use the same pin
configuration, so the LiquidCrystal library is designed to allow you to set it to your specific shield. You can read about it at arduino.cc/en/Reference/LiquidCrystal. Keeping it short, virtually
all 162 LCD panels can run over a 4-bit data bus with two extra control pins RS (register select) and EN (enable).
If you purchase the super-cheap $6 DFRobot shield (like we did), youll probably find that using pin D8 for RS and pin D9 for EN, along with D4-D7 for the four-bit data bus pins should
work. This is outlined in the code line:
LiquidCrystal lcd(8,9,4,5,6,7);
Once thats done, displaying text on the panel is as simple as setting to cursor position using the setCursor command and printing your text through the print command like this:
lcd.setCursor(0,0);
lcd.print(Darrens little);
lcd.setCursor(3,1);
lcd.print(digital clock);
The cursor position is set by (char, row) with each dimension starting with zero, meaning the top line is row 0 and the bottom line row 1. There are numerous other functions available as
well.
Programming push-buttons
One of the more unusual aspects of this shield is how the buttons are electrically connected to the Arduino. Push-button switches are essentially digital in their actuation theyre either
on or off. However, with five input switches, you could potentially tie up five digital input/output (I/O) lines to handle them. But instead, the switches are all connected to just one of the
analog inputs. When you push one of the buttons, it sets up a specific voltage on the analog input through a resistor network.
Using DFRobots LCDkeypad library, you can check for these inputs to know which button was pressed and perform an appropriate action. Its a little awkward in how it works but using
one input rather than five leaves you more I/O lines for your project. The five analog values are stored in a one-dimension array at the start of the sketch:
int adc_key_val[5] ={ 50, 200, 400, 600, 800 };
We incorporated a common routine used to check the input value to see which button was pressed.
This library allows you to create a repeating interrupt trigger at a program-defined time interval that runs a procedure or function. We use this library to create a repeating interrupt once per
second (1000 milliseconds). Each time it triggers, we run the clockUpdate code function that increments the seconds counter and takes care of the rollover for each new minute, hour,
day, date, month and year.
Setting the time is as simple as using the Up, Down and Select buttons.
However, the backroom code automatically sets the correct options for each time interval for example, you set the month by selecting from January, February and so on; days from
Sunday, Monday etc. Once you set the month, you can choose the correct day, which means the code knows that January has 31 days; April has 30, and so on. You cant select April 31
for example. It also knows the difference between AM and PM and that there can only be 60 minutes in an hour, 60 seconds in a minute. We do this using a series of two-dimensional
character arrays like this:
char day[7][10] = { Sunday ,Monday ,Tuesday ,Wednesday,Thursday ,Friday ,Saturday };
Here, were setting up the day options the array has seven elements, each with 10 characters. Because of the way Arduino works, you must fill the array characters explicitly, which is
the reason for the blank spaces in the smaller day names. If you dont, your sketch wont compile for you.
So overall, this is a more complex example of allowing a choice from a limited number of options and varying the options for subsequent input based on previous selections.
What our sketch doesnt do is take into account leap years well leave that as an exercise for you to do. It also doesnt allow for time modification after the initial setup. To do that, you
have to hit the Reset button and start again.
Remember, the idea here is to show off the LCD Shield as well as show how to code for it into your apps. The rest is up to you.
3D printed case
To finish off our clock, weve had a look on Thingiverse.com and not surprisingly, there are dozens of brackets and cases designed for various members of the Arduino family. We were
expecting to have to buy a small general-purpose ABS plastic Jiffy box from Jaycar Electronics to put our clock in, but someone has designed a case specifically to use this Arduino/Shield
combo, so we printed it out on our Printrbot Jr 3D printer. You can check out the case at www.thingiverse.com/thing:132497.
Installation
You dont need to screw the Arduino/Shield combo into place in the case it will sit in position on its own once the two shell halves come together. The only thing you need to be careful of
is the two retention clips. These can break so dont press the clips press on shell either side of the clips to compress it so the clips will lock into the base. Make sure the button actuators
move cleanly inside their holes too, otherwise, theyll jam up the switches. Use a small wood file to open the holes as necessary just not too much or the actuators will fall out.
The shield slots into the Arduino Uno board, overhanging on one end only.
Power
The clock consumes little power so you can either grab a simple 5VDC/200mA power brick with the right 2.1mm DC plug or you can get a cheap USB phone charger and power the clock
through the USB port. Either way will work just fine.
Modification ideas
The sketch code for this clock we threw together in a day is more a demonstration of how the 1602 Shield works. But if you want to modify it, go right ahead. Improvements we can think of
straight away are:
Our digital clock inside a 3D printed case designed for this shield.
Search Site
Search
Follow us
Recent Comments
Darren Yates on Arduino Masterclass
Part 4: Build a mini robot
Darren Yates on Arduino Project 5:
Digital audio player
http://apcmag.com/arduino-project-digital-clock.htm[31/03/2014 11:14:22 AM]
Magazine Resource
APC Tutorial Files
Arduino Source Codes
Tweets by @APCmag
Vote
View Results Polldaddy.com
Top
Future is AOP & PPA Digital Publisher of the Year and BMA Media Company of the Year.
This siteis part of Future plc, an international media group and leading digital publisher. We produce content across five core areas:
About Future
Jobs
PR
Advertising
Digital Future
Privacy Policy
Cookies Policy
Subscriptions
Investor Relations
Contact Future
Future Publishing (Overseas) Limited, Beauford Court, 30 Monmouth Street, Bath BA1 2BW. All rights reserved. England and Wales company registration number 06202940. Australian Office: Suite 3, Level 10, 100 Walker Street, North Sydney, NSW
2060 T: +61 2 9955 2677