Binary clock

Discussion in 'The Projects Forum' started by McZlik, Jul 11, 2012.

  1. McZlik

    Thread Starter New Member

    Jun 9, 2012
    Hi all,

    Once again I'm back with an LED and a clock project.
    This time a binary one, controlled with Arduino.
    It's already built into a nice small mint-tin, powered by a 9V battery.

    • Mint-tin
    • ATMega 328P-PU
    • 9v battery + flexible connector
    • 13 LEDs
    • 13 200 ohm resistors
    • 1 10K ohm resistor
    • 2 10 uF caps
    • 2 22pF caps
    • an 7805 voltage regulator
    • 16Mhz crystal
    • 3 momentary switches (open)
    • loads of cable
    • perfboard

    I've included the circuit, don't have a picture of it yet. (I'm at work currently)

    I'm still fine-tuning the program, I currently have a minute off every hour :eek:
    It's counting to slow, but I think I know where the problem is.
    Once I got it out, I'm planning to post it in the finished projects section.

    Is there any way to calculate how long it can run on a battery?
    Besides just plain trying it :p

    Let me know what you peeps think of it, and if I can improve something for the next time =]

  2. mcasale

    Active Member

    Jul 18, 2011
    You probably don't need to run the LED current through the 7805 regulator. It's just wasting power. However, the outputs of the MPU would need to be tolerant of 9V, which it's probably not.

    You might consider an external buffer chip (probably CMOS) to drive the LEDs. It's a trade off between battery life and cost and space.
  3. MrChips


    Oct 2, 2009
    Use a 32768Hz xtal for low power operation and real-time clock time base.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    It doesn't matter if you burn power inside the 7805 or inside a dropping resistor when running the LEDs direct from the battery. Power is being converted to heat either way.

    Most of that heat is going into driving the LEDs. To reduce the power necessary run the micro off the lowest voltage possible and then also use that voltage for the LEDs. 3.3V would be nice for red LEDs, may not be enough for other colors. Then use a switcher for the voltage regulator so you burn off the least power.

    As to how long the battery will work, that depends on the load, which is mostly the LEDs, and how may LEDs are on is always changing. So either count how many LEDs are on for every hour then for every minute (you'll already know the first 12 states) and calculate the average. That times each LED current, plus the micro's current is your average load.

    That load comes directly from your battery so you can compute amp-hours and see how that plays with your battery's rating (if it even specs one).

    If you use a switcher then you have to figure out the power to load, divide by the battery voltage and also divide by the switcher's efficiency to get the battery current.
  5. BMorse

    AAC Fanatic!

    Sep 26, 2009
    If you set it up to where the unit uses POV, you can just use 7 I/O's to control 12 LED's..... plus, you can brighten or dim the display by increasing or deceasing the refresh rate....... I used 3 I/O's for controlling the 3 rows, and 4 outputs for controlling each column, by "flashing" the LED's 1 column at a time, and doing it fast enough, the LED's will "appear" lit all the time to the human eye..... which of course consumes less power than having the LED's constantly on.....
  6. takao21203

    AAC Fanatic!

    Apr 28, 2012
    I have built a 5x5 LED matrix (yellow LEDs) which runs weeks from 2.4V

    No resistors but I don't see a danger for LEDs burning out since it runs for 2 or 3 weeks on one charge.

    3V is more than sufficient for all kind of LEDs!

    I also have a small binary clock where apparently I use 2.2K resistors to save battery power. Also runs weeks on a single AAA charge.

    Clocking frequency should be reduced to 500 Khz for an application like that.

    1X AAA battery + MCP1640 would be appreciate for power supply.

    I don't know properly if ATMEL chips can run from 3V or less.

    I'm still fine-tuning the program, I currently have a minute off every hour

    That's bad...for 8 MHz you actually need a complicate algorithm, since it can not be divided down for a timebase. Same for 16 MHz...
  7. McZlik

    Thread Starter New Member

    Jun 9, 2012
    Wow, look at that...
    I know I still have loads to learn, but didn't know it would be this much for such a small project :p

    First of all, I based my board on arduino's standalone project.
    Sow that's why I'm using the 7805 and a 16Mhz xtal.

    For the lower voltages, as far as I know, the ATMega328P-PU has to receive 5V to operate, and outputs 5V on its pins.
    Sow that's the reason for the resistors for the LEDs.
    I don't directly power them from the battery, why do you guys think that? Or did I made a mistake in my drawing?

    About the really bad off-timing for my clock, that's probably because I put a 250MS delay to many in my code :p Not sure, didn't check my code for it yet, but that's my guess.

    For the idea, I got it from this Instructables project. That's also the second reason for my built up.
    Only difference is that I use a 9V power source, and a 328 instead of his 48.

    My LEDs are actually blinking :p I shut them off for every update loop, maybe I should make the off time larger to save a bit more battery?

    Why is it more complicated to built a program for an 8/16 Mhz xtal?
  8. takao21203

    AAC Fanatic!

    Apr 28, 2012
    Because you need an algorithm to get the timebase.

    It can be done, but it is rather sophisticated. I don't have it as Arduino C, only as PIC 16F assembler. Maybe you can follow the logic?

    Code ( (Unknown Language)):
    2. this code is used to drive the clock's 1/50 seconds:
    4.  l_clkdrive:
    5.  incf v_clkdrive0,f
    6.  movlw d'39'
    7.  subwf v_clkdrive0,w
    9.  btfss STATUS,Z
    10.  return
    12.  clrf v_clkdrive0
    13.  call l_clkdrive_5f37
    14.  addlw 0x01
    15.  btfsc STATUS,Z
    16.  incf v_clkdrive1,f
    18.  bsf v_status,c_status_refresh
    19.  movlw d'50'
    20.  subwf v_clkdrive1,w
    21.  btfss STATUS,Z
    22.  return
    24.  bsf v_status,c_status_second_elapsed
    25.  clrf v_clkdrive1
    26.  return
    28.  l_clkdrive_5f37:
    29.  incf v_clkdrive_carry0,f
    30.  btfsc STATUS,Z
    31.  incf v_clkdrive_carry1,f
    33.  btfss v_clkdrive_carry1,d'1'
    34.  retlw 0xff
    36.  movlw d'113'
    37.  subwf v_clkdrive_carry0,w
    38.  btfss STATUS,Z
    39.  retlw 0xff
    41.  clrf v_clkdrive_carry0
    42.  clrf v_clkdrive_carry1
    43.  retlw 0x00
    it is a bit weird since depending on the actual state (or variable values), different values are returned in the W register.
  9. McZlik

    Thread Starter New Member

    Jun 9, 2012
    No sorry, I never used Assembler before.
    I understand that the commands are different depending on the PIC you use, am I right?

    Can you, or anyone else, make pseudo code from it?
    You don't have to reprogram it for me in Arduino C, as long as I can understand what's going on, I can remake it :)

    Thanks in advance!
  10. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    Here is a page showing a number of simple systems to generate exact 1 second periods from any PIC xtal;

    There are C code examples there that people have been able to use on Arduinos. :)
  11. WBahn


    Mar 31, 2012
    It isn't so much that the program is considerably more complicated (though it is a bit), but if you use a 32.768kHz crystal, you gain the following:

    1) Assuming you have a counter that can count at the main clock rate, then a 15-bit counter will overflow once a second. This makes programming easier since you can work with a power of two.

    2) The slower you run the clock, the less power the MCU will consume. To first order, it is linear with frequency. So running at 32kHz instead of 16MHz will reduce the current draw of the MCU by a factor of about 500.

    3) The 32.768kHz crystal is specifically designed for clock applications, thus it is much more accurate than typical crystals and oscillators. Many crystals/oscillators are spec'ed at 50 to 200ppm. At an error 100ppm, the clock will lose/gain about 9 sec/day. Doesn't sound like much, but now consider that this is 4 min/month or almost an hour a year. Few people would tolerate a watch that drifted anywhere near that. My last two watches (both fairly basic Seikos) average about 1 sec/month (they are almost always within 5sec when I change the time for daylight savings time, which is when I resync the seconds). The 32.768kHz watch crystal is commonly spec'ed at 20ppm but most will be much closer than that. At 20ppm you are talking about less than two seconds a day or about five minutes each time daylight savings time changes (and probably quite a bit less).

    4) Because this crystal is standardized across the watch/clock industry and virtually every battery-powered watch made used one, it is widely available at very cheap prices.

    5) Because you are at a much lower frequency, your board and device parasitics will have less of an influence and so your actual oscillation frequency will tend to be much closer to what you expect it to be at.
    Last edited: Jul 14, 2012
  12. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    I'm not arguing with any of WBahn's excellent points but in reference to the link to code that I posted above, the need for a 32.768 xtal is removed as the code uses a long constant to set the period of 1 second, and this can be adjusted in very fine increments less than 1 Part Per Million allowing even a typical micro xtal to provide a VERY finely trimmed clock.

    However watch style 32.768 xtals are usually more stable in frequency over changes in temperature variation.
  13. WBahn


    Mar 31, 2012
    And this is actually an important point that I meant to mention and simply forgot. Most crystals will vary up and down within their spec range, most notably due to temperature, more so than a watch crystal. So, even it had a 20ppm error in it, it will tend to run much closer to having that same error consistently and that allows you to calibrate the clock to that crystal's average error and end up with something that is very close.

    I'm guessing here, but I've always suspected that mass-produced watches do one of two things during the manufacturing process: they either laser trim something to obtain the nominal frequency under set conditions or they program a register (possibly by blowing on-chip fuses) to hard code a calibration constant.
  14. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    A good watch should have a freq trim pot (or cap) and a good watchmaker should adjust it. :)

    I have a decent brand inexpensive watch (think Seiko quality) that after having it's battery replaced went from always having a few seconds a month error to now have maybe 1 second a month error. Very nice!

    I'm just guessing but I believe the watchmaker trimmed the freq when he was replacing the battery, which is interesting to think what kind of equipment (and what period of testing) he needed to get that kind of accuracy?
  15. MrChips


    Oct 2, 2009
    Last edited: Jul 15, 2012
  16. McZlik

    Thread Starter New Member

    Jun 9, 2012
    The last post was a bit off-topic =p

    Anyways, thanks a lot for all the information so far :)
    I can really use this all for my LED analog clock project that I'm designing.

    The_RB, thanks for the link, I will try it out when I've got time to fiddle around with my electronics again :)

    WBahn, thanks for the really clear information :)
    My guess is that the temperature wont be much of an issue, because the clock will sit on my desk.
    One question though, what does ppm mean?

    About the 32.768 xtal, does anybody know if they can be used with arduino chips? (I should google that :p)
    I believe the Arduino chips require and 8/16 Mhz xtal, could be wrong though.
  17. takao21203

    AAC Fanatic!

    Apr 28, 2012
    Most larger PICs can use two crystals, one main crystal, one 32 KHz crystal.
    All PICs also can run from 32 KHz crystal.

    One solution is to use external RTC chip, and use the 512 Hz signal...

    Actually my assembler code is quite counter-intuitive, it is a cycle skipping algorithm. I tried about 1/2 hour to understand it. And I have inserted a battery inside the binary LED clock, and comparing it to my quartz-controlled VFD clock (not self-made)...

    After one hour, the two displays flip over at exactly the same time. So the algorithm is correct!

    It is kind of counting to 39, and each time also count to 625, if that is reached, skip 1/100 seconds increment, and reset the 625 counter.
  18. WBahn


    Mar 31, 2012
    It means parts per million. 1ppm is equal to 1% of 1% of 1%. 1000 ppm is 0.1%.

    So if an oscillator has an error that averages 100ppm, then the accumulated error on a daily basis is (3600sec/hr)(24hr/day)(100/1000000)=8.64sec/day.
  19. takao21203

    AAC Fanatic!

    Apr 28, 2012
    Get the datasheet for the chip that is used. ATMEGA328? I have one small Arduino here...But all this information can be found in the datasheet.

    Page 29! See Attach.