# How to prevent square wave oscillation circuit from producing backwards sawtooth shapes in LTSpice4

In a related question, "simple, energy efficient circuit to make single IR LED blink", the following circuits are shared in the (current) third answer:

I entered these into LTSpiceIV and the output is unstable, sometimes producing a nice square wave, and other times producing a backwards sawtooth ramp-down shape.

What is really going on here? I am a software developer trying to learn analog electronics, and have been playing around with the Joule Thief and other oscillating circuits, as well as trying to learn about SMPS converters, and would like to know how to diagnose and prevent unstable behaviors like this one in general, but I do really want to know about this one in particular. If I had an oscilloscope, I would build these and try to play with them.

Below is a screen shot showing lots of the backwards sawtooth wave shapes, using 2N2222, 2N2907.

I also tried different transistor-pairs; 2N3904, 2N3906 and 2N4401, 2N4403 were better, but still had problems.

What follows is the contents of the LTSpice-IV *.asc file, so you can paste this into a text file and name it with an ".asc" extension, and then you should be able to (on a PC) directly open it up in LTSpice IV and run the simulation to reproduce what I am experiencing.

Version 4
SHEET 1 1584 680
WIRE 352 -64 -176 -64
WIRE 512 -64 352 -64
WIRE 352 -32 352 -64
WIRE -176 0 -176 -64
WIRE 16 64 -16 64
WIRE 240 64 80 64
WIRE 352 64 352 32
WIRE -16 112 -16 64
WIRE -16 112 -48 112
WIRE 240 112 240 64
WIRE 288 112 240 112
WIRE -16 144 -16 112
WIRE 16 144 -16 144
WIRE 240 144 240 112
WIRE 240 144 96 144
WIRE 512 144 512 -64
WIRE -176 160 -176 80
WIRE -112 160 -176 160
WIRE 240 176 240 144
WIRE 272 176 240 176
WIRE -176 240 -176 160
WIRE 32 240 -176 240
WIRE 352 240 352 160
WIRE 352 240 96 240
WIRE 352 256 352 240
WIRE 512 272 512 224
WIRE 512 272 464 272
WIRE 464 304 464 272
WIRE -48 368 -48 208
WIRE 352 368 352 336
WIRE 352 368 -48 368
WIRE 512 368 512 272
WIRE 512 368 352 368
FLAG 464 304 0
FLAG 272 176 C1
SYMBOL voltage 512 128 R0
SYMATTR InstName V1
SYMATTR Value 4
SYMBOL pnp 288 160 M180
SYMATTR InstName Q1
SYMATTR Value 2N2907
SYMBOL npn -112 112 R0
SYMATTR InstName Q2
SYMATTR Value 2N2222
SYMBOL res 112 128 R90
WINDOW 0 0 56 VBottom 2
WINDOW 3 32 56 VTop 2
SYMATTR InstName R1
SYMATTR Value 1K
SYMBOL cap 80 48 R90
WINDOW 0 0 32 VBottom 2
WINDOW 3 32 32 VTop 2
SYMATTR InstName C1
SYMATTR Value 10nF
SYMBOL cap 96 224 R90
WINDOW 0 0 32 VBottom 2
WINDOW 3 32 32 VTop 2
SYMATTR InstName C2
SYMATTR Value 10µF
SYMBOL res -192 -16 R0
SYMATTR InstName R2
SYMATTR Value 330K
SYMBOL res 336 240 R0
SYMATTR InstName R3
SYMATTR Value 22
SYMBOL LED 336 -32 R0
SYMATTR InstName D1
SYMATTR Value LXK2-PW14
SYMATTR Description Diode
SYMATTR Type diode
TEXT 8 -24 Left 2 !.tran 60s startup


Finally, I entered in the second circuit, which is just the first circuit inverted and polarity-switched, but it won't even oscillate.

• I think your circuit might be at fault. Using this in a different simulator (the falstad.com/circuit one, specifically) I see pulses of over three amps every ~1s; neither a square wave nor a sawtooth wave. I haven't manually analyzed the circuit, though, to see what the problem with it is. – Hearth May 22 '17 at 22:26
• Errr... where is C1? Edit: never mind, I deleted the extra branch that led to nothing and still behaves badly. – Sredni Vashtar May 22 '17 at 22:49
• @SredniVashtar Make sure $C_2$ doesn't have garbage in its value and try setting UIC and a minimum timestep of 100 us. – jonk May 22 '17 at 22:59
• To the OP. Try using UIC and also using a minimum timestep of $20-100\:\mu\textrm{s}$ on your .TRAN card. See if that helps you. – jonk May 22 '17 at 23:02
• @Jonk , with a max timestep of 50 us (.tran 0 12s 0 50u startup ) I get nice spikes without any sawtooth. The pulses are evenly spaced (there is a fast spike in diode current, then a short simil-rectangular pulse. UIC does not seem to make a difference at this point. It appear the timestep did the trick. But I would not call this a "Square wave oscillation circuit". Just a "blinker". – Sredni Vashtar May 22 '17 at 23:15

When I see these behaviors, it's usually because of the solver choice, the automatic step size that LTSpice uses as a delta-time, or else something to do with the reltol option.

This circuit really is critical in the sense that there are very short LED pulses separated by a long period during which $C_2$ charges up. So LTSpice might incorrectly choose a minimum time step here and also this may compound with the relative tolerances, too.

So. The first thing I usually try with astable multivibrators like this is to set the UIC flag in the .TRAN to make sure that it doesn't attempt to find a quiescent point, instead. If that doesn't do it, then I set the minimum timestep to something painfully short and see if that gets the needed time resolution. And the final trick I try is to set RELTOL to a small value.

(Sometimes, it's about the solver itself. But usually not. You can change it in the options dialog box though.)

In this case, the minimum timestep does appear to clear things up. But it is very slow. It's a lot easier to just add this Spice line to your schematic:

.options reltol=0.0000001


That should help. (But feel free to combine that with a minimum timestep. I think $100\:\mu\textrm{s}$ would also be fine, though to be sure I'd go to $20\:\mu\textrm{s}$ if I left reltol at its default value.)

• the tolerance option does not appear to make any difference in my case, but that is because the output was already fine. Anyway, +1 for both the tips. – Sredni Vashtar May 22 '17 at 23:28
• @SredniVashtar Remove the minimum time period now. Should still work, but faster now. – jonk May 22 '17 at 23:30
• Indeed it does. I should have read with more attention. :-) – Sredni Vashtar May 22 '17 at 23:32