How to simulate an OFF switch/button?

I tried this, but it does not work:

.SUBCKT SWITCH 1 2
S1 1 2 SW off
.ENDS SWITCH

It’s ON instead of OFF.

Please have a look at the ngspice manual (http://ngspice.sourceforge.net/docs.html) chapts. 3.2.14 and 3.2.15 for switch instance line S… and the corresponding switch model.

2 Likes

See also Adding spice components that do not exist in pspice library

I figured it out.

The relay’s pins were mismatched, so I reordered them (3 4 1 2).

(It’s not a simulation of relay, I am using the relay symbol because did not found a generic “voltage controlled switch” symbol.)

Button’s subckt:

.SUBCKT MyButton_NO 1 2
S1 1 2 n001 0 MySW_NO
* Initial value, pulsed value, delay time, rise time, fall time, pulse width *
V1 n001 0 pulse(0 10 0.10 0.02 0.02 0.05 100)
.model MySW_NO sw vt=1 vh=0.2 ron=1 roff=1000MEG
.ENDS MyButton_NO

Relay’s subckt (for two types of relays - with NO switch and with NC switch):

.SUBCKT MyRelay_NO 1 2 3 4
S1 3 4 1 2 MySW_NO
.model MySW_NO sw vt=1 vh=0.2 ron=1 roff=1000MEG
.ENDS MyRelay_NO


.SUBCKT MyRelay_NC 1 2 3 4
S1 3 4 1 2 MySW_NC
.model MySW_NC sw vt=1 vh=0.2 ron=1000MEG roff=1
.ENDS MyRelay_NC

I learned that:

  • It does not work unless one of the pins of the voltage source is connected to ‘0’.
  • the 1 and 2 pins (of the voltage controlled switch) are the switch, the 3 and 4 are voltage inputs.
  • The pin 3 is the + input (it does not work when I change S1 1 2 n001 0 MySW_NO to S1 1 2 0 n001 MySW_NO).
1 Like

However, I don’t understand why I can’t add the .SUBCKT code as a text, like I add .TRAN 0.00001 0.2 0 (the .TRAN directive works as expected).

Also, I can add .INCLUDE filename.lib, but when I add the contents of that file it is ignored by the program.

When I click “Load directives from schematic” only the .TRAN line is loading.

However, I can’t select from the menu the model (need to write it manually):

What I understand is that, ngspice like to use the net 0 as global GND for reference. KiCad use GND net name instead of 0, so simulation will not work. Therefore, most of the case it will not simulate correct with the GND symbol.

I used to just labeled a net “0” and connect it instead of GND.
Or like you using the 0 power symbol net also work.
Or connect zero ohm resistor from the GND net to the net that labelled as “0” or “0” power symbol.

Had you try a single text object with multiple lines in schematic with the first line start with “.” character as the KiCad manual talking about? For example and a text object contain all the line below:

.include "XYZ file"
.subckt bla bla bla
bla bla bla...
.ends

KiCad really has no concept of voltage potentials. Meaning it really does not care what you name any of them. For kicad a power symbol is the same as a global label.

You are however right that the default lib has no 0 symbol in the normal power port lib. (There is however one in the legacy spice lib) The use of GND is “just” because most circuit designers are used to this name as the common potential.

I think KiCad should have some setting to have GND net name as 0 for spice by default - And user can chose difference net name to map to 0 as needed. May be there are already a solution that is coming which I don’t aware of .

I am not really sure how this would help. To be honest it would feel more like a bodge to be honest. (At least if it is “GND always doubles as 0”)

Maybe a better approach is to be able to select which net name is used as 0 on simulation export.

2 Likes

Also, I don’t know what is the purpose of your simulation. But be aware that, it still using very ideal model of switch, not relay. The more realistic mode would need addition of inductor, resistor, and event some coupling capacitor … But is it depend what you try to simulate. Without an inductor in the model your solution will likely to not using diode or some circuit to reduce voltage spike when switching, and that can kill your drive circuit easily…

This also the reason, you may want to have separated schematic for real PCB, and simulation. When you like both in the same schematic, it often require a lot of work, and customize .subckt to have the whole thing work…

Yes, this is why some of us think that integrating KiCAD with NGSpice is a well-intentioned idea that creates more work - and more possibilities for error - than it avoids. Similar to placing a truly useful word-processor into a spreadsheet program, or a pipe wrench that doubles as a hammer: the result is a contraption that sort-of, almost, works but isn’t truly satisfactory.

Dale

The token ‘gnd’ is automatically translated to ‘0’ in ngspice.

If this is not feasible, then one may add
set no_auto_gnd
in spice.rc (ngspice-30 required). Then one has to care for a node named ‘0’ as reference.

Bad example, as every tool has a hammer side to it. :boom:

We need a developer to drop in and tell us whether or not the net name “0” has a special significance in KiCAD, or perhaps net “0” is reserved for some planned future capability. If neither case is true, it would make sense to submit a feature request to use net “0” as the default name for the ground net.

Dale

Don’t use a Zero-Ohm-resistor. This is incompatible for any circuit simulator due to its conductance matrix then requiring an infinite conductance (Not possible). Use a voltage source with dc 0 instead.

1 Like

Aha… that is a great explain for Zero-Ohm-Resistor not working due to internal spice implementation :slight_smile: