I am trying to simulate ON, OFF switch in this simple circuit:

V1 0 1 1.5v
Rfan1 3 4 1
S1 1 4 SW ON
Rwire1 6 5 0.00001
VV1 0 2 dc 0 
VRfan1 3 5 dc 0 
VRwire1 2 6 dc 0 
.dc V1 1.5 1.5 1
.print dc i(VV1) i(VRfan1) i(VRwire1) 

but I got this error:

ngspice stopped due to error, no simulation run!

the circuit I am trying to simulate is like this one:

enter image description here

if I replace the switch with a resistor, the circuit will work normally.

any idea? EDIT

I have fixed my switch as the following:

S1 1 4 98 99 mySwitch ON
.model mySwitch SW vt=0 vh=1 ron=1n roff=10k
  • \$\begingroup\$ Why do you have so many nodes? (Your picture only has 3.) You probably should draw out the diagram indicated by your Spice card deck. It's certainly not the same as the picture you show. Also, switches in Spice have four nodes (see: here) and they require a model, as well. Your switch card doesn't look right to me. \$\endgroup\$ – jonk Jul 26 '19 at 17:56
  • \$\begingroup\$ I am adding components to measure current, those with DC 0 \$\endgroup\$ – simo Jul 26 '19 at 18:06
  • \$\begingroup\$ This simply means you should write that up using the schematic editor so that we can see the full diagram without having to pencil it out from a bunch of cards. Also, you still haven't addressed my lack of understanding of your switch card. Can you explain (or point me to documentation) that shows your switch card is valid Spice? \$\endgroup\$ – jonk Jul 26 '19 at 18:08
  • \$\begingroup\$ draw your circuit. start with node 0 connect V1, assign node 1, draw S1 , etc. If i do it by heart, I see: VV1 0 2 dc 0 then VRwire1 2 6 dc 0 then Rwire1 6 5 0.00001. VRwire is really superfluous. You can just use i(Rwire) to get the current. And VV1 is superfluous for the same reason. In short: Don't use voltage sources for current measurement when you already have components in series with them. It's superfluous* \$\endgroup\$ – Huisman Jul 26 '19 at 18:36
  • 1
    \$\begingroup\$ @simo If you are writing a GUI to generate Spice, you should consider taking the time needed to FULLY understand the Spice cards and their syntax. It's an essential step in preparing yourself to do the job. I believe your generated syntax is strong evidence that you haven't taken the time to read and understand the documentation. Which pretty much means you can't produce a good GUI. If you want to play with switches and read docs and do this without paying money, then get LTspice. \$\endgroup\$ – jonk Jul 26 '19 at 19:18

I get the error:

Error on line 3 :
s1 1 4 sw on
Unable to find definition of model - default assumed

The definition of your switch is incorrect.
It should be of the type SXXXXXXX N+ N- NC+ NC- MODEL <ON><OFF>. You should:

  • define the controls NC+ and NC-
  • define the model (which has the name reserved name SW in your case) by using either .model mySwitch CSW(...) or .model mySwitch CSW(...)
  • not using the name SW as it is reserved as said above (I think that's why it crashes).

So, using the fictional nodes 98 and 99 that control the switch:
s1 1 4 98 99 mySwitch on
.model mySwitch SW vt=1 vh=0.2 ron=10u roff=1G

Do read the manual, section 3.2.14 Switches and 3.2.15 Switch Model (SW/CSW).

In addition to Sorenp's answer, you can also implement the switch by making the value of Rwire1 conditional:
Rwire1 6 5 R={if(time<2,1G,if(time>3,1G,10u))}
Between time is 2 seconds and 3 seconds, Rwire1 has its original value. Before and beyond these time stemps the resistance is 1 GΩ.

  • \$\begingroup\$ Thanks, I am learning allot from you, but I could not find in the documentation the matching definition for S1 3 4 98 99 mySwitch sw ON in your answer, did you mean S1 3 4 98 99 mySwitch ON without sw ? \$\endgroup\$ – simo Jul 26 '19 at 19:16
  • \$\begingroup\$ You're right. I copied/pasted it, but sw should be deleted. It should be S1 3 4 98 99 mySwitch ON \$\endgroup\$ – Huisman Jul 26 '19 at 19:21
  • \$\begingroup\$ @simo BTW, when using a SW (so, controlled by voltage), it's likely to be S1 3 4 98 0 mySwitch ON as the controlling source should be grounded. \$\endgroup\$ – Huisman Jul 26 '19 at 19:29
  • \$\begingroup\$ Can’t I have a switch that is controlled without voltage or current? just mechanical switch ? ( pressing by hand, as we have at home, when turn the lights on.. ) \$\endgroup\$ – simo Jul 26 '19 at 19:33
  • 1
    \$\begingroup\$ @simo "Can’t I have a switch that is controlled without voltage or current? just mechanical switch?" How would ngspice know you pressed this mechanical switch? :-) In spice, you describe*/*model the pressing of the mechanical switch by using the voltage source. Or, by using the conditional resistor at the end of my answer. \$\endgroup\$ – Huisman Jul 26 '19 at 19:44

You can simulate open and closed circuits by replacing the switch with a resistor.

A very large resistance (>10M ohm when the switch is in an open configuration).

A very small resistance (for the closed configuration, try to measure the resistance in the switch when closed).

  • \$\begingroup\$ Thanks, that makes sense, but I am wondering what's wrong with my circuit.. \$\endgroup\$ – simo Jul 26 '19 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.