Ngspice voltage source: Square wave +/-12V but 0V when not active?

At the present I have a voltage source in the kicad schematic:
VPULSE y1=-12 y2=12 td=10m tr=2u tf=2u tw=500u per=1m np=30 ac=0

Before and after the pulses, the output is at -12V.

Is there a way to output 0V before and after the pulses (during the td=10m time and after np=30 has been finished) but still have the pulses go between -12V and +12V?

I have looked at the ch. 4 of the ngspice manual, but it is not immediately obvious to me. Hopefully someone has already done it or knows how to! :slight_smile:

(Oh, I run the nightly with ngspice 43.)

Maybe using a PWL (Piece Wise Linear) source is more suited to your application.

1 Like

Ok, I tried that, and it looks promising.

But I don’t figure out how to specify the repeat and delay (r=, td=) parameters in kicad?

There is a “code” tab in the simulation model editor, and I can write them there, but nothing happens when I simulate, and the next time I look in the code tab they are gone…

Edit by double click on the Eeschema canvas or in the ‘symbol Properties’ window, Fields name Sim.Params.

1 Like

Holger beat me by a few minutes by posting while I was reading the ngSpice manual. The “r” value adds a repeat parameter.

4.1.4
Piece-Wise Linear
General form:
PWL ( T1 V1 < T2 V2 T3 V3 T4 V4 … >) <r = value > < td = value >
Examples:
VCLOCK 7 5 PWL (0 -7 10 NS -7 11 NS -3 17 NS -3 18 NS -7 50 NS -7) + r =0 td =15 NS

Each pair of values (T i , V i ) specifies that the value of the source is V i (in Volts or Amps)
at time = T i . The value of the source at intermediate values of time is determined by
using linear interpolation on the input values. The parameter r determines a repeat time
point. If r is set to -1 or is not given, the whole sequence of values (T i , V i ) is issued once
only, then the output stays at its final value. If r = 0, the whole sequence from time 0 to
time Tn is repeated forever. If r = 10ns, the sequence between 10ns and 50ns is repeated
forever. The r value has to be one of the time points T1 to Tn of the PWL sequence. If
td is given, the whole PWL sequence is delayed by the value of td. Please note that for now r and td are available only with the voltage source, not with the current source.

Is there any way to repeat a finite number of cycles?
It seems to only say that the sequence is repeated forever in the manual?

I want a burst of pulses, then some time after to look at what happens after the pulses have stopped.

How many pulses? You can put the whole burst into the PWL.

Apparently you can also read the PWL parameters from a file, so you can create a quite complex pulse stream by some external script.

It’s also possible to read WAV files as input, but I don’t know details about that.

In this case 30 pulses like in the first example.

I did attempt to write some 10-12 pulses into the PWL manually, and it works. Is just somewhat tedious. Do you know where I can find the details about reading them from a file?

I have not clicked though on the links, but this search should get you close:

https://forum.kicad.info/search?expanded=true&q=wav%20%23schematic%3Asimulation-ngspice%20order%3Alatest

Another option is to use two sources and an “ideal” switch. Never tried it myself but I’m quite sure there are some simple primitives you can combine to generate a complex test signal. It also makes it more visual, which is also a bonus.

It looks like ch. 12.2.9 in the ngspice manual describes piecewise linear with values read from file.

Some discussion is here:

1 Like

There are many ways, e.g. using switches or multipliers etc.

One way might be the Analog multiplier example from More simulation examples for KiCad/Eeschema/ngspice - #4 by holger.
One input is the pulse sequence (unlimited) of interest, the other is a gating pulse between 0 and 1, setting the output to 0 when not being 1.

1 Like