## Crystal Oscillator Question (TS922 Op Amp) Moderators: adafruit_support_bill, adafruit

Re: Crystal Oscillator Question (TS922 Op Amp)

Thanks, mstone.

Correct me if I am wrong, but the "process" for designing a crystal oscillator boils down to:

Feedback Loop:
1) Look up the ESR for the desired crystal from its data sheet, or from a resource like the Matthys article.
2) Choose a resistor value Rb that is approximately equal to the ESR.
3) Connect one side of the crystal to op amp output and the other to op amp (+).
4) Connect one side of Rb to op amp (+) and the other to ground.

Op Amp Gain:
1) Choose two resistors R1 and R2 such that R2/R1 is a little greater than 1 (which means that R1 is approximately equal to R2). Or maybe it would be more correct to say that the ratio of R2/R1 should be about equal to the ratio (ESR+Rb)/ESR? (calculations omitted)
2) Connect R1 between op amp (-) and ground.
3) Connect R2 between op amp (-) and op amp output.
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

mauifan wrote:Correct me if I am wrong, but the "process" for designing a crystal oscillator boils down to:

To clarify, I am fishing for some kind of mental "trick" that will help me understand how to calculate the resistors to use for a given crystal frequency.

For example, the tutorial listed here (http://www.sparkfun.com/tutorials/218) mentions a rule of thumb that the value of a pull-up resistor should be on the order of 1/10th of the impedance of a microcontroller input pin. Given that the input impendance on a microcontroller is on the order of 0.1-1Meg, this "trick" makes a whole lot of sense to me.

In the case of the crystal, I am thinking more or less along the lines that the crystal is purely resistive at its resonant frequency. (Ok... maybe not purely resistive, but close enough.) Therefore the analysis on the (+) side of the op amp boils down to a simple voltage divider, which is how I figured the gain:

V(+) = Vout * Rb / (Rb + ESR) <-- V(+) = voltage at op amp (+) pin
Gain = Vout / Vin = Vout / V(+)
Gain = (Rb + ESR) / Rb

Correct?
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

mauifan wrote:Correct me if I am wrong, but the "process" for designing a crystal oscillator boils down to:
{ . . . }

That's pretty good, but the actual first step is:

1) Choose a topology

There are a hojillion different circuits out there, each with its own quirks. For someone just starting out in the field, I'd suggest learning the Pierce, Colpitts, and Wein-bridge oscillators, in that order.

- The Pierce oscillator is a good all-rounder. It works over a wide range of frequencies, and IMO is easiest to grok in fullness. It's basically the same as a two stage phase shift oscillator (R1 goes from amp output to C1, C1 goes to GND. R2 goes from the top of C1 to C2, C2 goes to GND. Amp takes input from the top of C2, inverts it and adds gain, then off to R1 again). The crystal simply takes the place of R2.

- The Colpitts looks a lot like the Pierce, but the crystal lives in parallel with the capacitors rather than being in series. The trick to understanding a Colpitts is realizing that the amp is only active for 10-15% of the cycle. It's like pushing someone in a swing.. you add a burst of power where it will do the most good rather than trying to push all the way up and all the way back.

- The Wein bridge is a classic.. capable of high speeds and low distortions, simple on the resonant side, and a PITA on the gain side. It uses both positive and negative feedback, and strikes a balance between them.

Once you've chosen a general type of oscillator, you can look for rules of thumb about resisance values and such. For the Pierce oscillator, all resistors should be about the same as the crystal's ESR, and all capacitors should be about the same as the crystal's expected load (usually 15-30pF).

To really understand sine wave oscillators (crystal or not), you have to know how to break the circuits down into phase shift sections and understand what the sections are doing.

You need an integer multiple of 360 degrees of phase shift to get oscillation. The amplifier usually provides 180 degrees (inversion) and the passive components supply the other 180. You only get up to 90 degrees of phase shift from a single reactive component (capacitor or inductor), so you need at least two shift stages. Only two stages don't perform very well though, so most oscillators have three or four.

Each stage has some loss because of two basic facts: First, a passive component's output can never go farther away from zero than its input. Second, the output is always trying to catch up to the input, so the input passes its peak and starts back toward zero before output can reach it. The point where the two voltages are equal occurs at the output's peak, which is however-many degrees past the input's peak. Playing with the trigonometry a little, the output's peak will be the input peak times the cosine of the phase shift.

For an ideal three-stage oscillator, the output of each stage will be 1/2 its input. For an ideal four-stage oscillator, the output of each stage will be .7071 times its input. Three-stage oscillators need a loop gain of at least 8 to work, four-stage oscillators need a loop gain of at least 4. Then you have to add the nonidealities, like one RC stage loading the other (changing the effective value and frequency response), component tolerances, etc.

Interstage loading is the big unknown, and can increase the necessary loop gain dramatically. At that point you're into testing and tuning.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 9471
Joined: Thu Feb 11, 2010 1:51 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

mstone@yawp.com wrote:1) Choose a topology

There are a hojillion different circuits out there, each with its own quirks. For someone just starting out in the field, I'd suggest learning the Pierce, Colpitts, and Wein-bridge oscillators, in that order.

- The Pierce oscillator is a good all-rounder. It works over a wide range of frequencies, and IMO is easiest to grok in fullness. It's basically the same as a two stage phase shift oscillator (R1 goes from amp output to C1, C1 goes to GND. R2 goes from the top of C1 to C2, C2 goes to GND. Amp takes input from the top of C2, inverts it and adds gain, then off to R1 again). The crystal simply takes the place of R2.

Thanks for the tips! Again, you are being extremely helpful!

I actually started out my endeavor to learn about crystal oscillators by building the op amp circuit listed in http://en.wikipedia.org/wiki/Pierce_oscillator using the LM324. It didn't work, I got frustrated, and had difficulty understanding the phase shift. I totally understand the need for the output to be in phase (aka even multiple of 360 deg), but I couldn't understand how the crystal and caps could accomplish the 180 degree shift -- well... at least not until you mentioned that the crystal was primarily a resistor rather than the capacitor (as I was reading).

So... if I understand this correctly, the op amp/inverter amplifies and shifts the phase 180 degrees. The ESR in parallel with the biasing resistor R1 forms a "virtual" resistor R, which I can think of as two resistors r1 and r2 (r1 = r2 = R/2). r1 and C1 create a RC filter that shifts the phase 90 degrees. Likewise, r2 and C2 shift the phase another 90 degrees.

So... it is starting to sound to me like the secret to this sauce is to choose "small" capacitors C1 and C2 such that their impedance "overwhelms" the impedance of the resistor R by a factor of at least 10? Ignoring the biasing resistor R1 for the moment (with serves to lower the effective ESR), my 32kHz crystal has an impedance of 30k. Therefore, my choice for capacitors would need to have an impedance of at least 300k. Thus, my Reactance calculator says that I need caps on the order of 16-17pf?
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

mauifan wrote:I actually started out my endeavor to learn about crystal oscillators by building the op amp circuit listed in http://en.wikipedia.org/wiki/Pierce_oscillator using the LM324.

Aha.. there's part of your problem. That design is a logic-gate square wave oscillator.. it wants something like a gate from a 74HC04. R1 is just a negative-feedback resistor that pulls the inverter's input and output to Vcc/2.

The fact that C2 connects directly to the inverter's output means that side of the crystal will snap back and forth between GND and Vcc really fast.. a desirable feature for a square wave oscillator. For a sine wave oscillator, not so much.

mauifan wrote:So... if I understand this correctly, the op amp/inverter amplifies and shifts the phase 180 degrees. The ESR in parallel with the biasing resistor R1 forms a "virtual" resistor R, which I can think of as two resistors r1 and r2 (r1 = r2 = R/2).

Nope, sorry.

I don't blame you for being confused.. nothing on that page shows the missing piece you actually need to get a sine wave oscillator working: a series resistor between the inverter's output and the top of C2.

Replacing the inverter with an op amp adds yet another problem because you don't bias an op amp's input with negative feedback. You do it by connecting the positive input to Vcc/2.

The circuit you actually want looks like so:

C1 and C2 should both be the same, and equal to the crystal's rated 'load capacitance'. If you don't have a spec sheet, 30pF is a good starting point.

R1 should be about the same as the crystal's ESR.. pick the nearest standard value that's lower.

R2 sets your gain. Start with something around 4 times the crystal's ESR, but be prepared to adjust upwards until you actually see oscillation.

As you guessed, the inverting amp gives you 180 degrees of phase shift. R1 and C1 give you another 90 degrees. The crystal and C2 give you yet another 90 degrees, for a total of 360.

Normally having two 90 degree shift stages means you get a lousy oscillator, but the crystal adds some extra magic to keep things running smoothly. It technically works as an inductor, turning C1-crystal-C2 into a pi network. It's a little easier to think of it as a resistor that knows how to steer toward the resonant frequency though.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 9471
Joined: Thu Feb 11, 2010 1:51 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Sorry it took me so long to respond, mstone. With Halloween preparations and such, I took a quick look at your circuit diagram and decided that I needed a mental break. :lol:

Looking at your diagram again, well... it looks a little "funny" to me because it is drawn a bit differently than the other diagrams that I have been looking at, but at the same time... I think it makes sense. The two caps shift the phase 180 degrees, so the signal at the op amp (-) should be pretty much in phase. The placement of R2 looked a little strange, but likewise, it seems to make some sense now.

All that said, I coulda sworn that I tried something like this using the LM324. I am going to have to try this again, but I tried both positive and negative feedback versions and all I could get out of it was output that looked a bit like a comparitor -- i.e. zero volts when input was below some threshold and Vcc when the voltage was above that threshold.

Anyway... let me go do some experimenting and I'll report back in a few days.

Thanx!
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

mauifan wrote:All that said, I coulda sworn that I tried something like this using the LM324. I am going to have to try this again, but I tried both positive and negative feedback versions and all I could get out of it was output that looked a bit like a comparitor -- i.e. zero volts when input was below some threshold and Vcc when the voltage was above that threshold.

That means you had it oscillating, but the gain through the op amp was too high. Makes sense if you didn't have a negative feedback resistor (R2), or if its value was large compared to the crystal's ESR.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 9471
Joined: Thu Feb 11, 2010 1:51 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Sigh...

Again, sorry it took so long. Once again, life found a way to sneak between me and my efforts to learn electronics.

I wired up your diagram, mstone. The op amp is the LM324. The crystal is 32kHz. R1 is a pot set at 30k. R2 is a pot set at about 120k. The caps are 22pF. I am going to have to double (if not triple) check my wiring to make sure that I got it right, but my first attempt did not work at all. My scope shows a constant 3V when I flip on the power switch, and 0V when it is turned off.

Oh... Vcc=6V. The op amp (+) connects to the 3V supply on my breadboard.
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Well... it looks like I wired the circuit correctly. However, I checked the crystal specs and it looks like it needs 12.5pF load capacitors. I guess I need to find smaller caps, or possibly remove the 22pF caps with the hope that the breadboard itself will provide sufficient capacitance.
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Try cranking the gain up to a megohm or so.. 10M if you have the parts on hand, or just pull R2 out entirely. It should give you almost a square wave, but at least you'll know whether it oscillates.

Gain calculations usually aren't very accurate. They give you a ballpark estimate and a "below here it won't work" limit, but they don't account for things like interstage loading. The op amp's output feeds current into the first capacitor, but the crystal pulls some away. That changes the effective values of R1 and C1, and the math gets ugly real fast.

It's easier (or at least less frustrating) to start with a lot of gain, get oscillation, then lower the gain to reduce the distortion than it is to start with a low gain and see nothing.

There is one thing you might be able to see with your present setup though: connect a long wire to the node where R1 and C1 meet, connect a scope probe to the op amp's output, set your scope to trigger mode with the threshold around 3/4Vcc, then tap the wire into your Vcc or GND rails.

Basically you're hitting the thing like a bell, and with any luck the scope will capture the ringing. Ideally, you'd see a wave that decays quickly. The decay means the circuit doesn't have enough loop gain to sustain the oscillation.

Keep plugging away at it though. I've been doing some sinewave oscillators myself recently, and they're frustrating beasts.. very sensitive to component values and ratios. They have a tendency to sit there silently mocking you until you make that one final adjustment, then they run with a beauty that's slightly insulting. You try the first one out of curiosity, and from then on it's all about revenge.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 9471
Joined: Thu Feb 11, 2010 1:51 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Haven't really had a chance to experiment since yesterday, but for what it is worth, R1 is a 100k pot and R2 is a 1M pot. I tried varying both pots up and down the dial with no success. It is entirely possible that maybe I missed the right combo, but thought I might chime in on the off chance that it rings a bell.

That said, I'll try removing R2 to see what happens... but it probably won't happen today.
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Just for reference:

I built a version of the same basic circuit last night to make sure I wasn't feeding you useless information. I used an MCP6001 op amp instead of the TS922, 10pF caps, a 33k resistor for R1, and two 68k resistors for the Vcc/2 voltage divider. The construction was semi-ugly-style on a small PCB I make myself, and included a socket so I could test various negative feedback resistors.

It worked, but was tempermental. A 10M feedback resistor gave the best chance of starting up well, but the sine wave output showed some clipping. A 1M feedback resistor gave me a better sine wave, but was more prone to spontaneously doing dead. Feedback values below 680k threw the thing into overtone mode, sending it oscillating at 192k rather than 32k, and resistances below 150k wouldn't oscillate at all.

Like I said though, the thing was flaky, and often wouldn't start up unless I deliberately added external influence like touching the crystal-side of the feedback resistor (large parasitic inductance).

Overall, I think the thing is a little too stable. I'm going to play with the feedback to improve the startup performance, and will post when I have something to show for it.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 9471
Joined: Thu Feb 11, 2010 1:51 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

Hi Mauifan,

Jeri Elsworth has an interesting video on a Wien Bridge Osc.
And she made some good points.
Listen to what she says about 'gain control'
because you will have the same issue with your osc.

Check it out here: http://www.youtube.com/watch?v=vBsSASge7ls&feature=em-subs_digest

John
john444

Posts: 443
Joined: Sun Mar 04, 2012 1:42 pm
Location: Claremore, Ok

Re: Crystal Oscillator Question (TS922 Op Amp)

Did a quick experiment and removed R2 from the circuit. Didn't help.

I put R2 back in the circuit and fiddled with the pots. As luck would have it, I found a setting that caused oscillation. Unfortunately, it was not the sine wave that I hoped for. It looked more like a sawtooth wave with the tops cut off. Reducing the gain didn't give me the sine wave.

There also seems to be a "trick" to get the oscillation started. Basically, I set R1 and R2 to max (100k and 1M, respectively), and then lower the value of R1 to maybe 10-20k. My scope also seems to play a factor -- it won't oscillate if the time scale is set at 50usec, but does start if the time scale is set at 500msec (presumably due to capacitance differences).
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm

Re: Crystal Oscillator Question (TS922 Op Amp)

john444 wrote:Hi Mauifan,

Jeri Elsworth has an interesting video on a Wien Bridge Osc.
And she made some good points.
Listen to what she says about 'gain control'
because you will have the same issue with your osc.

Check it out here: http://www.youtube.com/watch?v=vBsSASge7ls&feature=em-subs_digest

John

The video makes a boatload of sense to me... but AVR doesn't put a light bulb in their ATMega controllers, right? What do they do to get consistent oscillation? Does an inductor function as the "light bulb"?
mauifan

Posts: 161
Joined: Sun Aug 12, 2012 10:13 pm