# Square wave from comparator drifts and the integrated square wave doesn't yield a triangle wave

I am trying to make a function generator.

The generator runs on 16kHz, the rails are +12/-12V for test purposes.

I basically obtain a sine wave with an oscillator, drive a comparator (LM311) with the sine wave to get a square wave, then integrate the square wave to obtain a triangle wave.

The problem is the square wave drifts to the positive rail and I get nothing near a triangle wave. I also used a TL071, which also didn't work. Below is the circuit: This is the result. Blue is the sine wave, green is the triangle wave and purple is the square wave:  I want the generator to have a variable frequency.

• Having time and voltage scales on those sweeps would help. I think your triangle generator might just be a lot faster than you think it is. Maybe replace R6 with 1M so it's a bit more linear. I figure your slew rate (triangle ramp slope) to be about 120KV/s. Jun 25, 2021 at 19:55
• The 741 is an old dinosaur and should be expunged from everyone's parts toolkit (and it's not really that cheap today). At least use a TL081 or LF351 (JFET input opamps) or even an LM358 (it's a dual opamp) or its quad equivalent LM324. They are not "modern" opamps (although the LM358/LM324 are still widely used today for undemanding applications), but way better than the 741. Jun 25, 2021 at 21:16
• The purple wave drift looks like what you get if you AC-couple something with a DC offset. The capacitor charges up to the DC offset voltage and cancels it out, and this takes a bit of time. Jun 25, 2021 at 23:36
• I will change the op-amp. Other than that, which part is the one with DC offset and which part is the AC part? How can I solve this coupling problem? Thanks. Jun 26, 2021 at 5:54

R6 is too small, it should be in the order of 1M or 470k.

R3 is too large, make it about 4k7. This resistor forms a potential divider with the integrator's input resistor. When the input to the integrator goes high these two 100k resistors are acting together to halve the input voltage to the integrator compared with when the integrator's input goes low which unbalances the integrator.

There are 3 factors which control the amplitude of the output triangle wave:-

1. Amplitude of square wave input.

2. Frequency of input square wave. That is to say how long the square wave stays in the high state and how long it stays in the low state.

3. The R4 * C1 time constant.

At the moment, the amplitude of the triangle wave is far too large (the op amp is saturating at the supply rails) so increase the frequency of the sine wave or increase the size of C1 to bring the triangle wave's amplitude down to within the supply rails.

Even with a change to R3, making it 4k7, the square positive going excursion will be slightly smaller than its negative going excursion causing an offset of the triangle wave from centre ground. To significantly reduce this offset insert a small resistor (say 100R) between the 4k7 resistor and the output of the LM311 and connect the input to the integrator between these two resistors. This will reduce the amplitude of the negative going swing of the square wave and the triangle wave will be more central. You will have to adjust the value of this extra resistor to find its optimum value in order to minimise the triangle wave offset.

You may have realised from the 3 amplitude controlling factors listed above that the amplitude of the triangle wave will vary with sine wave frequency. Fixed value components will only allow a limited frequency range for an amplitude of the triangle wave between very small and an amplitude which is limited by the supply rails.

EDIT

To calculate the amplitude of the triangle wave:-

Vout = -(Vin * t)/(RC)

Where:-

Vout = the amplitude of the triangle wave

Vin = the amplitude of the square wave

t = half the time that the square wave is either high or low = 1/(4f)

RC = input resistance multiplied by feedback capacitance

There is a negative sign in the formula because when the input square wave goes positive, the integrator's output ramps down in a negative going direction.

• the 741 opamp is 53 years old and has trouble with frequencies over 9kHz. Modern opamps go to at least 100kHz. An integrator needs to have the resistor parallel to the capacitor at least 100 times the value of the input resistor which cannot work with an old 741 opamp. Jun 25, 2021 at 20:42
• @Audioguru any suggestions? TL071 doesnt seem to work. Jun 26, 2021 at 8:13
• @James I managed to create a good square wave thanks, but I still cant obtain a triangle wave. I increased the C1 to 1u and R6 to 1M, reduced R3 to 4.7k and R4 to 10k, still nothing happens. Jun 26, 2021 at 8:14
• @Tombeki Leave R4 at 100k so that it doesn't reduce too much the positive input to the integrator by its divider effect with R3 (4k7). Also if you let me know the sine wave (square wave) frequency I can work out what size of triangle wave amplitude you should be getting to see if it should be within the supply rails. Jun 26, 2021 at 8:59
• @James mathematically the frequency should be around 15.9kHz. When I add a frequency counter to the simulation I get 1589 Hz. I get a constant 1.5V output from the integrator. Jun 26, 2021 at 12:02

Since the content of the signal is purely AC you may want to consider capacitivley coupling the analog stages to eliminate the DC signal (you may have to provide a DC offset for the capacitor to center the signal if the rails you have are not single sided)

• an example of single sided = 0 to 5V; dual rails = ±5V
• I am sorry but I am not familiar with capacitor coupling and eliminating "DC" concepts. Can you explain further please? I really want to learn. Jun 25, 2021 at 19:17