Switch, Potentiometer simulator

Hi, is there a simple way to include on schematic Ngspice simulation to simulate a Push-Button (Example 5V when open, GND when pushed) and in the same idea a potentiometer ?
I try to add in parallel to the push-Button a Pulse-Generator but It seems not present on my Kicad 5.0.2 spice library, any idea ? Thanks.

How about a pulsed voltage source for simulating the button and a time dependent voltage or current source (depending on your needs) for the potentiometer? (Example sinus source but could also be a different mathematical function that changes the relevant value over time.)

Hi Rene, my problem and I am unable to add in my diagrams a source of impulse voltage. I added this information in the spice model editor:

But that does not seem enough.

When I edit my pspice lib I get only this elements :

No Pulsed Voltage Source on it. How to add it ? I need to create my self ?

Check the latest official library. We now have a few symbols specialized for simulation in it. Notably in your case are the source symbols. The library file is https://github.com/KiCad/kicad-symbols/blob/master/Simulation_SPICE.lib
But i suggest you download the full library repo or at least also download the dcm file as it includes the documentation for the symbols.

The symbols have been added with this pull request that could include valuable information for you if none of the symbols fit your need: https://github.com/KiCad/kicad-symbols/pull/1473

Thanks Rene for the links. I’ve updated my one library with the first Github link.
Now I get correct symbols.

It work for Push button simulation.

The extra symbol doesn’t seem necessary to me, since you were on the right track to begin with by adding the pulse parameters into the standard voltage source symbol. I believe your problem was that you made the “Delay time = 100m” which means it would take 100m before your pulse went to 0V. I don’t think your simulation was long enough to even wait for that to happen. Another quick comment is that I would recommend against setting your rise and fall times to zero in a PULSE waveform. Zero is invalid and ngspice will replace it with a default value. If you need fast pulse edges, define these parameters in your PULSE definition as either 1n or 1p.

Regardless, you’re half-way there. The way you have it now is a very poor representation of a low-side switching push-button. This it because you’re using an ideal voltage source as your actual switch. Traditionally, the way to do this in SPICE is to first create a separate control voltage source which simulates in time when the button is pressed/released. Then, you use this source to feed into a voltage controlled switch. The 2nd part is what you’re missing here. These two together will properly simulate the GND <-> High-Z behavior you want. This is especially critical when working with debounce circuitry, which appears to me you do have in your specific schematic. An N-channel FET can also be used for this scenario since you’re switching to GND, but it’s better to start with something more ideal and add non-idealities as you need them.

The first problem here is that KiCad has no SPICE SW symbol. The SW primitive needs 4 terminals (from the ngspice manual):
SXXXXXXX N+ N- NC+ NC- MODEL <ON><OFF>

I repurposed a dual pushbutton symbol for this experiment since it has four terminals and still looks like a switch visually. The SW primitive needs a model statement, which is shown in this screenshot. I kept it fairly ideal with on resistance (Ron) as 1 milliohm and off resistance (Roff) as 1 gigaohm. The Vt is the threshold voltage, and I put it at halfway (2.5V) of what the control voltage swings between (5V and 0V). If you look at the ngspice manual you can find other parameters like hysteresis voltage, but I want to keep it simple here.

The next thing I want to mention is that the PWL (piece-wise-linear) voltage source is much better suited for this type of behavior. It opens you up to doing many more things. For this example, I constructed my PWL such that it first does a clean pushbutton pulse and then a little later it does a bouncy pushbutton pulse on both edges. You can add many of these events within a single PWL statement as you like. I don’t want to really get into a full lesson on PWL, so I’ll leave it at that. Once thing I’ll mention though is LTspice has a really nice feature where instead of only absolute timepoints it lets you also use relative timepoints by putting a plus sign in front of the time number. So instead of putting PWL(0 0 100m 5 200m 0), you can put PWL(0 0 +100m 5 +100m 0) and it would do the same thing. The plus basically translates to “x seconds after the previous point, do this”. ngspice seems to be missing this feature, and that’s a shame because it is extremely useful especially for this type of stuff.


Lastly, I hope I don’t step on any toes here but the extra symbol library mentioned above seems like a waste of effort, at least in its current state. If this library can be updated with symbols for B sources, E sources, G sources, Transmission Lines, and the SW switch discussed above it would be infinitely more useful.

Thank you Ste for your demonstration, in fact yes, the idea is to solve and represent a problem of rebounds on a push button. I am not an experienced user of LTspice (or Ngspice) and I like your approach in the sense that we can parameter directly into the component the behaviour that we would like to assign to it. I will try a simulation with your recommendations.

BTW: Could you please provide the complete line for Field Value ?

With this :

image

I get :

Thank you again for your support.
Vincent.

Uhhh…sure. I’m not at the computer I used to make my earlier post, so this isn’t the exact one but it’s close enough. Basically, the bounces are just it alternating between 5V and 0V every 0.1m seconds:
PWL(0 0 5m 0 5.1m 5 15m 5 15.1m 0 30m 0 30.1m 5 30.2m 0 30.3m 5 30.4m 0 30.5m 5 40m 5 40.1m 0 40.2m 5 40.3m 0 40.4m 5 40.5m 0)

You can’t use the plus signs in KiCad/ngspice (yet). Otherwise making the above statement would be much easier. I also noticed that if you put this PWL statement in the field manually and then click the button to “Edit Spice Model” it will screw up the entire statement. So be careful about that.

I know I’m creeping into circuit design help, but if you want your output rise and fall times to be symmetric you typically add a schottky diode across the 2nd resistor to keep the RC time constants equal in both directions.

Thanks for the additional information indeed with a schottky the signal changes but I have the impression that the pulses are too fast for the R / C.

Without schottky :

With schottky :

I create this simulation circuit :

The out simulation :

Voltage control is Ok but the signal Out don’t go to 0.

A couple things I see wrong here. First, is your SW1 needs to be rotated 180° such that pin 1 is the output, pin 2 is GND, pin 3 is positive control voltage+, and pin 4 is negative control voltage. Look back on my schematic and you will see what I’m talking about. Second, I don’t see the .model statement for the switch. It might be outside of what you snipped in your image, but I thought I would mention it. Third is that the first parameter of your .tran command needs to be smaller. If you see my schematic, I put 10u since it needs to be smaller than the 100u of the bouncing. I typically leave the ‘S’ for seconds out because SPICE isn’t excepting them. I don’t know how capable KiCad/ngspice is at ignoring that.

Hmm interesting, I did not think a switch is an output and a GND, in fact, turning it works now. I added the .model to be in line with your recommendation.

I disconnect the schottky because result is different with or without :

With diode :

image

Zoom in “bounced” part :

image

I’ve changed values compare to first schematic with 2 x 10K and 10nF.

Cool. By the way, diode pin orders are backwards in KiCad. So if you’re having problems with it, you need to put an alternate node sequence of: 2,1

Yes, it’s better by setting alternate pin on diode :

image

Thanks a lot !

A post was split to a new topic: Help with Switch Simulation

Hi @mameco3798,

A lot has changed since 5.0.2 in 2019.
I started a new topic for you.