# Zero Crossing Detection of ~ 400 kHz Signal with MCU

I want to measure the frequency of a signal using the digital input pins of the NUCLEO-F767ZI. The signal is sinusoidal with an amplitude of 5 V and a frequency ranging from 100 kHz to around 400 kHz.

1.) First I thought about simply feeding the pure analog signal to the input pin that is 5 V tolerable. I thought about using a serial diode for protection against the negative half cycle and using the internal pull down resistor of the MCU. Then I could generate an interrupt whenever the sinusoidal signal is high enough for the GPIO to recognize it as logical HIGH.

simulate this circuit – Schematic created using CircuitLab

2.) After a bit of research on StackExchange, I also found configurations using opto-isolators: Detecting Zero Crossing of Mains (Sine and Square Wave) The advantage is that it would output a sharp rising edge easily recognizable for the digital input pin, rather than the limited slope steepness of a sine wave.

3.) Since the signal does not have a dangerously high voltage, I could also skip the isolation and use a simple BJT or MOSFET instead. This would also output a sharp rising edge.

simulate this circuit

Which of the above options would you recommend? And above all: I hope that the parasitic capacitances of the semiconductor devices do not have any effect below 500 kHz, is that right? Or do you have a different and better approach?

Best regards and thanks in advance!

• Welcome to EE.SE. Did you consider using a comparator, such as an LM339 or LM311? It is their job to convert slow rise/fall times to a much faster rising or falling edge. If your MPU has input capture or a spare counter input, then much of the work is done for you. Don't build what you can buy. – Sparky256 Feb 17 '18 at 20:54
• Regardless the comperator, which is indeed tailor made for comparing voltages, you are using that NPN transistor in a way that won't help you. Google "Emitter Follower" for more information. – Asmyldof Feb 17 '18 at 21:00
• Likely what you want is a series capacitor to couple the signal into an input biased by some resistors, diode clamped against excessive swing in either direction, and perhaps interpreted by a schmitt trigger. Unless you need to detect phasing, you don't need a literal "zero crossing" but just a logic signal which tracks the frequency of your input. – Chris Stratton Feb 17 '18 at 21:06
• Is the sine from 0-5V or is it from -2.5V to +2.5V? – crj11 Feb 17 '18 at 21:07
• Thanks for recommending a comparator! I will have a look at that. @crj11 The sine has an amplitude of 5 V and no DC offset, so it goes from -5 V to 5 V. – Geralt Feb 18 '18 at 14:28

If you are dealing with an analogue signal and trying to convert it to a suitable square wave for frequency measuring you have to consider the effects of noise and implement some form of hysteresis so that at the threshold point (where the circuit arbitrates between 0 and 1) there isn't oscillation of the digital output.

The above picture taken from here and it hints at using a schmitt trigger like the one below (I have used this circuit several times): -

It works from 3 volt supplies or 5 volt supplies. The line-in capacitor is to remove any DC component of the input. The capacitor on the inverting input filters the signal so that what appears at that input is Vcc/2. The picture comes from here Turning the output of an opamp into a square wave.

• At which values of LINE IN does the output switch? – Geralt Feb 18 '18 at 15:09
• A few tens of mV peak to peak is the low limit from memory. The data sheet will tell you but other similar devices from Linear Technology will be a little different. – Andy aka Feb 18 '18 at 15:39
• @Geralt - the whole point is that it looks at the derivative, not the value. Remember, the poster is trying to recovery frequency, not phase or amplitude. – Chris Stratton Feb 18 '18 at 17:19
• @ChrisStratton I am the poster. I changed my name in the meantime. :D No, I really wanted to know what Andy aka told me: The boundaries of the hysteresis, meaning around which voltage thresholds around zero (in the input) the Schmitt-Trigger changes its output. – Geralt Feb 18 '18 at 22:38
• The point of this circuit is that because of the initial capacitor it only looks at change not level - there will be no fixed relationship to zero. If frequency measurement is your goal then this is exactly what you want. If something else is your goal, then you need to edit your question to be more accurate. If you think that Andy quoted you a level with respect to zero, then you've misunderstood a not very clear response; what you were actually told was the magnitude of a typically required short term change. – Chris Stratton Feb 18 '18 at 22:46

You could use a comparator (or any opamp basically).
Since all you need to do is limit the frequency and convert the arbitrary wave into a square wave for the schmitt triggered timer capture input pin.

The most basic approach would be this absolute comparator based on a fixed reference, without any hysteresis.

simulate this circuit – Schematic created using CircuitLab

You could even use an on-chip analog comparator, if you can connect the input capture event to the comparator signal internally.

There will be caveats with this approach, but you didn't provide much details about the signal source and it's (common mode) levels.

• Give OA1 a positive feedback for the Schmitt-Trigger behaviour you noted. – Janka Feb 17 '18 at 21:41
• @Janka Yes, with a schmitt trigger pin you could get away with a few Mohm directly to mcu pin! – Jeroen3 Feb 17 '18 at 21:43
• The analog signal comes from a measurement device and I think that it cannot source/sink much current. So the OpAmp approaches might be best. But I think that I would connect the inverting input of the OA1 directly to GND, right? Because the analog signal ranges from -5 V to +5 V. So I want to detect a zero crossing at 0 V. – Geralt Feb 18 '18 at 14:45

You could try zero crossing detector.

simulate this circuit – Schematic created using CircuitLab

For most of the time (positive half cycle) Vin >> 0.7V hence the Q1 is in saturation and the output is low (GPIO). As input signal "approaching" zero the Q1 turns-OFF and the output voltage goes "high". And it stays high until the input signals turn-ON Q2 and Q3 (negative half cycle). And this will happen around -0.6V volts. So, for the remaining part of a negative half cycle, the output is low (Q2 and Q3 Turn-ON).

But to be honest I never try it at such a "high" frequency.

• I'd like to upvote this, but: what does it do? Please describe its functionality for me and OP! – Marcus Müller Feb 17 '18 at 21:49
• @MarcusMüller. I recognize this circuit. It came from stereo's of the 70's and 80's and was used to detect DC on the amp output. R1 would have been 10 K and a 10 uF cap to ground so only DC would pass through. This circuit will NOT work if the signal cannot sink or source several mA, and have a bipolar supply. – Sparky256 Feb 17 '18 at 22:43
• @Sparky256 Yes, you are right this circuit can also be used to detect DC-offset on the amplifier output and protect the loudspeaker form DC. And this circuit will work with the single supply, bipolar supply is not needed. – G36 Feb 17 '18 at 22:48
• @G36 Honestly I do not know how much current the signal can source or sink, I will have a look at the datasheets. But I would guess that it is not a lot, so I would rather stick to the answers above, containing OpAmps. – Geralt Feb 18 '18 at 14:35

A very simple and low cost (< $1) solution that also has the benefit of electrical isolation is to use an AC opto copuler like the TLP184SE shown below. These type of couplers have a "current transfer ratio" that specifies how much of the input LED current is converted into output transistor current. Just use the correct size resistor to set the LED current and you get a full wave rectified output at the other side. No extra power supply is necessary. You decide the output voltage by the pull up voltage used on the output resistor. The LED coupling also provides some "edgification" of the signal because they do not conduct until around 1.2V, so there is zero output when the input is between -1.2V and +1.2V. You will of course have to divide the frequency that you measured by 2 because you are now counting both halves of the pulse. You should also take a look at the STM General Purpose Timer Cookbook, which shows how to use the internal timer/counter logic to process external signals without CPU resources. • Ok, this is also interesting. I think that this device can be used up to a few MHz, so that the parasitics of the devices do not come into play below 500 kHz, right? And thank you very much for the link to the STM cookbook! – Geralt Feb 18 '18 at 23:13 • I agree that it should work below 500KHz. The part is in stock at many distributors and less than$1 in small quantity. – crj11 Feb 18 '18 at 23:58
• On further investigation of the datasheet, it may be marginal at 500 KHz. I think it will depend on the exact LED current used and the pull up resistor value. If you capacitively coupled the output at the collector of the transistor, that could potentially increase its frequency range. I think it will take some experimentation with a real part to know for sure. – crj11 Feb 19 '18 at 2:23