# How to convert sine wave from an oscillator to TTL signal for microcontroller?

I tried to measure inductance by using a colpitts oscillator to generate sine wave. It works. I need to convert the sine wave to 5V square wave to feed to a microprocessor. The sine wave is coupled with a capacitor 0.47uF to eliminate the signal offset. The signal after the capacitor is zero crossing sinewave. When the signal connected to a zero-crossing detection comparator, I got problem. The zero- crossing sine wave drifts to about 10mV above 0 V. The output of the comparator is flat no square wave, since the input signal always higher than 0V. How should I do to avoid the signal drift, to convert the sine wave to TTL.

• I like that circulating-current design ----- very localized ------ for excellent phase noise. Jun 9, 2017 at 15:34

How should I do to avoid the signal drift, to convert the sine wave to TTL.

You need to set DC voltage levels on both comparator inputs to (nominally) half supply rail. You can do this with a potential resistive divider on both inputs: -

For the input that connects to the capacitor use high value resistors to avoid low frequencies from your Colpitts oscillator being attenuated too much. Alternatively make a direct connection to the emitter and use a type of data slicer circuit like this: -

Your emitter connected sinewave is likely to be biased up sufficiently for the + comparator input and, R1 and C1 form a low pass filter to produce the same DC level on the other input but without significant AC signal content. Make R1 = 100 kohm and C1 = 1 uF - this will be OK down to frequencies as low as 10 Hz but if problems make C1 bigger.

You cannot expect an op-amp or comparator input to function correctly with just a capacitive connection AND you cannot expect (most) op-amps and comparators to work with an input connected to the supply rails.

Finally, it's a good idea (as pointed out in the comments) to add a pinch of hysteresis to prevent the output stuttering as the input crosses through the reference point. This is usually achieved by two resistors supplying a bit of positive feedback. Here's how you'd implement it for the data slicer type configuration (shown earlier): -

• Best use some hysteresis, a "sharply tuned" comparator will turn something like a slow sine wave ... from an unshielded LC oscillator that likely will pick up mains hum and am radio ... into all kinds of jitter and errant pulses. Jun 9, 2017 at 15:22
• @rackandboneman yes, adding a small amount of hysteresis is a good idea. I shall footnote it into the answer. Jun 9, 2017 at 17:35
• Or a massive amount - I can only imagine a few hundred ways you might get unexpected frequency multiplication with that kind of inductance meter design ... Oh, and some overvoltage protection might also be a good idea with inductors of unknown Q factor... Jun 10, 2017 at 21:49
• It's a colpitts oscillator and shouldn't need over voltage protection. Jun 10, 2017 at 22:02
• Thanks Andy, The data slicer circuit is a very interesting idea. wondering if I can use single power supplyed (+5V only) comparator with the data slicer circuit. definitely I will use add hysterisis to my circuit. the frequencies of my application are 200KHz to a few Mega Hz. Is there any speciall concern I should pay attention? Jun 12, 2017 at 6:21

Turn your output OP-Amp into a Schmitt trigger by placing a positive feedback resistor between output and positive input. You can adjust the trigger levels by adjusting the feedback and input/ground resistors.