12

I'm trying to reproduce in LTspice the switching response of a push button with bounce effect as shown on the second page of the SN015 Support Note by Würth Elektronik. Here is an excerpt:

Output without debounce circuit during high to low transition

What I have tried to do is use two voltage-controlled switches to toggle between transients and the low level, but there must be a better way to do it.

Push button with debounce circuit in LTspice

I've found the article below which is close to what I'd like to achieve, but it seems like the transients are regular and the logical level is low while, in the case of the push button, the bounce effect happens during a high-to-low transition.

LTspice: Using Time-Dependent Exponential Sources to Model Transients

Could you please advise?

Davide Andrea
  • 16,164
  • 4
  • 33
  • 62
noeld
  • 123
  • 7
  • 2
    You can see my post here on how to use a PWL source to do this. If you need randomness, then it gets more complicated. https://forum.kicad.info/t/switch-potentiometer-simulator/15544/9 – Ste Kulov Jan 06 '23 at 20:37
  • @Ste I've followed instructions about PWL at [LTspice: Piecewise Linear Functions for Voltage & Current Sources](https://www.analog.com/en/technical-articles/ltspice-piecewise-linear-functions-for-voltage-current-sources.html) but your post was definitely what I needed. Thanks. – noeld Jan 06 '23 at 22:30
  • 3
    Create whatever signal you need, put it into a wav file, and play it back in LTSpIce as a source – Scott Seidman Jan 07 '23 at 00:19
  • @ScottSeidman that could be an interesting feature not only in this case but in others too. But how would I sample the signal and convert it to a wave file? A microcontroller like an Arduino could probably do the sampling with an ADC input but how about the conversion? – noeld Jan 07 '23 at 19:59
  • 1
    I would use a program like octave (sort of a free, largely command compatible version of Matlab) to do both. If you can't construct the signal from the command line, look up "datathief" – Scott Seidman Jan 07 '23 at 23:22
  • 2
    I did something similar to @ScottSeidman 's suggestion in the linked answer below. It seems like it can be quite a powerful method to put in your toolbox. https://electronics.stackexchange.com/a/620082/ – Ste Kulov Jan 08 '23 at 04:19
  • 1
    @SteKulov. That looks even easier! – Scott Seidman Jan 08 '23 at 15:13
  • @Ste I gave your solution a quick try and that looks very promising and easy. This tool will definitely have a good place in my toolbox. – noeld Jan 08 '23 at 20:23
  • @Scott I will search for "Octave" and "datathief"; I'm not familiar with MatLab either. I also thought about a solution which would involve an Arduino and data transfer over the serial port to a PC. Then, on the PC side, I could read the incoming data and write them to a WAV file. But it seems like the ADC of the Arduino is limited to about 10kHz while the signal above would require 1 or 2MSPS. – noeld Jan 08 '23 at 20:30
  • 1
    By the way, you can also import the CSV file from an oscilloscope data capture as a PWL file in LTspice. – Ste Kulov Jan 09 '23 at 03:48
  • @Ste I did not even know that my DSO could do that: it seems like the user manual is not in sync with the latest firmware. Anyway, this is a very handy and welcome feature. I have to edit the CSV file and remove the headers to get it to work in LTspice. My only problem now is that no bounce effects appear on the DSO screen (as if my push buttons were just perfect!) so I cannot analyse the signal. – noeld Jan 09 '23 at 22:50

3 Answers3

20

You can use the rand() function to generate a noise-like signal, that combined with time and a bit of math and logic operators can produce something like switch bounce when used to control a behavioral voltage source.

This example circuit generates some random pulses and then using ORs and ANDs and NOTs combines them with logic 1 signals based on the simulation time to give a 20 uS delay at the start, a 1 V signal for another 280 uS, and then the random pulses for another 200 uS.

enter image description here

Update: Here's a version that adds bounce on switch close and makes it easy to set the timings by right clicking the top .param statement and editing the values in the list, as well as having the BV control a switch component. enter image description here enter image description here

GodJihyo
  • 17,628
  • 1
  • 16
  • 45
  • Thanks. That is the perfect fix for my initial schematic. I did use rand() but missed the logical operations with time. I've just added a multiplication by 5 of the result to reach 5V. Now I realize that the signal is not always alternating between 0 and 5V so sampling the signal would probably be more accurate for the simulation. – noeld Jan 07 '23 at 20:25
  • 1
    @noeld Just added an update. – GodJihyo Jan 07 '23 at 20:27
  • Thanks for your examples that were very helpful to me and that I reproduced to play with. They do reply to my question. I have accepted the answer about using the PWL voltage source because in the end this is the solution that allows the more accurate representation of the signal that I wanted to simulate. – noeld Jan 07 '23 at 20:58
  • 1
    @noeld Yes of course, the PWL will let you make pretty much any waveform you want, so for accuracy you can't beat it. Now you have several more tools in your toolbox, and that's always a good thing :) – GodJihyo Jan 07 '23 at 21:17
  • every proposed solution here is great and each will be useful for different purposes whether I have a graph or an actual signal sample or a random signal. My toolbox is definitely packed with many more powerful tools and... knowledge! :) – noeld Jan 08 '23 at 20:40
15

I'd suggest using a PWL voltage source. Convert that graph into a .txt file of time vs. voltage level, as accurate as needed. Then set the voltage source to use that PWL file as input. If you need an actual switch contact, feed this into a voltage-controlled switch.

rdtsc
  • 15,913
  • 4
  • 30
  • 67
  • 1
    Thank you, that worked. I've used the spice directive **PWL repeat forever (file=switch-response.txt) endrepeat** to make the transition and bounce effect recurrent. – noeld Jan 06 '23 at 22:10
  • 1
    Also note in addition to repeat, the command can also [trigger on some condition, and scale](https://www.analog.com/en/technical-articles/ltspice-piecewise-linear-functions-for-voltage-current-sources.html) (thanks Gabino.) – rdtsc Jan 09 '23 at 14:13
4

The bounce effect is not random and its evolution can be represented with a decaying oscillation, both in amplitude and frequency. This implies no other mechanical perturbations; if there are, you'll need extra modelling (which I heavily doubt is present in many, if any models). I'm nowhere near my tools but, you can use your favourite RLC, driven by a VCCS (for example), and whose voltage then feeds a VCSW (set up with a negative hysteresis). The frequency will be a little bit more difficult to achieve but, it is possible by making the L or the C behavioural, controlled by a ramp (the integrated, or lowpass filtered pulse, for example). I doubt I'll be able to post a schematic anytime soon but, it should be fairly straightforward to build the schematic. I have and worked quite well.

a concerned citizen
  • 21,167
  • 1
  • 20
  • 40