LTspice and mechanical switch problem - Page 1
Forum section: electronics / beginners

author: Chriss
author_rank: Frequent Contributor
timestamp: July 29, 2020, 01:35:52 pm
content: Hi!I'm migrating from MultiSim to LT and I try to make a simple circuit this kind:1 LED1 RES1 Vsource1 Mechanical switch.I can't find any solution from the part list for a mechanical button/switch like in MultiSim soif 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.

author: mikerj
author_rank: Super Contributor
timestamp: July 29, 2020, 03:24:23 pm
content: 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.

author: Ian.M
author_rank: Super Contributor
timestamp: July 29, 2020, 04:02:23 pm
content: 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.ascYou can also use abehavioural resistorin terms of time, which may be easier for very simple switching events. Edit the value to R=<expression> e.g:Code: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.

author: Chriss
author_rank: Frequent Contributor
timestamp: July 31, 2020, 03:12:42 pm
content: QuoteThis is exactly what I was searching for.Thank you.

author: Ian.M
author_rank: Super Contributor
timestamp: July 31, 2020, 05:23:29 pm
content: 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 simfor 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.

author: Chriss
author_rank: Frequent Contributor
timestamp: July 31, 2020, 06:54:21 pm
content: 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.

author: Ian.M
author_rank: Super Contributor
timestamp: August 12, 2020, 12:13:23 am
content: Well I've cracked the challenge of simulating switch bounce efficiently using triggered PWL sources.The tutorialLTspice: Piecewise Linear Functions for Voltage & Current Sourcesleaves out one key fact I found atStackExchange:TRIGGER does *NOT* work for PWL current sourcesso you *MUST* use a PWL voltage source. Also the TRIGGER expression can *ONLY* be in terms of node voltages.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.