# From sine wave to square wave

I want to make a square wave generator with an LM311.

I have a variable sine wave source from 1Hz to 16kHz. The amplitude can vary between 0.5V to 11.4V.

I tried to use the comparator circuit below but couldn't get an acceptable result.

Sine wave configuration:

Blue is the output of the comparator, yellow is the sine wave:

Edit: After I added the pull-up resistor and made the supply bipolar (+12/-12V) I get the result below which is quite fine but has little distortions such as spikes. Peak values are on the other hand are not even near to the desired output. The wave oscillates between -11V and 750mV

Edit2: In real circuit I get the result below. What should I do to flatten the wave? Why is this happening?

• Maybe your scope probe needs calibration adjustment. Does your scope normally display square waves ok? Display on your scope the probe calibration square waveform which will be output somewhere on your scope's front panel and see if that is perfectly square. If it isn't then adjust the small screw on the probe until it is. Jun 23, 2021 at 20:49
• I did the calibration the moment I got the probe, but it seems calibration was the problem! Thanks man! Although there is one simple problem. As you can see the voltage varies between -10V and 750mV. Any suggestions there? Jun 24, 2021 at 8:06
• Pin 8 to positive rail, pin 4 to negative rail. Tie pin 1 to whatever voltage you want the negative going output swing to limit at - ground or negative rail. Short pins 5 & 6 together (the offset adjustment pins). Pins 2 & 3 are the inputs, so tie one of them to ground and the other has the sine wave applied to it, (doesn't matter which way around). Sine wave must swing either side of ground. Pull-up resistor between pin 7 and positive rail. 4k7 would be a reasonable value so output sinks 24/4.7k = 5.1mA when output pulled down to negative rail. If the LM311 is ok it should work. Jun 24, 2021 at 10:57
• Yeah it worked thanks! There is still a little offset between negative peak and positive peak but it is acceptable I think. Although, when I try to connect the output to an integrator to obtain the triangle wave, form of the square wave gets heavily distorted. Jun 25, 2021 at 5:57
• Could you post a picture of the "heavily distorted" square wave which you get when it's input to the integrator. Jun 25, 2021 at 7:29

The LM311 isn't a dual output op-amp or comparator; it has a floating output BJT that can be wired in various configurations but, not like you showed in your question: -

In addition you need a pull-up or pull-down resistor to make it work. You'll also need to make sure that the input sinewave is halfway between the power rails regarding DC offset.

Also, don't expect spectacular performance at 16 kHz because it's quite a slow device: -

• I tried it with bipolar power supply but without a pull-up or pull-down resistor, it didnt work. I am a newbie when it comes to the applications of electronics so I would appreciate it if you can explain what are those resistors and why I need them. Also do you have any suggestions for better alternatives to the lm311? Thanks. Jun 23, 2021 at 13:19
• @Tombeki - this site is about answering questions and, as directly as possible. The resistors that pull-up (or down in some cases) are fundamental components for a lot of comparators. Not true for op-amps but, for comparators they are quite common. So, I suggest you read up on comparators or read the data sheet for the LM311 (I've already provided some extracts that show the "floating" output transistor. If I wanted to convert a sinewave to a square wave I'd be looking at a MAX999 low voltage comparator but, for your use, you'd need to reduce the input amplitude to suit the power rails. Jun 23, 2021 at 13:53
• Well I want to make a function generator with variable amplitude so I cant really reduce the input amplitude. Input voltage between the rails in my config, do I need to adjust according to something else? Isnt it already suitable? Jun 23, 2021 at 14:51
• @Tombeki of course you can - use a resistive potential divider to lower the amplitude of the sinewave to something suitable for feeding into the MAX999 comparator. Jun 23, 2021 at 14:52
• No I mean do I still need to adjust? Amplitude is already between the rails. Also if I lower the amplitude to obtain the wave form I would need to amplify it again which ı dont think very practical. Jun 23, 2021 at 18:45

For one thing, you'll need a pullup resistor on pin 7 — it's just an open collector.

For another thing, you'll have to bias the sinewave input so that it stays between the power rails of your comparator (Gnd and +12 V). Or switch to a bipolar (±12 V) power supply. Without that, when the input goes negative, it also pulls the output negative.

• I have a bipolar power supply. When I tried the circuit with bipolar supply in simulation it didnt work. I guess its because I didnt have a pull up resistor. How should ı connect the resistor, what does it do? Thanks. Jun 23, 2021 at 13:21
• The pullup goes between pin 7 and the positive supply. It sources the current required to drive the output high. Jun 23, 2021 at 16:17
• Does the value matter? Jun 23, 2021 at 18:43
• Well, sure. If it's too low, the comparator will have to sink too much current. If it's too high, the risetime of the output waveform will suffer. Try something around 10K to start with. Jun 24, 2021 at 13:14