# How to simulate simple on/off, Single-Pole-Single-Throw switch?

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)
.end


but I got this error:

ngspice stopped due to error, no simulation run!


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

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

• 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. – jonk Jul 26 '19 at 17:56
• I am adding components to measure current, those with DC 0 – simo Jul 26 '19 at 18:06
• 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? – jonk Jul 26 '19 at 18:08
• 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* – Huisman Jul 26 '19 at 18:36
• @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. – 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Ω.

• 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 ? – simo Jul 26 '19 at 19:16
• You're right. I copied/pasted it, but sw should be deleted. It should be S1 3 4 98 99 mySwitch ON – Huisman Jul 26 '19 at 19:21
• @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. – Huisman Jul 26 '19 at 19:29
• 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.. ) – simo Jul 26 '19 at 19:33
• @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. – 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).

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