# Generating square wave to be inputted into a MCU

There are signals like below picture inputted to MCU STM32f103: (Offset DC about 800mV, Vmax = 2.4V, f = 66 Hz)

I want to change it to a squar wave from 0V to 3.3V being accepted voltage range for my MCU. How I can implement it? Using a schmitt trigger implemented by an op amp? Can you please provide me a schematic?

EDIT:
Based on ChrisStrattons comment:
Cost and power do not matter. I am going to obtain frequency of signal by MCU Timer/Counter. In fact my ultimate goal is to have the frequency to calculate speed. One hertz could be an accepted precision and the frequency is less than 1 KHz. Actually the first thing I tried was AC coupling being able to remove DC offset. However, the amplitude was not what I want. To sum up, I need to have a square wave with the same frequency and 0 V to 3.3 V amplitude.

• What properties of the signal do you need to recover? If timing, with how much precision? Do you need to sleep to save power? If the requirement is lenient using the adc and software hysteresis could be an option. If the leading edge is reliably sharp AC coupling and a blanking time on retrigger could work. A meaningful answer would require details and goals not presented - do you want universal reliability? Minimum cost? Minimum.power? Aug 24, 2019 at 15:18
• @ChrisStratton Cost and power do not matter. I am going to obtain frequency of signal by MCU Timer/Counter. In fact my ultimate goal is to have the frequency to calculate speed. One hertz could be an accepted precision. Actually the first thing I tried was AC coupling being able to remove DC offset. However the amplitude was not what I want. To sum up, I need to have a square wave with the same frequency and 0 V to 3.3 V amplitude.
– Pana
Aug 24, 2019 at 16:16
• No, your application does not require a square wave, it requires recovering timing. You could do that with the ADC without further conditioning the signal electrically. You could for example look for a positive change of a certain amount within a small number of samples. You could then use that to adjust a software PLL. Aug 24, 2019 at 18:31

For standard IO the STM32F103 datasheet section 5.3.13 says:

• $$\ V_{IL(max)} = 0.28 (V_{DD} -2 \ \text V) + 0.8 \ \text V \$$. For a 3.3 V supply this is 1.16 V.

• $$\ V_{IH(min)} = 0.41 (V_{DD} -2 \ \text V) + 1.3 \ \text V \$$. For a 3.3 V supply this is 1.83 V.

Offset DC about 800mV, Vmax = 2.4V.

        0         1         2         3  3.3 V
|    .    |    .    |    .    |  |
GPIO:   |           ===.===              |
Signal: |       =================        |
`

Since your signal low < GPIO VIL min and your signal high > GPIO VIH max I think you can feed your signal straight in.

simulate this circuit – Schematic created using CircuitLab

Figure 2. A comparator with auto-reference adjust.

The idea here is that C1 will charge to a reference voltage a little higher than the low signal due to the duty-cycle of the signal. R3 helps pull it a little higher but may not be required.

Most comparators have open collector outputs so R1 has been added but the GPIO internal pull-up may suffice.

• Yes, you are right. However, in my circuit there are some conditions that do not conform to MCU logical range and I need to reassure myself. So please consider that I want to convert it to a square wave.
– Pana
Aug 24, 2019 at 11:40
• You need to make that clear in your question to avoid wasting folks' time. Aug 24, 2019 at 11:43
• This is a relatively bad idea unless the behavior of the input is known to be reliably consistent. Without a solid explanation of the source of the offset it's level cannot be relied upon to stay safely low. Aug 24, 2019 at 15:04
• Sure, but there was nothing in the question to indicate that a stable source was not the case. A comparator will suffer the same problem is the $V_{IL(max)} > V_{IH(min)}$. One could try using a very low-pass filter for the signal and use that as the reference. Aug 24, 2019 at 15:16

a much easier way would be a simple schmitt trigger like you first suggest, you can use an op amp to do it, it just changes in name to a comparator with hysteresis,

simulate this circuit – Schematic created using CircuitLab

Edit: supply voltage would be 0 and 3.3V, the 100K controls the amount of hysteresis, right now its quite low, but should be suitable

Edit2: Changed to non inverting, circuit also simulated here with falstads,

• The TL081 requires a minimum supply of 10V. Also, opamps can be slow when used as comparators. Better to use an actual comparator instead. Many operate at 3.3V, and some of them have built-in hysteresis. And your circuit inverts the signal, which may or may not matter to the OP. Aug 24, 2019 at 11:15
• @Reroute I simulated your schematic by means of Multisim but i get only a constant DC voltage in output.
– Pana
Aug 24, 2019 at 11:15