Author Topic: LTspice and mechanical switch problem  (Read 6355 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
LTspice and mechanical switch problem
« on: July 29, 2020, 01:35:52 pm »
Hi!
I'm migrating from MultiSim to LT and I try to make a simple circuit this kind:
1 LED
1 RES
1 Vsource
1 Mechanical switch.

I can't find any solution from the part list for a mechanical button/switch like in MultiSim so
if I click on it the switch should open or close the circuit, something like a real push button.

I always have to cut the power source circuit to the resistor if I wish to turn off the LED.

I hope you got what I mean.

Any idea how to solve this?

Thank you.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3339
  • Country: gb
Re: LTspice and mechanical switch problem
« Reply #1 on: July 29, 2020, 03:24:23 pm »
LTSpice does not have components that the user can interact with during simulation like the educational simulators.  Instead use the "sw" switch model and control the switch timing using an independent voltage source, which can be configured to give arbitrary switching times if needed.
 
The following users thanked this post: Chriss

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 13133
Re: LTspice and mechanical switch problem
« Reply #2 on: July 29, 2020, 04:02:23 pm »
Its neater to use the 'csw' current controlled switch device, because you don't have to hookup wires to control terminals for it.   For both it and 'sw' you have to supply a .model command to define the switch characteristics. 

N.B. 'csw' is slightly nonintuitive to use - fill in the name of the controlling voltage source on the SpiceModel attribute line and the name of the switch model (from the .model command) on the Value line.  Blame the authors of the original Berkeley SPICE for the inverted attribute order.  If the named voltage source's value is 0, it acts as a pure current probe.

N.B. the current through a voltage source driven by that source has opposite sign to the source's voltage.  To control a 'csw' with minimum  parts, set the internal resistance of the controlling voltage source to -1 and ground both terminals so it converts voltage into current of the same magnitude and sign.

I attach Cswitch.asc, which is a 'csw' example analogous to L.T's supplied 'sw' example: examples\Educational\Vswitch.asc

You can also use a behavioural resistor in terms of time, which may be easier for very simple switching events.  Edit the value to R=<expression> e.g:
Code: [Select]
R=if((time>1)&(time<2)), 1u, 1G)
which is on (one microohm) from time 1 second to 2 seconds, and off, (one gigaohm) the rest of the time.   You can even use the 'csw' symbol for a behavioural resistor - simply change the prefix to R before you make any other changes then close and reopen the dialog.
« Last Edit: July 31, 2020, 05:13:25 pm by Ian.M »
 
The following users thanked this post: exe, Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: LTspice and mechanical switch problem
« Reply #3 on: July 31, 2020, 03:12:42 pm »
Quote
You can also use a behavioural resistor

This is exactly what I was searching for.

Thank you.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 13133
Re: LTspice and mechanical switch problem
« Reply #4 on: July 31, 2020, 05:23:29 pm »
The 'csw' method is most useful when you need more complex switching e.g. multi-pole multi-throw or want to simulate contact bounce.  If you create an extra model and swap the values of Ron and Roff (so Ron>>Roff) you can reverse the switch's action from the same control source, useful for double throw switching.  You also need to tweak the thresholds for break before make.

To model contact bounce use a PWL voltage source.   You can trigger it off another source,and if you put two in series (with only one of them set to -1 internal resistance), and short the pair, you can trigger them off a PULSE source, with one providing the closing bounce and the other the opening bounce.

Edit: I've found problems triggering PWL sources like that.   See the attached sim [here] for an approach that works.  The rise and fall times of the control source define the bounce period, and the bounce pattern comes from a repeating PWL, ideally triggered by the control source to minimize the sim computational overhead when the switch isn't pressed, but I'm *STILL* having problems with triggered sources.
« Last Edit: August 11, 2020, 03:47:25 pm by Ian.M »
 
The following users thanked this post: Chriss

Offline ChrissTopic starter

  • Frequent Contributor
  • **
  • Posts: 534
  • Country: 00
Re: LTspice and mechanical switch problem
« Reply #5 on: July 31, 2020, 06:54:21 pm »
I used for many years Multisim, almost maybe 20+ years. I can remember the EWB - Electronic Work Bench was its name in the '90.
And therefore I'm thinking in a way like that app was composed but every day I like more and more LT.
I need to switch my mind and thinking more complex than in Multisim.
But I like how LT is made.

Thank you for the suggestions, I really appreciate it.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 13133
Re: LTspice and mechanical switch problem - switch bounce sim
« Reply #6 on: August 12, 2020, 12:13:23 am »
Well I've cracked the challenge of simulating switch bounce efficiently using triggered PWL sources.

The tutorial LTspice: Piecewise Linear Functions for Voltage & Current Sources leaves out one key fact I found at StackExchange: TRIGGER does *NOT* work for PWL current sources so you *MUST* use a PWL voltage source.  Also the TRIGGER expression can *ONLY* be in terms of node voltages.  |O  :--

LTspice switch bounce sim attached.  The rise and fall times of the control source Vsc determine the bounce period, and the bounce pattern comes from a repeating PWL source Vbounce, triggered by the control source output V(sc) during its transitions to minimize the sim computational overhead.  Note the offset source Vofs is also triggered identically to avoid an offset current in the steady state.  You can edit the Vbounce PWL list manually to change the bounce pattern.  I've used a comma within each <time>,<value> pair and spaces between pairs to make that easier.  :popcorn:
« Last Edit: August 12, 2020, 12:19:20 am by Ian.M »
 
The following users thanked this post: Chriss, pe0hm


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf