Skip to content

Latest commit

 

History

History
162 lines (121 loc) · 8.85 KB

File metadata and controls

162 lines (121 loc) · 8.85 KB

NGspice Tutorial

When exploring electronics, getting started is going to be quite annoying. You need lots of components, a breadboard time to put it all together, and even then if a change is desired, you need to do it all over again. In comparison to that, simulation of electronic components and circuits does not require any of the hardware, you have every possible component at hand, and it is additionally possible to perform automatic sweeps accross the various component sets, even if some amount of external software may be needed for that. This assumes that ngspice is already installed and you are running a linux machine.

NGspice is a software that is able to simulate electronic circuits quite accurately and is free to use and modify. This text should guide you to become comfortable in using ngspice for designing circuits, before having them be turned in to actual hardware.

The humble voltage divider

As a first circuit, let us consider the humble voltage divider. This is mostly used to get you used to the notation of things. First we need to specify the divider netlist. A netlist is essentially a textual representation of a circuit diagram. It contains devices and the nodes that interconnect them, as well as the values of the various components being used in the circuit. The following shows the netlist for a voltage divider, which is stored in a file called voltage_divider.circ

.title Voltage Divider
* This is a comment line
* There is a voltage source at the input that provides 10V
Vin in 0 dc 10 ac 0
Rt in out 100
Rb out 0 100
.end

As can be seen, the voltage divider has an input source, and two resistors across the voltage source, with the middle node being the output. The first entry on each line is the name of the component and then the following two entries are the 'nodes' or 'nets' that the component is connected to. The entries after that specify the parameters of the component, which in the case of the resistors it is their resistance. So far so good, but if we want to know how the output voltage is, ther is still two more things we need to do. We need to tell NGspice to load the circuit, and we need to tell NGspice what we want it to do with this cirucuit.

To get NGspice to load the circuit, either ngspice can be started with the path to the circuit file as an argument, or if no argument is usedm by simply calling ngspice and when getting to the ngspice prompt calling source voltage_divider.circ.

Now that the circuit is loaded, tell it to calculate the steady state voltages. This is called the 'operating point', which is why the command in ngspice is calle op. So once the circuit is loaded, call

ngspice 225 -> op

ngspice should respond with:

Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Using SPARSE 1.3 as Direct Linear Solver

No. of Data Rows : 1

showing that it did the analysis and that it produced a single row of data. To show the values it calculated, the print command of ngspice can be used.

ngspice 225 -> print out

should thus print the output voltage. The unit here is V, but with scientific notation. And there we go, we have sucessfully run our first simulation.

Scanning parameter values

So as a next step we might want to see what happens if we change a parameter of our circuit. In the first case let's say we simply want to see what happens if we change the voltage of the input. For this we can run a 'DC sweep' via the dc command.

dc vin 0 10 0.1

which should get you a response along the lines of:

Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

Using SPARSE 1.3 as Direct Linear Solver

No. of Data Rows : 101

This time however we have 101 numbers, as indicated by the output. As there are lots of numbers now, instead of printing them, it is possible to plot them as well. For this we need to chose what to plot, in this case let us use the output voltage. As the input voltage varies from 0 to 10 we expect the output voltage to linearly vary from 0 to 5. Running

plot v(out)

should show a nice linear behaviour.

Besides simply varying a voltage, the resistance may also be varied by the dc command. Varying the resistance of R1 should produce a nice 1/R curve for the voltage at the out node.

dc r1 0.1 1meg 10
plot v(out)

should yield just such a nonlinear curve.

A few more sophisticated circuits

Ok fine we have seen, that if we want the voltage from a voltage divider, we can have ngspice calculate that value for us, however that math is not particularly hard. In the next section we shal take on a few more sophisticated circuits, namely the recrifier and the filter.

The Filter

The Filter is a circuit that is commonly used in signal processing (stuff like transmitting your voice from one side of the planet to the other is a form of signal processing) and measurement. Filters are used to block 'noise' from the environment so that the circuit can focus on processing only the meaningful and relevant parts. This blocking depends on the frequency range of the signal of interest. The ideal Low pass filter will block all signals above a certain frequency, while the ideal high pass filter will block all signals below a certain frequency.

Side Note: Music streaches over a large frequency band (somewhere between 20Hz and 20000Hz) while human voices typically occupy the frequency range up to 3000Hz. Phone calls typically transmit human voices and thus a low pass filter is used to block frequencies above ~4000Hz. This blocking allows for some neat tricks in the phone system while also serving to make a phone call easier to understand in noisy environments. This is also why music sounds so bad on the phone. The circuitry in the phone is not able to properly transmit the high frequency part of the music.

Let's start with a Low pass filter. The low pass filter is very similar to our voltage divider. The filter however swapps out the bottom resistor for a capacitor. So at DC the capacitor charges up to the DC voltage and stays there. If a slowly oscillating AC voltage is applied, the capacitor has enough time to charge through the Rlp resistor, but as the frequency of the AC is increased the capacitor has less and less time to charge, resulting in less voltage appearing accross the capacitor (which is the output voltage), thus the voltage at the output drops with increasing frequency. The frequency where this becomes noticable can be calculated with the formula 1 / (Rlp * Clp) (try putting in the units into this equation and see if they cancel. You should get 1/s as the unit of this expression).

.title Low pass filter
* The sib
Vin in 0 dc 0 ac 1
Rlp in out 1k
Clp out 0 1u
.end

The thing that is interesting about the low pass filter is how the amplitude and the phase of a sine wave changes between the input and the output of the low pass filter. We can of course run a DC analysis, but as discussed before, we will not see much happening, as the capacitor will simply charge up and remain at the final voltage. To get an Idea on how the voltage changes, an AC small signal analysis is performed. The 'small-signal' part will be more important later, when we get in to non-linear circuit elements, but for this example we can ignore the 'small signal' part of the command. So to run an analysis of the low pass filter that is described above we run the following after starting ngspice and loading the low-pass filter circuit:

ngspice 210-> ac dec 100 1 1meg
ngspice 211-> plot v(out) phase(out)

This will produce a plot that shows the amplitude of the output signal (in volts) and the output phase in radian. It can be seen that at low frequencies the amplitude of the output is very close to the amplitude of the input. As the frequency rises the amplitude drops, going closer and closer to 0, while the phase of the output waveform also reduces unit it plateaus around -90deg (-1.6rad). If it still looks a bit unfamiliar to you, the plots are still showing linear, not logarithmic quantities.

ngspice 212-> plot db(out) phase(out) xlog

produces another plot that looks much closer to the textbook, as the signal amplitude is shown in db, while the x axis is logarithmic. In deed, the circuit heavily attenuates signals above 200 Hz, while everything below 10Hz barely notices the voltage divider. We do need to keep in mind that the output node is unloaded. This means that all the charge that flows in through the 'in' node has to flow back out through the 'in' node.

.title loaded low pass filter
Vin in 0 dc 0 ac 1
Rlp in out 1k
Clp out 0 1u
Rload out 0 10k

The Rectifier

This circuit is used essentially everywhere where a battery is being charged. This means that this happens in every smartphone and laptop charger as well as essentially every LED lightbulb.

This circuit uses our first nonlinear component, a Diode. Very roughly, a diode conducts current in one direction but not the other and is thus useful to turn AC voltage into DC voltage.