Author Topic: Triangle waveform generation using Opamp  (Read 2568 times)

0 Members and 3 Guests are viewing this topic.

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Triangle waveform generation using Opamp
« on: September 16, 2026, 06:53:57 am »
I want to experiment and generate a triangle wave using opamp, the input is a PWM, tried simulating in LTspice but could not get the result, initially i want to generate triangle of 1kHz but do not know how to choose the components. I know the capacitor charges when current passes through it and discharges through load resistor. The model does not run and shows error. Some suggestions of how to get started then i can work it out. Thank you in advance. 
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5323
  • Country: nl
Re: Triangle waveform generation using Opamp
« Reply #1 on: September 16, 2026, 08:17:18 am »
I don't have (lt?) spice installed to see your circuit, but the most common is to use an integrator combined with a schmitt trigger to toggle the input of the integrator at predefined levels. If you know the (constant during one flank) voltage over the resistor, then you can calculate the current though the resistor, and then the rate of change follows from the capacitance of the capacitor.

https://duckduckgo.com/?q=opamp+triangle+wave+generator&iar=images&t=h_
« Last Edit: September 16, 2026, 08:19:45 am by Doctorandus_P »
 

Offline PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 3323
  • Country: au
Re: Triangle waveform generation using Opamp
« Reply #2 on: September 16, 2026, 08:56:38 am »
I want to experiment and generate a triangle wave using opamp, the input is a PWM, tried simulating in LTspice but could not get the result, initially i want to generate triangle of 1kHz...
If you have dual-rail opamps, you just need a balanced square wave feeding the resistor into the integrator.
However, you will discover a practical issue of square-to-triangle, is integrator creep, as you cannot perfectly match those charge and disharge I * T products.
A parallel resistor can be added to tolerate some skew, but that also means a less than ideal triangle.

The more practical triangle generator is to combine a Schmitt and integrator, in a closed loop system - see 3.20.4

https://eee.poriyaan.in/topic/triangular-wave-generator-using-op-amp-11952/
 

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: Triangle waveform generation using Opamp
« Reply #3 on: September 16, 2026, 09:12:54 am »
The image of the circuit, i am trying to write the mathematical equation i will post once done.
 

Offline Obin

  • Regular Contributor
  • *
  • Posts: 125
  • Country: be
  • Hi
    • nacho.works
Re: Triangle waveform generation using Opamp
« Reply #4 on: September 16, 2026, 09:15:29 am »
You can also build a classic relaxation oscillator and instead of take the square wave at the output, tap of the triangle that's present at the capacitor. You'll need to buffer it of course, but its a nice signal and you can control the frequency while preserving the amplitude.

Regarding the question: I mostly use KiCad for simulation, i find that for oscillators and similar types of circuits, changing the 'Use initial conditions' setting can sometimes help getting the circuit going.
« Last Edit: September 16, 2026, 09:17:56 am by Obin »
 

Offline Obin

  • Regular Contributor
  • *
  • Posts: 125
  • Country: be
  • Hi
    • nacho.works
Re: Triangle waveform generation using Opamp
« Reply #5 on: September 16, 2026, 09:31:31 am »
The circuit you posted is not really an oscillator/"triangle generator", it behaves more like an integrator.
You apply a squarewave to the non-inverting input of the opamp, which makes the opamp charge and discharge the cap it has in its feedback loop.

To make them 'generate' triangles, you need to feed it with a squarewave that is tuned to the RC of the circuit, so that the capacitor never completely charges or discharges.

edit: I noticed my trace labels are not in the print screen, yellow is the square wave input, and the triangle is taken at the output.
« Last Edit: September 16, 2026, 09:33:09 am by Obin »
 

Offline Terry Bites

  • Super Contributor
  • ***
  • Posts: 3155
  • Country: gb
  • Recovering Electrical Engineer
Re: Triangle waveform generation using Opamp
« Reply #6 on: September 16, 2026, 09:46:37 am »
This explanation relates to a classic integrator comparator circuit.
 

Offline SteveThackery

  • Super Contributor
  • ***
  • Posts: 3302
  • Country: gb
  • 50 year novice
Re: Triangle waveform generation using Opamp
« Reply #7 on: September 16, 2026, 10:18:09 am »
The video shows the use of a potentiometer to balance the waveform around 0V. Couldn't the same thing be achieved by adding a capacitor to the output, thus blocking the DC component?
 

Offline Terry Bites

  • Super Contributor
  • ***
  • Posts: 3155
  • Country: gb
  • Recovering Electrical Engineer
Re: Triangle waveform generation using Opamp
« Reply #8 on: September 16, 2026, 02:53:15 pm »
Yes, But I'd worry about distortion if the RC time constant is too short.
Its the unequal excursions at the comparator output that create the offset.
The pot is the simplest solution but the comparator is still banging the rails creating all that recovery time stuff.

Maybe inject a signal into the comparator ninv for some pwm action?
Hmm, I might just give that whorl some time lateur....

I'm sure the video used margarine (the next step down from a jelly bean) quality opamps.
« Last Edit: September 16, 2026, 03:18:04 pm by Terry Bites »
 
The following users thanked this post: SteveThackery

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 2276
Re: Triangle waveform generation using Opamp
« Reply #9 on: September 16, 2026, 09:27:50 pm »
The image of the circuit, i am trying to write the mathematical equation i will post once done.

Hello there,

[see attachment with slightly different circuit]

Your circuit is not exactly an integrator.  It's an integrator WITH a pulsing offset that pulses with the input pulse amplitude.  That means you might get a sawtooth out of it some how, but to get a regular  triangle (sloping up then sloping down, then repeating) you should use an inverting configuration rather than a non-inverting configuration.  The non-inverting variation causes the circuit to have other characteristics that a standard integrator does not have.

A regular integrator has a time equation:
Vout=V1*t/RC

but your circuit has:
Vout=V1*(t/RC+1)

and that pesky "+1" is what prevents it from working as a regular integrator.

We can get rid of that "+1" in a couple different ways, but without adding any more parts I think the only way is to change where you are taking the output Vout from.  Instead of taking it from the output of the opamp referenced to ground, take it from the output of the op amp referenced to the input voltage V1, and run V1 from some negative voltage to the same value positive voltage.  For example, plus and minus 5 volts.
If you don't want do it that way then you probably have to use another op amp set up as a subtractor, subtracting the input pulse voltage from the op amp output voltage, and get that working.
The simpler way though is to set it up as an inverting amplifier with capacitor feedback.

Effectively what the new output VoutB in the attachment is doing is subtracting the input voltage V1 from the output because the output ends up having that added to it.
The normal integrator again with a specified drive voltage is:
Vout=V1*t/RC

and your circuit is:
Vout=V1*(t/RC+1)

which is the same as:
Vout=V1*t/RC+V1

When we subtract the input voltage from the output, we effectively do this:
Vout=V1*t/RC+V1-V1

and since V1-V1 cancel, we get the normal looking output:
Vout=V1*t/RC

The other problem though is that this is pure theory, and although it works perfectly in pure theory, in practice you have to compensate for some capacitor voltage ratcheting, which can quickly saturate the output and stop the circuit from working.
The usual 'fix' is to connect a large value resistor across the capacitor so that the DC gain is lower.  This works to some degree, if you can put up with a small amount of nonlinearity, and some output offset anyway.
If you don't like that idea, then you have to incorporate a reset circuit that resets the capacitor before each new cycle.  Although that has been used in precision integrator circuits (like the very old digital meters) that can be quite a pain to add to the simple circuit.

Again the simpler idea is to use an inverting setup rather than noninverting as that eliminates the offset problem, and then all you have to do is think about the capacitor voltage ratcheting.

See the attachment for a way to get the triangle from your circuit, in theory.
« Last Edit: September 16, 2026, 09:31:01 pm by MrAl »
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5323
  • Country: nl
Re: Triangle waveform generation using Opamp
« Reply #10 on: September 17, 2026, 01:27:49 am »
 How much experience do you have with simulators?

There are very common problems such the pin mapping between the graphics you see on screen and the actual model not being correct, or even the a model for the opamp that is somehow faulty or even completely wrong. This is especially common with KiCad / ngSpice because models are not included by default, and pin mapping is intended for IC footprints (which is often different from spice model pin mappings). It's not a big problem, but it is an extra step and you have to verify it works.

I also find the labeling of the power supply connections confusing. The most obvious method is to simply use two positive voltage sources, put them in series, put GND in the center, +15V on the top and -15V on the bottom. Now I have to make the added assumption that "-15" actually results in the polarity of the voltage source flipping. Plotting the DC operating points is also a quick check that your circuit has some sense of reality.
« Last Edit: September 17, 2026, 01:42:56 am by Doctorandus_P »
 

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: Triangle waveform generation using Opamp
« Reply #11 on: September 17, 2026, 04:30:47 am »
I am confused with the circuit, mixing all kinds of analysis, I have changed the configuration to inverting, few questions i have
a. Does the capacitor get discharged through 10kOhm during the Off time of the PWM period?
b. What is the capacitor charging equation and discharging equation?
c. The equation i have written for Vout is it correct?


 
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5323
  • Country: nl
Re: Triangle waveform generation using Opamp
« Reply #12 on: September 17, 2026, 04:53:04 am »
Connecting your power sources with wires instead of labels does not improve readability. That was nit what I was hinging at.

For the rest, your tree questions are irrelevant before it's clear whether you can do any sort of simulation at all.
And once you have any sort of simulation working, you can answer those question easily yourself by just running the simulation.

So start with the simple things, get those working first, and then extend in small steps.

Have you done any simulation that "worked" and has sensible results? The simplest would be of an RC combination and then measure the response.

After that, you can for example add an opamp, configure it as a buffer and verify that works. It seems (maybe too) simple, but it verifies the model of the opamp works (including power connections and such).
 
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5323
  • Country: nl
Re: Triangle waveform generation using Opamp
« Reply #13 on: September 17, 2026, 04:58:21 am »
Another option is to start with a complete working example. It can be a different kind of circuit. Then you can modify it in little steps towards the circuit you want to test / build yourself.

It's another way of eliminating (unknown) variables.
« Last Edit: September 17, 2026, 05:00:07 am by Doctorandus_P »
 

Offline MariuszD

  • Frequent Contributor
  • **
  • Posts: 658
  • Country: pl
Re: Triangle waveform generation using Opamp
« Reply #14 on: September 17, 2026, 07:13:58 am »
Your circuit is an integrator. As an integrator, it has good parameters and therefore is not suitable for use in the application you want to use it for.

Any offset at the input causes drift.
2916624-0

To eliminate this flaw, the integrator needs to be closed in the comparator's closed loop. The second solution is to worsen the performance of the integrator by shunting the capacitor with a resistor.

 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 909
  • Country: ie
    • LinkedIn
Re: Triangle waveform generation using Opamp
« Reply #15 on: September 17, 2026, 10:04:27 am »
Quote
Does the capacitor get discharged through 10kOhm during the Off time of the PWM period?
No. To discharge capacitor you need to generate symmetric signal - +/- 5V
 

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: Triangle waveform generation using Opamp
« Reply #16 on: September 17, 2026, 10:07:18 am »
I tried with simple RC network and then added opamp(considering example opamp circuit). The triangle is getting saturated.
 

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: Triangle waveform generation using Opamp
« Reply #17 on: September 17, 2026, 10:09:48 am »
Quote
Does the capacitor get discharged through 10kOhm during the Off time of the PWM period?
No. To discharge capacitor you need to generate symmetric signal - +/- 5V

Symmetric signal to be generated at the output?
 

Offline MariuszD

  • Frequent Contributor
  • **
  • Posts: 658
  • Country: pl
Re: Triangle waveform generation using Opamp
« Reply #18 on: September 17, 2026, 11:17:50 am »
The triangle is getting saturated.
The integrator circuit does not have a defined initial state. So at the start, it might already be saturated.
2916804-0
After adding the resistor, the system will tend toward zero, but the waveform will not be perfectly linear.
2916800-1
 

Offline Andree Henkel

  • Regular Contributor
  • *
  • Posts: 242
  • Country: de
Re: Triangle waveform generation using Opamp
« Reply #19 on: September 17, 2026, 12:25:21 pm »
attached is a two op triangle gen we use in a production pcb
not every op can be used as comparator / schmitt trigger, but the used one can

we use it to modulate the "ground" pin of a frequency set resistor of a DC/DC-converter - that is the 51k at output
so we get a spread spectrum modulation of the DC/DC to reduce FFT peaks caused by its switching frequency and harmonics of it

R26+C47 set frequency
R18+R31 set amplitude
 
The following users thanked this post: Vihaan

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 2276
Re: Triangle waveform generation using Opamp
« Reply #20 on: September 17, 2026, 12:38:05 pm »
Quote
Does the capacitor get discharged through 10kOhm during the Off time of the PWM period?
No. To discharge capacitor you need to generate symmetric signal - +/- 5V

Hi there,

That only works in pure theory with an ideal op amp and everything else absolutely perfect.
For example, if the input wave is even 0.1 percent different from positive peak to negative peak, we end up integrating that input offset and it ratchets up and up until the output saturates.  This is like for a pulse that is say +5.002v down to -4.998v.  The time it takes to saturate can be very fast but it depends on the resistor also.

With a feedback resistor, the output offset depends on the DC gain which is R2/R1, so if R2=100k and R1=1k, the gain is 1000, so with a 2mv input offset (like that +5.002v example) the output offset is 2 volts, which is often significant but better than saturation.
Lowering the feedback resistor to 10k means the gain is then only 100, so a 2mv offset leads to a 0.2 volt output offset, far better.
The drawback is of course some curve to the slope, but at the end of a 1ms pulse the percentage difference is only about 2.5 percent, which is not real bad.  With 100k it is 10 times less than that, but then we have the increased output offset.

This is without considering the normal input offset of the op amp itself, which also has the same effect: the output can saturate with no feedback resistor, so the feedback resistor has to be chosen with that in mind as well.

 

Offline MrAl

  • Super Contributor
  • ***
  • Posts: 2276
Re: Triangle waveform generation using Opamp
« Reply #21 on: September 17, 2026, 12:48:58 pm »
I tried with simple RC network and then added opamp(considering example opamp circuit). The triangle is getting saturated.


Hi,

With no resistor in the feedback path (across the cap) the DC gain could be higher than 100000, which means even the slightest input offset gets amplified until the output saturates.  You have to add a feedback resistor.

The value of the feedback resistor depends on how much curve you can tolerate in the output response which will not be a 'perfect' slope.  The curve may not be that bad though.  For example, if you use a 10k feedback resistor the difference at the end of a pulse of 1ms would only be about 2.5 percent different than with an ideal integrator.  The output offset then depends on the op amp input offset and the DC offset of the pulses coming in.  The pulses coming in really should be plus and minus something, like +1 and -1 which I think you are trying now.
Beware the simulators will output a nearly perfect pulse so to do a good test you should introduce a small amount of offset at least.  For example, instead of plus and minus 1v, try +1.002v and -0.998v for the plus values, once you choose a feedback resistor value.
 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 909
  • Country: ie
    • LinkedIn
Re: Triangle waveform generation using Opamp
« Reply #22 on: September 17, 2026, 12:49:12 pm »
Quote
That only works in pure theory with an ideal op amp and everything else absolutely perfect.
Of course, and this was already mentioned in response No. 2.
 

Offline Chingy

  • Newbie
  • !
  • Posts: 4
  • Country: cn
  • Support engineer, DMM & clamp meter manufacturer
    • Chinyoo
Re: Triangle waveform generation using Opamp
« Reply #23 on: September 18, 2026, 07:02:08 am »
Hi Vihaan,

Using a PWM to generate a triangle wave is feasible, but you need to shift your approach: you don't need simple capacitor charging and discharging, you need an op-amp integrator circuit. Connect the non-inverting input to ground, feed the PWM through a resistor to the inverting input, and place a capacitor in the feedback loop. The integrator will convert your PWM square wave into a triangle wave, and the output slope is determined by Vin divided by R times C. For component selection, if your PWM frequency is f, the period is T equals 1 over f, and for full-scale output you should roughly satisfy R times C approximately equal to T over 4. For example, with a 1kHz PWM giving a 1ms period, if you choose R as 10k ohms, then C should be around 25nF. As for why your LTSpice simulation fails, the most common causes are missing op-amp power supplies, no DC feedback path causing saturation, or incorrect PWM source settings, so check that your op-amp has dual supplies like plus and minus 12V. For debugging, first replace the PWM with a simple square wave source and verify the integrator output. If you see a sawtooth or saturation, reduce the input resistor or increase the feedback capacitor. Also remember to set an initial condition such as .ic V(out)=0 in the simulation, otherwise the integrator may not start properly. Good luck with your simulation.
Support Engineer at CHINYOO — multimeter & clamp meter factory in Wenzhou, China. Happy to help with measurement questions.  www.chinyoo.com
 
The following users thanked this post: Vihaan

Online VihaanTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: Triangle waveform generation using Opamp
« Reply #24 on: September 18, 2026, 09:53:57 am »
Thank you the simulation seems to work.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf