ENع
←SPICE
Lesson S1 · Special topics

Simulating circuits with SPICE

14 min11 steps5 questionsIntermediate
About this lesson

SPICE solves circuits from a text description called a netlist. Follow one RC circuit, a 10 V source, R1 = R2 = 1 kΩ and C1 = 1 µF, from netlist to answer. See how SPICE numbers the nodes, stamps each part into a modified-nodal-analysis matrix, finds the DC operating point (v(2) = 5 V, I(V1) = −5 mA), and then steps through time by turning the capacitor into a resistor and a current source at every step. It builds on Nodal analysis, Solving circuit equations with matrices and The RC circuit: natural and step response, and on the Hardware lessons on Ohm's law and on resistors and capacitors.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S1 · Interactive
01 / 11

A circuit as text

SPICE is a program that solves circuits. You describe the circuit as a netlist: plain text, one line per part. Each line gives the part's name, the two nodes it joins and its value. The first line is only a title.

the netlist (input deck)

 1  RC divider charging
 2  V1 1 0 DC 10
 3  R1 1 2 1k
 4  R2 2 0 1k
 5  C1 2 0 1u IC=0
 6  .op
 7  .tran 0.1m 3m UIC
 8  .end

line 1 is always the title: SPICE skips it.
Lines starting with . are commands.
SPICE: Simulation Program with Integrated
Circuit Emphasis, UC Berkeley, 1973.
Loading figure…
or ← →
Read the lesson step by step
  1. A circuit as text

    SPICE is a program that solves circuits. You describe the circuit as a netlist: plain text, one line per part. Each line gives the part's name, the two nodes it joins and its value. The first line is only a title.

    the netlist (input deck)
    
     1  RC divider charging
     2  V1 1 0 DC 10
     3  R1 1 2 1k
     4  R2 2 0 1k
     5  C1 2 0 1u IC=0
     6  .op
     7  .tran 0.1m 3m UIC
     8  .end
    
    line 1 is always the title: SPICE skips it.
    Lines starting with . are commands.
    SPICE: Simulation Program with Integrated
    Circuit Emphasis, UC Berkeley, 1973.
  2. Number the nodes

    A node is a point where parts join. SPICE needs every node named, and the ground node must be called 0. Here node 1 is the source's top, node 2 joins R1, R2 and C1, and node 0 is ground.

    node 0   ground, the 0 V reference
    node 1   V1 (+) and R1
    node 2   R1, R2 and C1
    
    rules (ngspice manual §2.1.3–2.1.4)
      the ground node must be named 0
      every node needs a DC path to ground
      every node needs at least two connections
  3. Read one line

    The line R1 1 2 1k says: a resistor called R1 joins node 1 to node 2 and has 1 kΩ. Letters scale numbers: k is 10³, u is 10⁻⁶, m is 10⁻³. Careful: in SPICE m means milli; mega is meg.

    R1   1   2   1k
    │    │   │   └ value: 1k = 1 × 10³ Ω
    │    │   └ second node
    │    └ first node
    └ name: the first letter is the type
         R resistor, C capacitor,
         V voltage source, L inductor
    
    scale letters
      meg 10⁶   k 10³   m 10⁻³
      u 10⁻⁶    n 10⁻⁹  p 10⁻¹²
  4. The source and its sign

    V1 1 0 DC 10 puts a 10 V source with + on node 1 and − on node 0. SPICE counts source current flowing into the + node, through the source, out at −. A source that delivers power shows a negative current.

    V1   1   0   DC 10
         +   −   value
    
    SPICE's reference for I(V1):
      into + (node 1), through V1, out of − (node 0)
    
    That is the passive sign convention:
      p = v × i is the power V1 ABSORBS.
      A source that supplies power has
      I(V1) < 0 and p < 0.
  5. Each part stamps the matrix

    SPICE writes Kirchhoff's current law at every node, using modified nodal analysis. Each resistor adds its conductance, G = 1/R = 1 mS, into fixed cells. The source adds a row saying v(1) = 10 V. Watch the cells fill.

    unknowns  x = [ v1  v2  I(V1) ]
    
    R1 (1–2, G = 1 mS): +G at (1,1) and (2,2)
                          −G at (1,2) and (2,1)
    R2 (2–0, G = 1 mS): +G at (2,2) only
             (node 0 has no row)
    V1 (1–0): 1 at (1,3) and (3,1), 10 in b
    
      [ 1m  −1m  1 ] [v1]   [ 0]
      [−1m   2m  0 ] [v2] = [ 0]
      [ 1    0   0 ] [iV]   [10]
  6. The operating point (.op)

    At DC a capacitor carries no current, so .op treats C1 as open. Solving the matrix gives v(1) = 10 V, v(2) = 5 V and I(V1) = −5 mA. The minus sign means V1 delivers 50 mW; R1 and R2 take 25 mW each.

    .op   C1 open (no DC current)
    
      v(1)  = 10 V
      v(2)  = 5 V
      I(V1) = −5 mA
    
    hand check: divider 10 × 1k/(1k + 1k) = 5 V
    
    power absorbed (passive sign convention)
      V1  10 × (−5 mA) = −50 mW
      R1  25 mW    R2  25 mW
      sum −50 + 25 + 25 = 0
  7. Transient analysis (.tran)

    .tran 0.1m 3m UIC asks for the voltages from 0 to 3 ms. UIC means start from the IC values in the netlist, so C1 starts empty at 0 V, not at the .op answer. Now the capacitor's current matters.

    .tran tstep tstop UIC
      tstep  0.1 ms   print step
      tstop  3 ms
      max step (ngspice default)
             min(tstep, tstop/50) = 0.06 ms
      UIC    use initial conditions:
             skip the .op, start from IC=0
    
    C1 law:  i = C · dv/dt
    A derivative: the answer at each moment
    depends on the moment before.
  8. A capacitor becomes a resistor and a source

    SPICE cannot solve a derivative directly. Over one time step h it uses i = C·(v_now − v_before)/h. That is a conductance GC = C/h = 10 mS in parallel with a current source GC·v_before: parts a DC solver already handles.

    backward Euler, step h = 0.1 ms
    
      i = C (v_now − v_before) / h
        = (C/h)·v_now − (C/h)·v_before
           └ conductance  └ current source
    
      GC  = C/h = 1 µF / 0.1 ms = 10 mS
      Ieq = GC · v_before   (into node 2)
    
    This "companion model" turns every
    time step into a DC problem.
  9. One time step is one DC solve

    First step, h = 0.1 ms. C1 was at 0 V, so Ieq = 0 mA. Node 2 sees 1 + 1 + 10 = 12 mS. KCL gives v(2) = 10 mA ÷ 12 mS = 0.833 V, the first plot dot.

    KCL at node 2, step 1 (t = 0.1 ms)
    
      (v2 − 10)·1m + v2·1m + v2·10m − Ieq = 0
      Ieq = GC × 0 V = 0
      12m · v2 = 10m
      v2 = 0.833 V
    
    matrix: cell (2,2) grows by GC to 12 mS,
    b gets Ieq in row 2. Same matrix shape
    as the .op; only two numbers changed.
  10. March to 3 ms

    Repeat: each answer becomes the next step's v_before. v(2) climbs 0.833, 1.53, 2.11 V and levels off at 5 V, the .op answer. Hand check: τ = (R1 || R2)·C1 = 0.5 ms, so v(2) = 5(1 − e^(−t/τ)).

    step   t (ms)   Ieq (mA)   v(2) (V)
      1    0.1      0          0.833
      2    0.2      8.33       1.528
      3    0.3      15.28      2.106
      4    0.4      21.06      2.589
      5    0.5      25.89      2.991
     ...
     30    3       ...        4.98
    
    exact: τ = 500 Ω × 1 µF = 0.5 ms
           v(2) = 5(1 − e^(−t/τ))
  11. Smaller steps, smaller error

    At t = τ the exact answer is 3.16 V. Steps of 0.1 ms give 2.99 V; steps of 10 µs give 3.14 V, almost on the exact curve. Real SPICE uses a better rule (trapezoidal) and picks h from an error estimate.

    v(2) at t = τ = 0.5 ms
    
      exact            3.161 V
      h = 0.1 ms       2.991 V   error 0.17 V
      h = 10 µs       3.142 V   error 0.018 V
    
    backward Euler: error ∝ h (first order)
    ngspice default: trapezoidal (second order),
    step chosen from a truncation-error
    estimate, never above tmax.

Quiz Unlocks after the last step.