View Full Version : Corrections for internal clock drift
Jeremy Leach
23-01-2006, 10:13
I've been looking into using an LM331 voltage to frequency converter following other posts about measuring LDR readings. This looks an excellent way of getting precise readings by inputting to the PICAXE and using the count command.
However one thing that bothers me is that the PICAXE18X internal clock isn't as precise as an external resonator/crystal. So even if the input frequency from the LM331 was precisely fixed the count command could give different results as the measurement period could vary slightly with variations of the internal clock frequency.
(Question 1: How much drift in the clock can one expect?)
So I've thought up this simple idea to detect clock drift, that I'm hoping could be used to apply a correction to the Count values and therefore more accurate readings of the voltage (LDR or whatever) being measured. It's not too complicated, and I think it should work. I'm just putting it forward for any comment/ suggestions ...
<code><pre><font size=2>
PICAXE OUT>----.
|
.-------|
| |
| |
--.-- .|.
/ \ D1 | |
/___\ | |R1
| |_|
| | |\ | D2
|_______|__________| \|__________>PICAXE ADC INPUT
| | /| | (the same PICAXE)
| |/ | |
--^-- C1 --^-- C2
_____ _____
| |
| |
GND_______|___________________|_
</font></pre></code>
Operation of the circuit is as follows:
A PICAXE output pin is set to generate a PWM signal using PWMout command, the pulse width being fixed. C1 charges (well, ignoring C2 for the moment) with a rate determined by the values of R1 and C1. The rate of charge is carefully set so that C1 has charged to roughly half of the supply voltage by the end of one input pulse. Between pulses C1 discharges rapidly through D1.
D2 and C2 act as a smoothing filter. C2 discharges very slowly through the PICAXE ADC input. The voltage across C2 (VOut) will therefore precisely track (less the diode forward voltage drop) the peak voltage across C1, which is set by the pulse width, and the values of C1 and R1. Since the pulse width depends on the internal clock, so will VOut.
In practice I can see that it's important that all components have very good tolerances.
To get a rough idea of how accurate this circuit might be: Suppose the PICAXE clock frequency is exactly 4MHz and all values are set so that the charge cycle of C1 raises the voltage across C1 to half the supply in the duration of the pulse. Half the supply is a ADC reading of 128. If the internal clock increases by 1% the pulse width will increase by 1% and Vout will increase by 1% (assuming a linear ramp charge rate of C1, which is roughly true), or an ADC reading of 129.28, rounded to 129. So this could monitor the clock frequency to an accuracy of around 1%.
If this isn't all bonkers then I think a lookup table could then be used (or some simple formula perhaps) to apply corrections to input frequencies.
I haven't worked out component and Pulse values yet. Note this all assumes a well regulted supply.
Any comments?!
Edited by - jeremy leach on 1/23/2006 9:15:33 AM
Edited by - Jeremy Leach on 1/23/2006 9:36:52 AM
BeanieBots
23-01-2006, 10:58
Hmm, an interesting idea which looks plausible in theory. However, I'd question it in practice. As you say, it does assume an accurate stable power supply. It also assumes constant volt drop across the diodes. The one I'd be more concerned about is the effect of PICAXE output impedance. At the sort of currents you would require to acheive the correct charge rate, the PICAXE output impedance will become a significant part of the equation. I have absolutely no idea how stable it might be, particularly with temperature and possibly even as a function of the actual PWM value.
Also be a little cautious about the characteristics of the analog input. It is not a simple resistive load. In a DC capacity, you are loading it with the leakage resistance of a capacitor. This will be comparable in maginitude to its own impedance. It could even be current source depending on what is connected to the other inputs and actually charge the capacitor even when there is no PWM signal.
Without spending weeks getting all the exact characteritics of each component and calculating their effect, build it and find out. The theory looks sound but the proof is in testing. Particularly with respect to temperature.
Then again, the drift is almost solely down to temperature (ignoring supply changes) so a simple temperature compensation might be an easier option.
Michael 2727
23-01-2006, 11:01
Has anyone thought of trying another device like
a phototransistor on the input,?
I have quite a few I may experiment if I get some
spare time.
Edited by - Michael 2727 on 1/23/2006 10:05:26 AM
frankeltham
23-01-2006, 11:09
Just wondering if you'd tried it?
Michael 2727
23-01-2006, 11:16
No, not yet, and it's too hot here now, I may
take a look in the morning.
I suppose I should beg, borrow or steal a decent Light Meter and make a good job of it
Edited by - Michael 2727 on 1/23/2006 10:19:00 AM
frankeltham
23-01-2006, 11:54
Oh.
Is the PIC internal oscillator that bad?
And I was just wondering, on your circuit:
Won't the voltage at your smoothing capacitor be a bit wobbley? How would you know if the ADC was actually measuring the voltage peaks as opposed to taking a sample somewhere on the slope?
Could be a job for Mr SillyScope.
How about using a precision F-V convertor or an F-VRMS?
My lightmeter cost about £100 and claims 5% accuracy, so I wouldn't worry too much. It uses a photodiode. (I don't hink you'd get much joy with a photo-transistor and LDRs are so non-linear and slow at sarker levels).
frankeltham
23-01-2006, 11:56
Jeez. My spellings getting as bad as everyone else's.
I meant 'think' not 'hink'. And 'darker' not 'sarker'.
Michael 2727
23-01-2006, 12:15
I think the object of this exercise was to
try to get a linear response or better than
standard from an LDR.
I just wondered if using a phototransistor
would give any better results.
I have a heap of old Fairchild devices that
Dick Smith's used to sell, long since extinct as far as I know, but something similar
may be around and may or may not produce a
more linear reading,,?
BeanieBots
23-01-2006, 12:36
Good point, the response of a photovoltaic device (which I think a photo diode is) will give a current linearly proportional to the energy (light level?) falling on it. Thus, a current amplifier should be linear.
At the end of the day, no matter what the process, you are attemting to produce a 16-bit A2D. By any method, that is quite a task if you want the reading to actually represent the measured quantity and not just be a value that looks like it changes with what is being measured.
frankeltham
23-01-2006, 12:47
Why a phototransistor? (I merely ask for my own education).
Check out the curves on phototransistor vs photodiode.
The PhotoD is much more appropriate. You would have petty limited light range response with a photoT and the temperature effects are pretty large vs photodiode.
A couple of random examples.
Fairchild PhotoT
http://www.farnell.com/datasheets/70522.pdf
and PhotoDiode
http://www.farnell.com/datasheets/70723.pdf
I understand the wishes, but won't your calculations for linearising anything be more prone to mathematical error than temp effects on a PIC oscillator?
(When I say 'error' I mean mathematical uncertainty rather than cock-up).
(However, I DO appreciate your plan concerning compensation. I'm not poo-pooing it at all.)
And as someone said earlier, it may be beneficial to put thought into compensating the sensor rather than the PICAXE.
All* expensive calibrated light meters use photodiodes... why's that then?
*I've never seen an exception on machines post 1903.
Jeremy Leach
23-01-2006, 13:06
Thanks for the comments. I wasn't actually thinking just about LDRs. I'm currently using an MPX4115 pressure sensor interfaced to a 12 bit MP3201 ADC interfacing to my PICAXE, just to try to gain a couple more bits of resolution. I was thinking maybe using a LM331 was a far better way to go, but the possible drift in the PICAXE18X clock makes me doubt it's worth it unless it can be corrected somehow.
I don't know how much drift to expect in a PICAXE clock - I seem to remember it being mentioned but can't find it.
Beaniebots - if it is solely dependant on temperature then it might be simple to compensate.
Another, perhaps overkill, approach would be to have a separate crystal and do a count from that as a reference. Seems a bit overboard though, and the whole idea was to have a simple method of correcting for drift.
BeanieBots
23-01-2006, 13:57
What about driving the LDR with constant current? Easily done with a single op-amp which at the same time would convert to volts. As for compensating the 18X, I think you would be hard pushed to find a simpler/cheaper alternative than to use a 28X.
If you don't mind the time, please try your suggested circuit. I'd be very curious to know the REAL results and what has the most significant effect on it.
Jeremy Leach
23-01-2006, 15:01
Unfortunately not got a great deal of time to experiment at the moment.
Right - got another idea that eliminates the need to make adjustments for the PICAXE clock drift.
At the input to the LM331 have a 'switch' to force the input to a known fixed voltage. This will then force a precise frequency from the LM331 which can be read as a reference frequency by the PICAXE using Count.
The switch can be a PICAXE output with a diode that pulls the input to the LM331 high:
<code><pre><font size=2>
PICAXE 'switch' Output
_|_
\ /
--v--
|
Sensor input>----50K--.--50K---->LM331Input
</font></pre></code>
So to accurately make a reading:
Step 1: Pull PICAXE switch output low (so sensor input voltage is applied to LM331). Count SensorCount pulses in set duration using Count command.
Step 2: Pull PICAXE switch output high to force input to LM331 high and generate set frequency. Count RefCount pulses in same set duration.
Calculate the precise sensor voltage = SensorCount/RefCount * VSupply
Edited by - Jeremy Leach on 1/23/2006 2:04:53 PM
BeanieBots
23-01-2006, 15:22
Yes, except there is still a diode in circuit with what is trying to be a 16 bit reading.
Good idea though. Self calibration.
Perhaps using analog switches and taking into account their internal resistance which is typically in the order of 100R.
Why not use the LM331 itself by using a temperature stable precision resistor for reference. That way you can compensate for other errors in the LM331 circuit and the Pixaxe clock.
Michael 2727
23-01-2006, 22:31
Frank it doesn't nessesaraly have to be a phototransistor, thats just what I had to
play with at the time.
Anything, transistor, diode, LDR, even an LED has some photoelectric properties, that
works and can give a better linear response
would be useful addition to the cause.
Although rarely used and overlooked the BASE
pin of many phototransistors is still
connected to the device, I thought it may
have been an easy way of controlling the gain
with just a resistor or two therefore eliminating
the need for any OP Amp etc.
Edited by - Michael 2727 on 1/23/2006 9:39:05 PM
Jeremy Leach
24-01-2006, 08:27
Beaniebots <i>except there is still a diode in circuit with what is trying to be a 16 bit reading </i>
So is the problem that the diode forward voltage drop will vary with temperature slightly? I know there's a very small reverse bias current that varies with temp.
I appreciate what you're saying about 16 bit readings - it is asking a lot to get this sort of precision. Actually, maybe an issue with using a LM331 is that it requires a stable supply and regulators may not be stable enough?
Anyway....another idea to provide a fixed reference voltage under the control of a picaxe output:
<code><pre><font size=2>
+V -----------
|
| SENSOR OUTPUT>-----.
| |
| |
--.-- --.--
| | | |
| |10k | |
| | | |100K
| | | |
|___| |___|
| LDR |
| .--------. |
.----------| |--------.----->INPUT TO LM331
| .--------.
| ^^^^
--.-- |\ |
| | ----| >|-------
| |10k |/ |
| | PICAXE DRIVEN LED
| |
|___|
|
|
GND---------------------
</font></pre></code>
I know analogue switches have been mentioned, but I like the idea of trying to provide this switched reference without additional ICs (actually I'm trying to cram a new solution onto my existing board !).
This idea uses a PICAXE output to drive an LED physically next to a miniature LDR. Put both in a black, sealed tube. Use a bright, perhaps daylight, LED. Maybe when LED is on the resistance of the LDR will be around 100 Ohms, and dark around 10 Meg ?? So in effect it's a (relatively slow acting, required quite a bit of power to turn on) switch.
The advantage I can see is that it's a switch with no diode-like voltage drop and it can be placed anywhere in a circuit.
Circuit now operates as follows:
Turning the PICAXE output on, lights the LED, makes the LDR resistance approx 100 Ohm.
100 Ohm is pretty insignificant compared to other values, and so it effectively connects the input of the LM331 to the potential divider of the two 10K resistors. 10K is just a suggestion, low values will ensure fixed ref voltage, although need to watch the supply current.
When the PICAXE output is off the LDR resistance will be very high and the switch effectively off.
To be honest I'm not convinced myself ...but it's an idea !
Edited by - Jeremy Leach on 1/24/2006 7:29:46 AM
Jeremy Leach
24-01-2006, 09:09
As an aside ....in the interests of board space ...has anyone ever tried wrapping coil wire arounda miniature reed switch to make a miniature relay?
BeanieBots
24-01-2006, 09:15
Jeremy, Stop.
You are moving from plausable to bad to dreadfull.
Many of the ideas are fine purely in theory with perfect components but in practice you are better without them. There are several problems with the latest idea. The most significant is that of LED ageing. Although not very noticeable to our logrithmic responding eyes, an LEDs output reduces dramatically with age, especially early on. You can even see the difference after a few weeks. Try it. Get two LEDs of identical make and colour. Put them next to each other and make a note of how close their intensity. Then leave just one on at max current for a few weeks and then test again. (it's part of the spec that manufacturers always leave out).
The physical arrangement would also have to be extremely robust to avoid movement.
I think you should take a step back and go with something like the original idea proposed by Hippy. Namely, two different drive currents for light and dark.
Although I know you are not trying to read the light level to 16-bit accuracy, you ARE trying to interpolate small changes in a specific area which requires you to read to 16-bit accuracy. Unless your circuit and components are up to 16-bit, you will be no better off than using the original resolution. In fact it will be worse, because you will get a number that you THINK is valid but it has changed due to reasons other than light level.
Think of it like taking a picture of someone holding a newspaper. If you want to zoom in (on the taken picture) and read the print, the whole picture must be of the required resolution. The only other way is to zoom in on the paper before taking the picture. You cannot take a wide angle picture and zoom in post event without a high quality camera.
Jeremy Leach
24-01-2006, 11:17
I value your opinion Beaniebots, and yes, I think I'm wandering off too far into the land of theory !
I think your suggestion to use a PICAXE28X was a good one, if I'm trying to use an LM331, then I'd have the benefit of the crystal clock and all the worries about clock drift would disappear. I still like the idea of using an LM331 because I'd imagine it's still possible to get better results than the the standard ADC input, even if nowhere near 16 bit resolution.
One issue with the 28x solution though is that my design benefits from two smaller parallel running PICAXEs. So I might just save these ideas for a future project.
A problem I find is that there are just too many design options !
Anyway, thanks for the input. BTW the LED ageing info is interesting.
Edited by - Jeremy Leach on 1/24/2006 10:20:24 AM
BeanieBots
24-01-2006, 17:49
I admire your enthusiasm and would hate to squash it, but equally, I'd hate for you to get your hopes up and then have them squashed.
As a guide to what's required with resolution, an example that was once quoted to me goes as follows:-
Covert the bits to steps and imagine that you have to build a bridge of that length in meters.
So 8-bit would equate to a 255 meter bridge.
Think about the materials and what might be involved.
Now consider a 16-bit "bridge".
How might the design and materials change for a bridge that is over 65 kilometers?
What other things might you need to consider that you did not before?
One thing is for sure, you can't just scale up the 255 meter version using the same materials.
Keep the ideas comming but also think about the components and what their more detailed specs are.
A single step at 16-bit with a full scale of 5v is about 76uV. How much would an LED pulling 10mA reduce your power rail by? How much would the voltage across a diode be affected by a 1 degree temperature change?
Don't give up, it can be done. There are just a few more things to think about.
frankeltham
24-01-2006, 20:09
Just out of interest, what sort of levels of oscillator drift are we talking about on 08Ms, 18Xs versus those with external osc??
And are we talking about temp related drifts or manuf tolerances or what?
(Just so I can get an idea on the level of precisions and required stability.)
Its just that I want to use an 08M as a fairly accurate pwm osc. and I haven't had a good play yet. I need it to be pretty constant but OTT is not necessary for me.
BeanieBots
25-01-2006, 07:42
Not sure without digging out the specs but it can be enough to cause serial baud problems. Resonators are pretty good but you really do need to use a crystal for accuracy.
Jeremy Leach
25-01-2006, 08:43
Thanks Beaniebots.
Actually I've had a better idea where it comes to my MPX4115 pressure sensor. The output voltage is only in the range 4.25V to 3.6V for normal atmospheric pressures (on land), which is only 0.65V range. So it would be a great advantage to use op amps to apply offset and gain, to get a full scale voltage. It just so happens that 'Bray's 1-wire Barometer' on the net has just the circuit using LM358N op amp.
Of course, I can imagine the accuracy after the op amp depends on the component values etc etc ;-)
This has made me wonder whether a general purpose 'offset and gain' unit could be an idea in a design where lots of analogue sensors were needing to be measured. Have analogue Mux feeding into this sinlge unit. The offset and gain parameters could possibly be controlled by digital pots .... but now I'm drifting off !
BeanieBots
25-01-2006, 12:57
It's the right direction. Offset and gain is the best way to home in on the area of interest with maximum resolution. Just like framing and zooming a picture properly when it is taken rather than trying to edit it afterwards.
frankeltham
25-01-2006, 13:01
y=mx+c
I havn't read the whole thread (well..sort of on and off) so I appologize in advance for anyhting I may be repeating or beating to death...
I can offer a parallel here, of a sorts. When I tinker and build processors for my radio telescopes resolution is key. I've used everyhting from 8 bit to 24 bit ADC's, and all manner of amplification.
To go with what beaniebots said about the resolution: My first foray into very high precision (>12 bits) conversion was dreadful. I actually found I got better response from a 10 bit ADC than I did a 16 bit. the "aggrivation" seems to go in exponential leaps and bounds every time you add jsut a single bit.
You have to think of what's called noise floor. It's basically what your readings will be if you terminate the end of your entire chain without the sensor, and start taking readings. If you want 16 bit readings, but your total noise in the system wont allow better than 10 bit, it's pointless to use 16 bit.
Just as an idea to what I'm getting at:
my first 8 bit system total cost was around 10 dollars and a few hours work
my first 16 bit system total cost was around 40 dollars and 2 weeks of work
my first 24 bit system: Still working on it. heh. My total cost so far is around 60 dollars, and I'm not done. and the ADC didn't cost me a penny. It's been almost 2 months now, and Im' still tracking down bugs. Also...I havn't gotten better than 18 bit resolution. Kinda makes it bass ackwards to even bother, but it'll be worth it in the end.
It may seem like I'm babbling alot, but theres a point to it all. Theres a world of difference between 10 bit and 16 bit, and another world of difference between that and 24 bit.
If you dont really NEED it, I would design around it. Really...it's not always worth the effort.
--AndyP
BeanieBots
25-01-2006, 18:22
You're a brave man at 24-bit!
I've not played at that level for a while.
On the subject of cost though, like most things, you get what you pay for. The last 8 digit DVM I bought (circa 82) cost around £3k and that was without the leads which are comparable cost! Even then, you still have to wait 3 hours for it to warm and settle before taking a reading. Don't forget the cost of keeping it calibrated at around £500 twice a year.
The biggest stumbling block I've always found when working at that sort of edge, is what do you use to test its accuracy?
Nearly forgot, high tollerance low noise resistors will set you back around £6 ea, so don't go using them for pullups or LED current limiters like I caught an apprentice doing once.
Edited by - beaniebots on 1/25/2006 5:30:10 PM
premelec
25-01-2006, 23:24
I'm getting the feeling that the thread has drifted.... signal to noise on pressure or temperature or light devices precludes resolution of more that 12 bits most of the - practical - time. 8 bit PICAXES aren't a great choice for this either... however I'm all for people learning about practical limitations of measurement as well... :-) don't forget thermoelectric and triboelectric effects.... :-)
I thoguht triboelectrics went extinct like a few hundred million years ago ;)
re 24 bit bravery: I'm getting close to jsut kicking it across a field. My attempt is to get 1/100th of a dBm resolution, from a chip that gives out ~35mv/dBm, introducing as little noise as possible (since it's radio noise I'm trying to measure anyways!) It's not a necessary evil, but an attempt to future proof myself, at least for a while.
To get back on track and stop hijacking the thread: How about using a Dallas DS32KHZ as the external oscillator, for comparison to the internal oscillator, and adjustment form there. It's a TCXO in a 14 dip sized module, only out less than 2min/year at roomish temperature, according to the data sheets. I havn't had mine running a year yet so I couldn't tell ya.
Anywhoo, it'll give a seriously stable 32768hz pulse that you can use count or pulsin to measure, and it's pretty small. Same size a .3 dip, little wider, a bunch taller, but generally itty bitty (relatively).
Theres also the OTCXO route (Ovenized Temperature Compensated Crystal Oscillator). Spensive, big, but about as stable as you can get without going to GPS or Cesium ;).
--Andy P
frankeltham
26-01-2006, 07:38
Now, nearly back to the plot - frequency drift.
I'm just about to put an 08M in the freezer for a few hours.(-18oC nom.)
1. How much osc change should I expect?
2. Will it be able to communicate with my PC afterwards?
3. How much will my 38KHz pwm osc change by?
4. Or will it even work afterwards...?
Michael 2727
26-01-2006, 07:48
You could try blasting it with cigarette lighter refil Butane,, its good for around
minus 34 deg C or there abouts.
3 good blasts.
Thats how I test the DS18B20s.
Put your smokes out first, Pleeeease,,!!
Jeremy Leach
26-01-2006, 07:52
Andy ...the DS32KHZ IC looks interesting. I'd been searching all over for a simple all in one oscillator ! RS components stock it in the UK, at around £9, so it's not the cheapest solution - plus a little too accurate !
I think if I get to start using the LM331 VtoF then I'll move to a 28X solution with the external crystal.
I have found the following application note invaluable for getting better than 12-bit performance ( 0.01% linearity, 100 dB dynamic range 100ppm/C ) from the LM331 V-F converter. Using the configuration shown in fig 4 of this document compensates for most external component errors and 50Hz noise.
http://cache.national.com/ds/LM/LM231.pdf
frankeltham
26-01-2006, 13:20
Well, with a given pwm output command (calculated to give about 38kHz):-
37.8kHz after freezer (about -18oC)
38.6kHz at about 20oC.
pwm out 50% duty
reading +/- 0.2kHz (bit wobbley I know, my sample was a bit small.)
So, a couple of percent over that range.
Done on expensive Tektronix digital scope (calibrated last year at great expense).
Not 'scientific' but will give a rough idea. I only tried it on one chip and now I have things to do...
BeanieBots
26-01-2006, 13:34
Always good to test these things but finding the chip number and looking it up on the microchip site could save you a lot of effort and/or let you know if your results are typical or extreme.
One way to get a rough idea of PICAXE oscillator accuracy without any proper measuring equipment is to send a 'null' character ($00) via serial at a low baud rate to the PICAXE and measure the incoming pulse using PULSIN. It should be 9 x bit time long; 30000uS at 300 baud.
A PICAXE with a DS18B20 can be used with a PC to send the 'pulse' and read back through serial both temperature and pulse length.
It's only a rough guide because of inaccuracies in the PC UART clock, temperature measuring and PULSIN.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.