# Current controlled switch with negative current?

I am trying to create a current controlled switch, which opens only at negative current, I tried this approach:

Wswitch com no Vcurrent negative_switch
.model negative_switch CSW it < 1nA ih=0 ron=1n roff=1g


but, I got error:

unrecognized parameter (<) - ignored


Any idea? how can use less than in the mode above?

EDIT

I tried to flip the switch as below but worked only at positive value:

Wswitch2 no com Vcurrent negative_switch
.model negative_switch CSW it=1nA ih=0 ron=1n roff=1g

• Could you try making a switch for positive current and connecting it backwards? – user253751 Aug 19 '19 at 4:26
• No, it did not work, kindly check EDIT above – simo Aug 19 '19 at 4:33
• @Simo Of course flipping the switch contacts/nodes will make no difference. You should flip the controlling part, so Vcurrent. – Huisman Aug 19 '19 at 6:10
• I need to keep the controlling part as is, as controlling part might be positive or negative, it's not fixed – simo Aug 19 '19 at 6:14

.

# As suggested earlier: first make it work in LTspice.

Short description:

• Upper circuit contains the controlling current, which is measured by a 0V voltage source Vcurrent.
• The circuit in the middle manipulates the current. I first read your question it should detect both positive and negative currents, hence the name "Vabscurrent" of the 0V voltage source. Make sure current source B1 is oriented correctly and the "additional circuit" is grounded as well.
EDIT
The middle circuit as shown is superfluous, you can simply flip the voltage source Vcurrent if you want the switch to trigger for negative currents. I leave the picture as is, as it shows you can manipulate the current to have the switch trigger to whatever condition you want.
In case you want to have the switch trigger for positive and negative currents, you could use B1 0 N003 I=abs(I(Vcurrent)).
You can set any condition for the switch using Arbitrary Behavioral Current Source, e.g. when a certain condition occurs after 10 seconds: B1 0 N003 I=if( time>10, if(certain condition), 1A, 0A ).
• The lower circuit contains the current controlled switch (CSW) which is controlled by the current through "Vabscurrent". I added a picture of the Component Attribute Editor to show the name of the voltage source should be entered in the Spicemodel line.

After implementing it in LTspice, read the netlist and use/compare it to the netlist you wrote in ngspice.

W1 N005 0 Vabscurrent MySwitch
R1 N005 N004 10
V1 N004 0 5
R2 N001 N002 R={1k-100*time+1k}
Vabscurrent N003 0 0
V2 N001 0 5
B1 0 N003 I=-I(Vcurrent)
Vcurrent N002 0 0
.tran 10
.model MySwitch CSW(Ron=.1 Roff=1Meg It=4m Ih=-.5m)
.backanno
.end

• Thanks for thoughtful answer – simo Aug 19 '19 at 19:38
• How can I print in ngspice the current state of a switch? ex: opened or closed? can I print resistance between two nodes? ex: nodes of the switch – simo Aug 20 '19 at 22:53
• I tried to print current as .print dc i(Vabscurrent), I got Warning: can't parse 'Vabscurrent#branch': ignored Error(parse.c--checkvalid): Vabscurrent#branch: no such vector., is this because I tried to print inside SUBCKT ? – simo Aug 21 '19 at 3:04
• I would think you need to address it like i(V.x1.Vabscurrent) or so. But make a subcircuit in LTspice and find out... – Huisman Aug 21 '19 at 5:50
• @simo You need to explicitely save data from the subcircuits. Section 17.5.61 of the manual: save may store data from nodes or devices residing inside of a subcircuit So, you need to add: save i(V.x1.Vabscurrent) or save V.x1.Vabscurrent#branch. In the examples in the manual they use multiple instances of x1, no idea why (maybe sub-sub-subcircuit?). Anyway, if it fails, you may try save i(V.x1.x1.x1.Vabscurrent) – Huisman Aug 22 '19 at 12:22