Building an Arduino Clock
My "Fabulous Five" dollhead piece was my first arduino project. It’s purpose was learning how to read sensors, then drive motors using an arduino. To that end, it drives DC hobby motors and servos, as well as using input from 4 sensors, an IR distance sensor, a sonar distance sensor, a PIR motion sensor, and a photoresistor light sensor.
For my second project, I wanted to build something a bit more desk-worthy. When I saw this clock on YouTube, I had my initial plan. Using a 3-axis accelerometer to do clock settings instead of buttons. A new sensor, and a novel way to use it, the project was underway.
How to set the clock? Do I find a way to interface a WWV receiver? Put in wireless network connection to sync with an NTP server? Measuring cost/difficulty/convienience I settled on using a DS1307 Real Time Clock board. With a battery backup it’ll keep time for 7-15 years (then just put in a new battery), and at a street price of $20 for a DS1307, battery and crystal on a board, it seemed like the simplest and cheapest way to go. Move the clock anywhere, plug it in, it’ll instantly show the time. A side benefit of the DS1307 is that it has user-settable registers that are also maintained by the on-board battery, so all the clock settings can be stored there with the main power off.
The display was another quandary. Ideally, I wanted to use large LED numbers to make the clock easy to read, but that makes for a larger case, and with only 4 digits/letters, setting anything more than the time and an alarm is hard to read, and I didn’t want multiple displays, or to require hooking the clock to a computer. I opted with a 2×16 character LCD, easy to hook up, easy to read, and it can display a lot of information.
I picked up a 3-axis accelerometer board from Adafruit based on the ADXL335, another easy to connect $20 part.
I wasn’t sure about the alarm. I worked that out after I built the circuit. To keep trying and learning the characteristics of new components, I hooked up a tri-color LCD, and then for sound used a simple peizo.
As I played with the version wired up on a breadboard, I realized it would be handy to be able to shut the alarm off by using a distance sensor, rather than having to physically tilt the clock, so I added an IR sensor to the mix. I also added a backlight timer at this point to shut off the backlight (nice if this sits on your nightstand), which you can now turn on by just putting your hand near the clock.
I was going to build a wooden box to put the clock into. While cleaning out some old boxes, I came across an old box of Legos (actually an original Lego Mindstorm). Wondering if I could make a fun enclosure out of that, I tried, and succeeded. I quite like the clock incased in legos, it’s a whimsical finishing touch.
Parts list:
I got all the parts from my two favorites parts suppliers: Sparkfun electronics, and Adafruit industries
- LCD Display
- 3-axis accelerometer ADXL335
- Real Time Clock DS1307
- IR Distance sensor
- Project board
- tri-color LED
- Piezo buzzer
- Arduino Duemilanove
Source code:
Prototype on the breadboard:
Project board wired up with real time clock:
Board with LCD attached and LED alarm going off:
Side view, put in its lego enclosure:
Lessons learned:
It was the first time I used a peizo. It wasn’t until after I’d soldered everything up and gotten it running that I realized if I had the peizo hooked up to one of the PWM capable pins of the Arduino, I could play notes, instead of just a single loud tone. I’d hooked up the tri-color LCD and the backlight of the LCD to PWM pins so I could run them at different intensities, but I had other pins I could have used. Unfortunately, the project board can’t handle a lot of unsoldering before the copper pads detach, and I didn’t want to risk ruining the entire working clock by moving two wires. Given the LCD is just used to blend the colors, but blink at mostly full intensity, I really didn’t need to hook it up to the limited PWM pins, given how I ended up using it. Oh well. Next my piezo based project (which I’m working on now!) will play music.
As expected, tuning the accelerometer is one of the harder parts of the project, and it changed slightly each stage as the board moved to a different resting position. If you build one, the first things you’ll want to do is to just run a loop displaying the x/y/z values in real time, and then writing down the values to figure out what roll right/left/forward/back correspond to. Also, the output is variable, based on input voltage, so if you don’t have a stable voltage input (for instance, running the board of a 9V battery), it may change over time. I added a calibration routine to set get the values for "centered".
A note of caution, if you look at my code, it’s based on the direction my accelerometer board was put onto the circuit board. Yours will almost certainly be different, and thus you’ll have to calculate your own values. I did make them all variables, so hopefully it’s easy enough to change it around.
I had hoped to run the entire thing using an arduino pro board and a battery. After getting the board, I realized it didn’t have any of the power circuits of a duemilanove, and as a result, the only power available was the same as the input voltage. That was a problem. The battery output was nominally 3.7V, but actually put out about 4.5V fully charged. The ADXL335 is low voltage, and could handle no more than 3.7V, and I didn’t want to burn that out, and the DS1307 needed 5V to go into read/write mode, which the battery would never produce.
I could have built two regulated power supplies for the 3.3V and 5V needed, but that added more complexity than I wanted to put into this first clock project. I could just add a minty boost to get the 5V, but also needed to get a regulated 3.3V. I decided that would be something I’d work on later after I’d spent some time with the clock, and just went with a duemilanove board.
The clock will suck a 9V battery dry in 48 hours or less with the IR sensor turned off, and the LCD backlight off. I believe I can double that by adding more power saving features, shutting off the LCD and the clock and putting the board to sleep. The accelerometer uses hardly any power at all, so keeping it powered, and using it to "wake up" the clock when in power save mode is a reasonable option.
Clocks are cheap, this one, not so much. This was a really fun project to build, and I learned quite about about a variety of things doing it. New sensors, building a prototype board. But, the parts cost was relatively high when you put it all together, a bit over $100, in fact. Adafruit makes a way cooler looking clock, the Ice Tube Clock. A more sane project, if what you really are looking for is a nice looking alarm clock, instead of a learning project. I built one of those too, and it was a lot of fun.
Nov 04 2009