Tweaking a 32.768 mhz crystal

cachomachine

Senior Member
Is it possible to tweak the frequency of a 32.768 MHZ crystal?

I have a DS1307 chip connected to a Picaxe 18M2, I display the result on a 2X16 blue parallel LCD display using the 4 bits method.

Works fine except that the the DS1307 is advancing aprox. 1 minute every week.

Can i do something to tweak the 32.768 MHZ crystal so my timing is more acurate?
 

techElder

Well-known member
Yes you can! I've done it before! So I finally get to add something real to this forum. Golleegee ...

You take the "can" off first. Then you make an elaborate "clamp" out of material that will not affect the capacitance around the crystal element.

Then you basically use this elaborate clamping mechanism to "squeeze" the crystal element ever so slightly without making it stop oscillating.

This way your crystal changes frequency ever so slightly.

See how easy that is?

PS. Yes, we really did that in a company I once worked for. Part of vibration analysis if I remember correctly. I was only a technician, so I wasn't privy to many details, but it did happen.
 

MartinM57

Moderator
You mean 32.768KHz crystal :)

1 minute is 60 seconds in 604,800 seconds so about 100ppm (parts per million)

The timekeeping accuracy is mainly determined by the crystal accuracy, but physical circuit layout and temperature can also have an effect.

You should be using at least no worse than a 20ppm crystal - so what crystal are you using, what does it's data sheet/purchase info say?

With 20ppm you should be seeing circa 12 seconds/week worse case. You might be lucky and get one that is spot on.

For 1-2 seconds per week across a reasonable temp range you should be looking at DS3231/2 or similar - but much more expensive and not in a friendly DIP package

You might be able to "drag" the frequency of your current crystal with a very small capacitor (no more than 10's of pFs) on one leg, but that's a bit of a bodge to solve the root cause problem
 

westaust55

Moderator
Are you using a 32.768 kHz crystal with 12 pF capacitance?

From the Maxim DS1307 datasheet:
The internal oscillator circuitry is designed for operation with a crystal having a specified load capacitance (CL) of 12.5pF.
X1 is the input to the oscillator and can optionally be connected to an external 32.768kHz oscillator.
Some other time keeping devices eg 1-Wire DS1417 require 32.768 kHz crystal with 6 pF capacitance.
Connections for a standard 32.768kHz quartz crystal, EPSON part number C-002RX or C-004R (be sure to request 6pF load capacitance).
 

srnet

Senior Member
I would concur with the comments about using the 'right' crystal.

If timing accuracy to a minute or so a week is enough, and it often is, then I am happy to use the cheapest watch crystal I can find, about 10p on eBay last time I looked.

If you want better accuracy, then choose a decent crystal for the job, and match the load capacitance to the device ........
 

cachomachine

Senior Member
Your right 32.768 KHZ, my mistake.

All i know about the crystal I bought it at my local electronic store is that it is a 32.768 KHZ crystal(no data sheet)

Matin, where do I connect the small 10 pf capacitor? across both crystal legs?

Texas, I have a small hemostat that I can clamp on the crystal to see what happens! (Are you serious?)

The present environement is 23° C steady (Aprox 74° F)


I can also use a 'manual' method. (read,subtract and write back in my program)

I'll let you know about my findings.
 

hippy

Technical Support
Staff member
As well as needing to be the appropriate type of crystal, long leads, long tracks, stray capacitance and signal crosstalk may affect crystal accuracy. On a breadboard the accuracy is likely to be worse than on veroboard or stripboard, and that can be worse than on a PCB. Most DS1307 datasheets includes recommendations on PCB layout.

You may be able to tweak the frequency of the crystal by adding resistors or capacitors but that should generally be unnecessary and I have no experience in doing that.
 

westaust55

Moderator
I would start with an adjustable/trimmer capacitor 0 - ~12 pF
or something like a 1 or 2 pF fixed value and gradually work up.

Connect between pin 1 = X1 to Gnd on the DS1307
 

mrburnette

Senior Member
The present environement is 23° C steady (Aprox 74° F)
With a reasonably constant 23° C ambient temperature, it may be just as easy as correcting in software... my last cheap crystal was 3 seconds ever 24 hours so every night I had the software adjust -3 seconds. Since we are dealing with integers, there is obvious errors that will creep over time, but in 90 days, I am +20 seconds off... a vast improvement and since I am keeping track of days, I can easily adjust in that section of code, too. Of course, move this to another area with a different temperature and all bets are off!

- Ray
 

hippy

Technical Support
Staff member
Good point about simply correcting the error in software if it is reasonably constant and consistent.

It's reported some digital watches keep more accurate time when worn as it keeps the crystal at a more consistent temperature, a primitive 'crystal oven'. That can also be achieved by tying the crystal to a resistor so heating of that keeps the crystal at a more constant temperature, but will likely eat up current. Won't really help if ambient temperature is stable however.
 

geezer88

Senior Member
I would go with padding the crystal with a small variable capacitor. This technique was frequently used to tweek crystals frequency by small amounts. With a variable cap you can adjust to suit by watching the gain/loss over time, or if you know someone that has a laboratory grade frequency counter, you could adjust it right on in a few minutes. Here's an article on crystals that will probably tell you more than you really want to know:

http://www.raltron.com/cust/tools/appnotes/Crystal Mini Primer - Part 1.pdf

tom
 

mrburnette

Senior Member
I would go with padding the crystal with a small variable capacitor. [/url]
<...>
tom
Yep, but you still have the ambient temperature issue... with the software adjustment, one could in theory develop an algorithm (gosh, I love algorithms!) and simply measure the ambient temperature using the embedded readinternaltemp temp instruction.
So, if said clock was moved from one location to another and the delta-T created a significant change in time-keeping, the gain/loss could be normalized to a 12/24 hour window and an adjustment applied conditionally with IF or SELECT statements based on the PICAXE temperature... Thus, keep your crystal and the PICAXE at the same temperature and the software is far more flexible as it will automatically revert from location 2 to location 1 based only on the software. Essentially, there is no finite limit to the number of conditions that could be applied simply with this technique... and thus the owner could leave that little plastic adjustment tool in the pocket-protector :)

- Ray
 

MartinM57

Moderator
http://www.picaxe.com/BASIC-Commands/Advanced-PICAXE-Configuration/readinternaltemp/

This command is used to provide an indicator of the internal temperature of the chip. It is designed to be used as a cooling failure warning threshold device, not an accurate temperature sensor! For accuracy use a DS18B20 sensor and the readtemp command instead.

..and you would have to find out what the drift with temperature is so that you can put the appropriate control in the algorithm. It's not impossible, but....
 

cachomachine

Senior Member
@Texas
The clamping method is working but a lot too much, i have reset the chip to the original time at 7:35 this morning and clamped the crystal with a small pair of hemostat clamped to the first teeth.(lesser pressure i can)
It is now 13:52 and the time is already 54 secondes too slow.
now i will unclamp it and see if it returns to the its faster pace.
 

srnet

Senior Member
Bizzare.

No doubt you can invent all sorts of clever software adjustment mechanisms to compensate for crystal frequency drift, but just think about the time and effort required in testing to ensure that at some point in the future the clever routines dont scarmble the clock.

Myself, I would start by spending a £1 or so on a decent crystal to see if it workes better.
 

John West

Senior Member
If it's only advancing a minute a week I'd probably add a bit of ground-plane beneath the xtal if it doesn't have any now. That will slow it down a touch.
 

westaust55

Moderator
While performing a time correction in software can certainly be done, a downside is that the correction is only being applied while the PICAXE is powered up. We use RTC chips such as the DS1307/DS1338 with back-up battery capabilities so that the RTC will keep functioning while the microcontroller is de-energised.

A work around to accommodate microcontroller down time, at the expense of additional program space, is to have the PICAXE log to EEPROM the time when it last performed a correction and then when powered up read back the time of last correction, determine how many correction time intervals (eg days) have passed and undertake a &#8220;bulk&#8221; correction to get back on time.
Thus for the smallest code, a hardware approach (first the right class/quality of crystal, second option hardware trimming) is the better approach.
 

mrburnette

Senior Member
<...>
Thus for the smallest code, a hardware approach (first the right class/quality of crystal, second option hardware trimming) is the better approach.
LOL... of course, if the grade is given on having the least (shortest, fewest instructions) then the hardware solution would obviously be the desired approach... of course, I never worry about using software, in the appropriate measure, to perform a necessary or needed task ;)

You guys make it difficult to be a devil's advocate here on the forum...
But, I'm going to keep the old C-clamp in the tool box and continue to use software mainly because I'm pigheaded.

- Ray

Graphic of hardware device to adjust crystal frequency:
ThumbScrew.jpg
 

mrburnette

Senior Member
Bizzare.

No doubt you can invent all sorts of clever software adjustment mechanisms to compensate for crystal frequency drift, but just think about the time and effort required in testing to ensure that at some point in the future the clever routines dont scarmble the clock.

Myself, I would start by spending a £1 or so on a decent crystal to see if it workes better.
Yes... but in the context of the original author's notes, the clock is in a temperature controlled room. The clock is happy where it is as is the owner but said clock has a fairly obvious crystal drift which makes the clock noticeably "off" during a specific timed period. Unless this clock grows legs and start to roam as dogs will do, I do not think that even spending a quid for a new crystal is warranted. It's just drift and is "fixable" in software to the limits that the PICAXE integer routines will permit in some fixed time period.

If we are talking about a lab standard, I'm in full agreement with all of the non-software fixes with the possible exception of physical torture to the crystal or crystal can which is prohibited under the Geneva Convention.

- Ray
 

hippy

Technical Support
Staff member
I do not think that even spending a quid for a new crystal is warranted.
I agree with all you say except for that. It would be worth it if that puts the drift into acceptable bounds and alleviates having to do software adjustment. Just a reduction in drift may make a software fix easier to implement.
 

fernando_g

Senior Member
Something that has not been discussed in this thread is that, if you will be attempting to adjust the frequency, you must have a reference measurement device whose accuracy is at least an order of magnitude better than the one you are adjusting..

This means that simple scope or multimeter readings will not do, unless said piece of equipment has been calibrated against yet another, higher accuracy instrument.

Having said that, you can still accomplish this the long way: by any means software, hardware-mechanical- tweak the frequency a little, and wait at least a full day to see if your adjustment has improved -or worsened- the accuracy.
Of course, this trial and error procedure will take several days to fully accomplish, but available high accuracy time references are everywhere, your mobile phone is one of them.
 

mrburnette

Senior Member
I agree with all you say except for that. It would be worth it if that puts the drift into acceptable bounds and alleviates having to do software adjustment. Just a reduction in drift may make a software fix easier to implement.
OK... I take it back... I really want to be hitting 100% with you guys
...and what the heck... it only a quid Guv'nor :eek:

Say, what is the price of a pint of Guinness these days? It has been far too long since I spent time in the UK (Bath... came in by rail from London to tour ... think the pub wan near the old Roman bath... anyway, very nice until wife developed headache and I had to track down a chemist. Need to take another trip up to the Highlands and Aberdeen to Crathes castle ... there are these pastries to die for in city centre in Stirling!)

So the moral of this thread is to buy your crystals from a decent supplier with appropriate ppm specs for your application so you will not get stuck into buying individual crystals after-the-fact - and missing out on that quantity discount.

- Ray

PS: @Hippy: even cheap crystals need a good home...
 

premelec

Senior Member
A way I have used to zero in a time reference can be done if you have a receiver for WWV - WWVB or other such station - you feed that into the vertical of an oscilloscope and use the local oscillator to trigger the sweep - and watch which way the clock ticks on the trace from the receiver drift - and adjust for no drift. I have leaded and etched crystals in the past as well as using a thumb screw on an FT243 case to adjust crystal holder pressure - and frequency - however these methods are passe' :)
 

srnet

Senior Member
Unless this clock grows legs and start to roam as dogs will do, I do not think that even spending a quid for a new crystal is warranted. It's just drift and is "fixable" in software to the limits that the PICAXE integer routines will permit in some fixed time period
Fair enough, remember that however clever you think the software correction is, at some point in the future the hidden bug could surface ...........
 

hippy

Technical Support
Staff member
available high accuracy time references are everywhere, your mobile phone is one of them.
But be careful with anything that is not permanently synchronised to a time reference. I've picked up quite few radio controlled 'atomic clocks' and it's interesting to observe how out of sync they get.

The main problem is likely that they individually sync to the reference only once a day or so and then rely on their own time keeping which may not be stellar because it doesn't need to be as a sync every day or week keeps it good enough for human use.

Of the five I have next to me; three are within a second of each other, one is three seconds behind, the other is lagging by a full 15 seconds.

"A man with two watches never knows what time it is".

One handy source for time references in the UK is a Sky Digibox. The RS232 port on the back puts out programme and other information including time. Unfortunately you cant control the box through that port, but you can pull the time from the data. I haven't tried that, don't know how accurate it is, but it looks possible with a PC or PICAXE X2. Protocol details on t'web including here -

http://www.heenan.me.uk/control-sky-from-pc/gnome-protocol.html
 

fernando_g

Senior Member
"A man with two watches never knows what time it is".

Wise words indeed, hippy.

I like premelec's suggestion to use a National Frequency Standard's broadcast signal as a reference.
 

Dippy

Moderator
"A man with two watches never knows what time it is".
- Yeah, but on the whole he has a better idea than a man with no watches :)

The same could apply to a man with 50 watches or no watch. Wise not, but if you say it with a Philosopher's voice it sounds good ;)

"A man with a thousand watches should open a shop".

And not so wise (and useless) as: "Even a stopped clock tells the right time twice a day."

All very tung in cheeck (I'm using Westy's auto spell checker).
 
Top