Instructables
This Instructable will show you how to build an binary alarm clock, with a touch sensor snooze button. This is my first instructable and my first real arduino project, I hope you like it!

I bought an arduino a while back and I think it's really nice, but I haven't really done anything useful with it yet, so now I thought it was time to make a bigger project. I decided to do an alarm clock since my old one is broken.

Here's a video of it in action:



There seems to be some problems with the video embedding,  If you can't see the video above, here's a link to it: binary alarm clock
 
Remove these adsRemove these ads by Signing Up

Step 1: What should be done?

To help me organize my thoughts about how I should build the clock I wrote down what I think an alarm clock should be able to do. I then tried to build the clock to meet the requirements.



An alarm clock should be able to:                 What I used:
- keep time                                                          - code
- display time                                                      - LEDs 5 for hours, 6 for minutes
- keep an alarm time                                         - code
- let the user set the time                                  - buttons
- let the user set the alarm                               - buttons
- make noise when the alarm goes off.         - speaker
- let the user snooze                                          - touch sensor "button"                     
- let the user turn on and off the alarm.          - touch sensor "button"

It would be nice if it also:
- looks reasonably good                                  - a nice box as case
- is cool and geeky                                           -  true binary display
holidayv3 months ago

this is a great instructable! Very well documented, with exception that a schematic is missing, which makes it hard to understand how you hooked up the buttons. I was wondering, how would you modify the code for a momentary switch for the snooze. I'm building this in an old alarm clock housing that has the snooze switch built into the feet of the clock. A reply back would be greatly appreciated.

grayconstruct9 months ago
This could be useful to detect individual differences from crystal to crystal, however, a crystal oscillator is affected by environmental factors as well. Mainly temperature, power supply ripples, even age of the crystal :(
I found this informative article http://kunz-pc.sce.carleton.ca/thesis/CrystalOscillators.pdf if you want super details. My latest arduino clock uses the DS3231SN chip (ChronoDot RTC product) which (in reasonable temperatures) is accurate to better than one minute per year drift. And the battery backup means you don't have to set the clock every time you loose power.
fNX2 years ago
Really great project! Thanks a lot, this one inspired me to do my pocket binary alarm clock !
2011-10-30-002449.jpg
njakol (author)  fNX1 year ago
Cool!
hi,
when i go to upload to my arduino and it verifys it first it comes up with an error saying 'CapSense' does not name a type. i am only trying to upload the binary_clock_v1 file am i doing something wrong or missing something?
njakol (author)  juniormachinist1 year ago
Do you have the CapSense library?

"Step 13: Upload the arduino sketch.

Make shure you have the CapSense Library and the Tone Library, they can be downloaded here, and here.
Unzip and place the folders in the folder libraries in your sketchbook folder. "
jlateur2 years ago
Works like a charm.
Where are the wires from the buttons going? Great project!
njakol (author)  analog.motion3 years ago
To the arduino, look at the 5th picture in step 14.
From step 3:
"Here are the pins I used for the different parts:

- 2 and 4: Touchsensor (4 is the send pin, 2 is recieve)
- 6 Hour button
- 5 Minute button
- 7 Switch
- 14-18 Display hour pins
- 8-13 Display minute pins"
how can I not use the touch sensitive on/off/snooze feature? I am pretty new to electronics, so any help would be great.

Thank!, and great instructable
jolijar4 years ago
Where did you get that box from?
njakol (author)  jolijar4 years ago
I got it when I bought a tie + napkin + cufflink kit. The little window was so you could see the color of the tie.
Laserman5954 years ago
When ever you do find out please post it so it would be possible to  compensate for it every 6 or so months.  =-)
njakol (author)  Laserman5954 years ago
 I'm not shure what you mean, but if you mean the clock error, then I have as you can read already compensated for it in the code, the discussion below is just about other possible ways to do it. I have however not tested a very long run of the clock (several weeks) without resetting yet because I have had some problems with the touchsensor being oversensitive and after a couple of days trigger alarm on/off constantly. I think I may have to ground the arduino better, the capsense page say that it's important.
0jack4 years ago
I just want to say how lovely this is, both the clock (which is aesthetically pleasing and clever), and the instructable. I'm a writer for my day job and I do textile crafting for my hobbies (sewing, knitting, spinning, etc.) but I actually understood what you were doing here, which means you did a great job.  I used to know a little C++ and this and that, when I was way, way younger, and I followed along the code bits you show surprisingly well, too, because you were so clear what it was all about.

I've always wanted to learn this kind of thing but don't know a single soul who knows much beyond not using the wrong adaptor for the printer (and some don't know that much!).  The more instructables I read, though, the more convinced I am that I'll have to make one, some day.  It's getting harder to resist.  :) 

njakol (author)  0jack4 years ago
Thank you for the kind comment, I'm glad you liked it!

I didn't know anyone that could help me much when I started learning about electronics etc. either, but the internet is full of information and you can ask at forums if you get stuck.
daltore4 years ago
Have you thought about using ternary operators?  They're basically if() statements, but use a slightly lower-level structure.  I'm not sure if they run faster under the Arduino, they may actually end up compiled the same since it's an embedded environment, but basically, it goes like this:

condition ? run if true : otherwise run this;

So you could do that first if() statement like this:

current_millis_value < previous_millis_value ? m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value : m += current_millis_value - previous_millis_value;

It's not as pretty, but as always, C ignores whitespace, so you can write it like this if you want to organize it a little:

current_millis_value < previous_millis_value ?
m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value :
m += current_millis_value - previous_millis_value;

It might run faster and eliminate some of the timing error.

Also, you might be able to spread out the time correction a little more evenly than 2 seconds every 5 hours.  Since it's .4 seconds, that's 400 milliseconds every 60 minutes.  That goes down to 20 milliseconds every 3 minutes.  Since you're counting milliseconds directly, and the overall count of seconds is a multiple of 3, it might be overall a little more accurate to the user to do the correction more often.

Great tutorial though, I might have to try this!
njakol (author)  daltore4 years ago
No I haven't thought about using ternary operators there. However I think if-statemets may be a bit easier to read. Maybe one should do a test of ifs vs. ternary, but I think they are equally fast.

About the time compensation, yes I have thought about that and maybe I will add it later. Since the clock only shows minutes I don't think it matter that much. The advantage with that method is that it's easier to compensate for cases when you have for example 0.43 instead.

Hmm, I somehow thought It would be more complicated to compensate the milliseconds, but now when I think about it, it should be as easy, one just have to make the millis variable signed, so the subtraction won't cause it to underflow.

Thanks for the suggestions, I'm glad you like the instructable!
daltore njakol4 years ago
Leave it unsigned.  You could just as easily have it count up to 1001 instead of 999.  This would save issues of the clock wondering what time it really is.

Yeah, I'll have to see how ternary operators and if() statements compare when I finally get an Arduino (Christmas is coming, as my plan currently stands).  For a lot of embedded environments, all those conditional structures get compiled down the same way anyway.  I think I recall that the C18 compiler (for PIC microcontrollers, like the one in my VEX robot) compiles if() and switch() statements to the same thing.  I wouldn't be surprised if it takes while() loops down to if()-goto structures.  Or I guess I could just look at the avr-gcc manual, but that would be too easy :D

I think I might eventually try this project, I've wanted a binary clock for a while, and I love the AVR line.  One thing that I found last night is that the Arduino bootloader still allows you to use the lower-level avr-gcc commands.  It turns out that digitalWrite() takes about 10 times longer to run than accessing the PORT register directly, because with each call of digitalWrite(), it turns off all of the PWM timers (and in the comments in the code, the guy is asking why that couldn't be moved up to pinMode(), which is interesting).  Basically, digitalWrite() is just some safety mechanisms and a parser for the PORT register.  It sets the PORT of your choice (lets say pin 13, which is under PORT B, pin 5) to an entire byte all at once (you can toggle 8 pins just about simultaneously).  This would work like this, IIRC:

PORT_B = 0b00010000;

Or, to only affect that pin, you can use the | operator:

PORT_B |= 0b00010000;

You don't have to make these modifications, I realize it would take a long time to recalibrate, just thought you might be interested.  I still love the tutorial.  I think it might also be interesting to make a multiplexed or charlieplexed version, just for funsies.
njakol (author)  daltore4 years ago
"Leave it unsigned.  You could just as easily have it count up to 1001 instead of 999.  This would save issues of the clock wondering what time it really is."

Wouldn't that just work if you want to correct at least 1 millisecond every second (60ms per minute) wich is more than I want to?

"[...]
It turns out that digitalWrite() takes about 10 times longer to run than accessing the PORT register directly, because with each call of digitalWrite(), it turns off all of the PWM timers (and in the comments in the code, the guy is asking why that couldn't be moved up to pinMode(), which is interesting). 
PORT_B = 0b00010000;
[...]"


Actually there have been much discussion about how to make digitalWrite faster on the arduino developers mailinglist lately (link), maybe digitalWrite compiled to one instruction will come in the next version of the arduino ide.
I think I'll just wait for that since using digitalWrite makes the code cleaner and easier to understand*, and It works  good as it is. I don't have much time now anyway to tinker with the clock, because I have calculus exam soon :S.

* = for me at least, I'm not used to the PORT stuff since I have mostly used the arduino. I would like to learn more how microcontrollers really work sometime though. The arduino hides all complexity, which is really nice, and makes it easy to make stuff with it,  but it would be intresting to learn how it works low-level.

"You don't have to make these modifications, I realize it would take a long time to recalibrate"

I don't think I would have to recallibrate, the time error should come from the crystal oscillator frequency and not from how long it takes to execute the loop.

"I think it might also be interesting to make a multiplexed or charlieplexed version, just for funsies."


Yes, I would like to experiment with charlieplexing too sometime.
daltore njakol4 years ago
Oh, yeah, with the "1001 instead of 999" thing, I was thinking 2 milliseconds instead of 20.  And actually, you could tack on 7 milliseconds every minute, and then 6 every 3 minutes, like so:

if(seconds==59 && m > (!minutes%3 ? 999+6 : 999+7)) {  //If divisible by 3, add
    seconds++;                                                                              //18 ms, otherwise 19
    m = 0;
}
else if(m>999) {  //Normally, a second is just 1000ms
    seconds++;
    m = 0;
}

I think how you do it with the 2 seconds every 5 hours is fine by the way, this is more just a point of discussion.
Just wondering, why didn't you use a RTC for timekeeping? Nice guide btw.

-Simon
njakol (author)  simonrichards1504 years ago
I didn't use one because I didn't have one, and it's cheaper to use just code :) but maybe I will add it in a later version if I can't get the clock to be as exact as I want, but It seems to work OK without for now.
cyrozap njakol4 years ago
Yeah, it'll start drifting by a lot after a while. You should use this. I read that it is very good. You can even get one that is the electrical equivalent, but the headers are soldered on the wrong way, for half of the price.
njakol (author)  cyrozap4 years ago
It will drift a bit, but as you can see in the instructable I have compansated for the drift I could measure in the code so I don't think it will be more than a couple of minutes per year. I'll have to wait and see I guess.
 This is so cool! Thanks for posting this, this is amazing. I definitely like this binary clock system rather than Binary Coded Decimals.
njakol (author)  hollenback.c4 years ago
Thank you! I'm glad that you liked it.