# LTSpice variable PWM source with two different timings

I'm wondering if anyone has already done this, to save me going down some rabbit hole.

I'm trying to make a pwm that is initially on 100% for some X period of time, then changes to some Y duty cycle at around 40kHz. I know I could make a PWL file, but that seems way too tedious, for doing things on ~100ms+ timelines.

simulate this circuit – Schematic created using CircuitLab

• Make x (simple) PWM as more you need. Then add and/or multiply the waves as needed. Commented Aug 18, 2022 at 17:47

You can do that with a regular pulse as long as the duty cycle and frequency are going to be constant.

1. Set Vinitial to whatever your high voltage is.
2. Set Von to 0 V
3. Set Tdelay to the delay you want at the beginning
4. Set Tperiod to the length of one cycle at the frequency you want
5. Set Ton to the portion of the period you want the waveform to be low to get the duty cycle you want.
6. You might want to change the rise and fall times to something other than the default values.

So if you wanted 40 kHz at 90% duty cycle you would use 25 $$\\mu\$$s period and 2.5 $$\\mu\$$s on time so that it is high for 22.5 $$\\mu\$$s and low for 2.5 $$\\mu\$$s.

• This is exactly why I asked. I was overlooking the simple path. Thanks! Commented Aug 18, 2022 at 17:49
• Not a fan of setting rise and fall to zero. You should always define them since LTspice replaces the zeroes with unknown values it picks itself, which are based on the timesteps it calculates internally. Commented Aug 19, 2022 at 2:57
• @SteKulov If tr, tf are not specified then the formula is min( T - Ton, Ton ) / 10. Commented Aug 19, 2022 at 6:06
• @SteKulov I just went with the defaults and left them blank in the example, it must have filled them in with zeros. Added the suggestion to set them in the answer. Commented Aug 19, 2022 at 6:16
• @GodJihyo You'll need to change the last part to say: 2.5 us = Ton + (tr + tf)/2. Also, it's $\mu\text{s}$ (seconds), not $\mu\text{S}$ (Siemens). Commented Aug 19, 2022 at 6:19

One thing you could do is do two pwm's and then use another switch or something to switch between them at a given time.

Another option would be to have one turn off and then the other turn on, then add the outputs together with a b-source

• For adding them together with behavioral voltage source, do you just set V=V1+V2? Commented Aug 18, 2022 at 17:56
• Yeah, you can use functions in a bv source. But it's v = V(V1)+V(V2), you have to specify if you want the current of the netname or the voltage from the netname Commented Aug 18, 2022 at 18:11
• LTSpice is fun! So turns out you can't pick the voltage V(V1) like that (But you can for current I(V1) ). To get the voltages I had to use the net name on the positive terminal. So V=V(hold)+V(pick) Commented Aug 18, 2022 at 18:16
• Yeah, provided a picture so its easier to understand. You can also use the delays an ncycles to provide some control when the PWM's turn on and off. Commented Aug 18, 2022 at 18:34

If you need a fixed duty cycle PWM then the two solutions above will suffice but, if your aim is to have a time-variable PWM then a simple source will not be sufficient:

V1 acts as the ramp generator and V2 as the controlling DC signal, A1 is the comparator. It's a PWL() source that starts with a value of 1.1 V for 0.1 s, then switches to 0.1 V in 1 us, and then goes on in a ramp until 0.9 V at 1 s. The two OTAs above, A2, A3, form a 2nd order Bessel with fc = 5 Hz, filtering V(out) to show that it does, indeed, vary as V(DC) dictates (shown as the red and blue traces). For the comparator, vt, vh are the threshold and hysteresis settings, vhigh, vlow self-explanatory, and tau, tripdt are temporal helpers to help preserve the sharpness of the edges while avoiding slowing down the simulation.

TLDR: as long as V(DC) is greater than the ramp, the output will be logic 1 (and logic zero if it's less than the ramp). Anything in-between will generate the PWM. It's flexible and allows for a dynamic modification of the output.

• My primitive brain can't wrap around how you did the Bessel with the OTAs. Commented Aug 18, 2022 at 19:09
• @SteKulov It's derived from the middle one (bottom Laplace for confirmation). Also see this file, it's exactly the same approach, only since this one has no zeros, the a coefficients are eliminated in favour of a differential input for the first OTA. Commented Aug 18, 2022 at 19:42
• Nice post, Looks Fancy... Commented Aug 19, 2022 at 0:17
• @VoltageSpike Thank you, hopefully it's also useful. Commented Aug 19, 2022 at 6:07