Quantcast

How To Build a Custom Timer Clock and Learn Arduino In The Process

By Alex Castle

Still daunted by the prospect of starting your own Arduino project? Alex walks you through a very simple clock timer project, from start to finish. You'll learn about how to connect an Arduino board to an LCD and plastic housing, and then program it using sample code. Trust us, it's really not that difficult!

I’ve written a couple of times about Arduino on Tested—first to explain the platform from a top-down perspective, and then to examine the individual boards that are on the market. Before moving on to other topics, I want to take a step-by-step look at a single Arduino project, from start to finish. By doing this, I hope to show just how easy it is to get started with the platform.

But what to build? I wanted the project to be as simple as possible, because I knew that I would want to explain the whole thing, including the code, in a single article. I also wanted to avoid any specialized hardware, since I don’t want to get bogged down describing how to incorporate a breathalizer into your Arduino project, when that’s not going to be relevant for 99% of the stuff you might want to try at home.

With that in mind, I decided on a chess clock. It’s the simplest thing I could think of—just two buttons and a screen—that I could actually get some practical usage out of. Yeah, I’m sure there’s a dozen iPhone apps that’ll do the same job, but nothing’s as fun (and as deliciously nerdy) as an Arduino-based do-it-yourself solution.

Part 1: Preproduction

Like with any project, a little bit of planning will make your Arduino build go a lot smoother. A simple diagram is often all you need. Here’s mine for the chess clock:

Obviously not a work of art. But by quickly drawing it out I was able to figure out exactly what I’ll need for the build. First, I need an Arduino board. I already have an Uno R3, so I’m just going to stick with that and save some money, but you could use a Leonardo just as well. A more powerful Arduino board like the Due would be wasted here, and a smaller board like the Micro wouldn’t work because I plan to use a shield.

For the display, I’ll be using an LCD shield—an add-on board that mounts directly onto the top of the Arduino Uno. It’s perfectly possible to incorporate an LCD screen into your project without using a shield, but the shield streamlines the process, is really easy to reuse in other projects, and ties up fewer of the Arduino’s I/O pins than an LCD display by itself would. I bought the 16x2 RGB LCD shield kit from Adafruit, which comes with some onboard controls. I won’t need the controls for this project, but I could imagine using them in the future.

Finally, I’ll need some buttons, wires, and a box. I have plenty of the first two lying around (of course, if you don’t you can order them online for next to nothing), and I bought a lovely plastic box to build the project in. All the parts cost well under $100, and you can shop around for the best prices for Arduino components. Now to put it all together:

Part 2: Assembly

Three days later, the parts have made their way all the way out here to Honolulu, and it’s time to get down to business.

First, I’ll need to put together the LCD shield kit. It comes completely disassembled, so you have to solder the LCD and the assorted chips, resistors and switches to the PCB. You can sometimes find pre-assembled shields, but the DIY kits don’t take long to build, and are a fantastic way to practice your through-hole soldering—an important skill for anyone getting into electronics.

Through-hole soldering is the use of solder to fix chips and other components into the little metal-plated holes (called through-holes) in printed circuit boards. Like with any other form of soldering, the main trick is to keep the soldering iron’s tip tinned (coated in solder), and to make sure you’re heating up the elements to be soldered (the trough-hole, and the component’s lead or wire) rather than heating the solder directly. Once the through-hole and lead are heated, you just briefly touch the solder to the joint. It will melt and flow into the hole. Afterwards, make sure to trim any excess length of the lead hanging off the bottom of the PCB, so it doesn’t short anything out.

With the shield assembled and mounted to the Arduino Uno, I try to fit everything into the box. Although the box I bought is advertised as fitting an Arduino, the LCD shield is slightly larger, and doesn’t quite fit. I’ve never let a little ABS plastic stop me, though—I grab my trusty Dremel rotary tool and carve out a little extra room where I needed it.

Part 3: Coding

If you’ve done any programming before, working with Arduino should be a piece of cake.

Depending on your background, this can be the easiest or the hardest part of the process. If you’ve done any programming before, working with Arduino should be a piece of cake. The Arduino IDE comes with a simple editor that allows you to write programs in C++, and the programs (or sketches, as the Arduino IDE calls them) tend to be on the very simple end of the spectrum.

If you haven’t programmed before, I’m not going to try and teach you how in this article—it’s too muchg to cover here, and there are a lot of great resources for that all over the web. I will, however, attempt to go through the process of writing an Arduino sketch in a way that’s clear enough that you should be able to understand what’s going on, even if you don’t understand the specific syntax stuff that comes with programming in C++.

(For information about installing the Arduino driver, IDE, and add-on libraries, see my original guide to Arduino.)

To get started writing the Arduino sketch for the chess clock, I’m going to try to loosely describe what the program will do. First, in plain English—how do we want our chess clock behave? I’d describe it like this:

“The chess clock maintains two timers. One timer is always ticking down. At any time, a player may press their button to cause the opponent’s timer to stop ticking down, and theirs to stop. When a timer reaches zero, a ‘LOSE’ message is displayed, and all other functionality stops until the timer is reset. The timer may be reset at any point by pressing both buttons.”

If we can agree on this basic functionality for a chess clock, we’ll move on to the pseudocode stage.

Here, we’ll boil the above description down to a series of actions and IF (conditional) statements that will go into the loop() function. All the actions in loop() are executed, in order, and when the end of the function is reached, it starts over again. It might sound a little complicated if you’ve never programmed. But when you look at the pseudocode, it should make sense. It looks like this:

IF the reset button combination is pressed

  • Reset the game

IF the game is over

  • Don’t do anything else

IF the change turn button is pressed

  • Change which player is currently having their turn

IF it’s the first player’s turn

  • Update player 1’s clock, and check to see if he’s out of time and the game is over.

OTHERWISE

  • Update player 2’s clock, and check to see if he’s out of time and the game is over.

Seems pretty simple, right? If you just keep looping through those instructions, you’ve got a functioning chess clock. Actually writing it into code is a little more complicated, but not by too much. Most Arduino sketches use a functional programming model, where related chunks of code are organized into functions that can be called from anywhere else in the code. So, a first pass at the sketch, with some chunks called out as separate functions might look like this:

This looks a little more complicated than the pseudocode at first blush, but as long as you understand a couple simple syntax issues, it’s actually very much the same! Any line that starts with two dashes (“//”) is a comment and doesn’t have any effect on the code. Those comments describe how the code matches to the steps described in the pseudocode.

Once you’ve got the program in this state, you simply have to figure out the particulars of what each function needs to do. For example, the UpdatePlayer1Time() function looks like this:

Even if you’ve never programmed before in your life, you could learn what you need to start writing Arduino sketches in an afternoon.

(Download the full code here)

Again, a little daunting at first, but if you check the comments, there’s nothing too arcane going on. We’re manipulating a set of variables that keep track of how much time the player has left. We’re accessing built-in function in the Arduino IDE, and also in the library we downloaded for the LCD, in order to access Arduino’s built-in clock and to tell the LCD screen what it should display.

A lot of Arduino sketches are about this simple—keeping track of some data in variables, and using functions from other libraries to talk to the hardware you’ve connected to your project. Even if you’ve never programmed before in your life, you could learn what you need to start writing Arduino sketches in an afternoon.

While I’m doing the actual programming, I keep the Arduino hooked up to a little breadboard, so I can make sure the circuit does what I think it does before I actually start installing everything into the box. A breadboard is a plastic grid that makes it easy to prototype circuits without actually soldering anything.

After ironing a couple of bugs out of the code, I’m good to go, so I solder the buttons into the circuit, and install the Arduino into the box. And just like that, we’ve got a functioning chess clock:

Not the fanciest chess clock in the world, but that’s sort of the point. There are tons of ways you could improve the clock from here—you could add functionality in the software to allow the player to set a custom time limit, or to keep track of match victories. You could improve the hardware by adding bigger buttons or a battery pack. Hell, you could add the aforementioned breathalyzer hardware and handicap a player’s time accordingly.

Hopefully this article has shown how fast and simple it is to get from an idea to a functioning prototype using Arduino. Even if you’re new to electronics, programming or both, it’s a great way to learn.