Arduino Clock Project for Ahmed

Introduction: Arduino Clock Project for Ahmed

This is not an Instructable how to recreate the clock project that got Ahmed Mohamed in trouble in September 2015, nor do I want this to be a discussion about what happened. What I do want to do is lay out a simple clock project using an Arduino to help inspire people like Ahmed to keep hacking, making, and programming.

At it's most fundamental, a digital clock is a computer with input and output and with behavior controlled through logic. Using an Arduino, we can program and test the behaviors step by step. This can lead us in to some insights as to why the basic digital clock hasn't changed in several decades. It can also let us creatively explore some alternatives.

Summary of Steps:

Using Arduino's internal counter to keep time.

Adding visual output using a 7-segment display.

Adding an alarm using a piezo speaker.

Using a button for input to silence the alarm.

Adding a real-time clock (RTC) for accuracy.

Adding battery power.

Parts & Supplies:

All you really need to start you exploration is an Arduino. However, to complete this project, some other optional items are needed:

When I tackle a complex project, I like to break it into discrete steps based on the functionality and wiring. Then, when something goes wrong, I can always fall back to the previous "known good" state to help figure out what went wrong.

We will start by just writing some simple code to handle seconds, minutes, and hours using the Arduino's internal counter which tracks the time, in milliseconds, since the board was powered-on or reset.

Here is our starting sketch, with no libraries or wiring. Time is simply displayed using the serial console:

Step 2: Adding Some Output - 7 Segment Display

Seeing time click off on the serial console is cool, but hey, you already have an accurate digital clock down in the corner of your computer screen. We need some real output, so I'm going to add a 7-segment display. Adafruit and SparkFun have a number of different display options, but I'm going to use Adafruit's I2C one (https://www.adafruit.com/products/881).

Both the display and the RTC module (Step 5) have software libraries that must be installed to give us the functionality to easily program them. Libraries abstract all the low-level hardware code so we can just focus on the task at hand.

Depending on your start time, you may just see "0 00". Pretty boring. If you want to see some number move, try substituting in the second digits (s1, s2) for the minutes digits (m1, m2) for digits # 3 & 4.

Also, most digital clocks give the indication of seconds passing by flashing the colon in between hours and minutes. You can do that using a modulo test on seconds to flash on if it is odd, off if it is even. Try adding:

matrix.drawColon(seconds % 2);<br>

With the RTC library available (even without the actual module installed), we can use some tricks to get the time the sketch was saved and set the time.

Wow. That sounds just like the clock next to my bed. See how I'm using modulo again to alternate the speaker on and off. My clock by my bed also beeps in one second intervals and will alarm for a minute if not reset or "snoozed". Coincidence?

Time to explore:

Change the pitch from 880. Can you come up with a pattern or play notes?

Add the Digital Pin 13 LED in the party. You could use it as a visual indication of alarming.

Step 4: Silence That Alarm. Adding Input With a Button

You usually don't let your alarm clock beep for an entire minute hoping it will shut up. You either hit the snooze button or turn the alarm off. Right now, all we have is the reset button which will silence the alarm, but only for another minute since it will reset the time. Time to add an input button we can use to silence the alarm.

Lets add our button by connecting one side to Analong Pin 0, which we will treat as a Digital Pin, and the other side to Ground.

int buttonPin = A0;

In setup(), we need to set the pin as OUTPUT, and then engage the internal pull-up resistor. This means a digitalRead() will return HIGH (5V) unless pushed.

Step 5: Getting Real With Time - Adding a Real Time Clock

By now, you must realize the limitations of a software simulated clock. How does a watch or phone keep accurate time? First, you need a continuous power supply, and secondly, you need an accurate crystal. A Real Time Clock module takes care of this for us. Once it is set, as long as it's small battery has power, it will keep time accurately to several seconds per day.

Adafruit's RTC module uses the same I2C bus that the display uses. How does this work? Each item has a unique address, and the libraries handle the rest for us.

// Start the Real Time Clock<br> RTC_DS1307 rtc = RTC_DS1307();

In setup(), we need to check if the clock is running and set if it is not. Then we can initialize the variable using the current time.

Step 6: Going Portable - Adding Battery Power

So by now, we have a fully functioning clock. If you are like Ahmed, and want to take it somewhere to show someone, we are going to need a bigger battery than the one keeping the RTC alive. Arduinos thrive on 5V, and can run as low as 3V given the right conditions. Unfortunately, the Adafruit RTC needs a minimum of 4.5V to operate correctly, so we need something regulated correctly and not just hit-n-miss with for instance 3xAA.

There are a number of options, but they all add cost. A recharable lithium-ion battery requires a boost circuit to get to 5V and a recharging circuit, so we would need something like adafruit.com/products/1944. I'm trying to keep this simple, so I'm going to opt for 4XAA fed to the Arduino's Vin, letting it handle the 5V regulation. The holder is cheap, as are the batteries.

Time to Explore:

How much current is your Arduino drawing with and without the display running?

How long would you expect your batteries to last.

What could you do to lower the current draw and extend the run time?

Step 7: Packing It Up - Adding a Project Box

You have accurate time, a clock that behaves the way your programmed it, and it is now battery powered. It is time for the final step: putting it in a project box.

An enclosure can be just about anything, and instead of buying something off the shelf, I like to look through thrift-stores or keep my eye on the recycle bin. For this project, I wanted something with a clear lid so people could see the inner-workings and not be worried that it had a nefarious purpose.

I cut openings for USB and external power to the Arduino. I hot glued the Arduino, breadboard, and battery pack to the bottom after roughing it up with some sand paper. I then marked and cut openings in the lid to hot glue the various components in place. Even if you have followed this Instructable to the letter, here is your chance to make it a creation all your own! I'd love to see photos if you do.

A couple of notes to help you out:

Male-female jumpers work great for getting the 7-segment display off the breadboard.

Solid core hookup wire can help making the longer connections so that you can still open the lid to work on it.

Heat shrink makes things look professional and helps avoid shorts.

Step 8: Some Final Thoughts

Our Arduino turned in to a pretty expensive clock, but the fact that you made it and programmed it makes it priceless. And, you can always incorporate the parts into your next, bigger and better project!

If you still have clocks on the brain, here are some interesting clock projects with unique displays:

Comments

Thanks for the great instructable. Have you considered the millis() function rollover? The millis() function keeps time as an unsigned long variable. The range is 0-4,294,967,295. To convert that to days, we can divide by 1000, 60, 60, and 24. The result is 49.71, so the variable should fill up after about 50 days. Your RTC module seems to fix that, but it's interesting to consider what happens after the unsigned long fills up.