Electronics

Performing A Circuit Simulation In KiCad

KiCad Simulations Graphic
Written by John Woolsey

Last Updated: March 21, 2023
Originally Published: July 1, 2019

Skill Level: Intermediate

Table Of Contents

Introduction

This tutorial will teach you how to run a circuit simulation in KiCad. A basic understanding of electronics is expected along with knowing how to create schematic diagrams in KiCad.

The focus of this article will be on running circuit simulations within KiCad, not on how to create a schematic. There are already quite a few tutorials and resources on the internet to help you with creating schematics, including the KiCad Eeschema manual. I will instead concentrate on how to set up and run simulations in KiCad because that material is not as readily available.

The resources created for this tutorial are available on GitHub for your reference.

This tutorial is provided as a free service to our valued readers. Please help us continue this endeavor by considering a donation.

What Is Needed

  • Linux, macOS, Or Windows Based Computer
  • KiCad

Background Information

Sometimes a BJT transistor is used as a switch to drive higher loads (current) than is typically capable from a microcontroller’s GPIO output pins. For instance, the maximum current ratings of the GPIO pins for the Arduino Uno is 20 mA and the Raspberry Pi is 16 mA. A typical setup is shown below.

Schematic Diagram Of Transistor Switch Circuit
Schematic Diagram Of Transistor Switch Circuit

This involves operating the transistor in saturation mode, i.e., VE < VB > VC for NPN and VE > VB < VC for PNP transistors. When a typical BJT transistor is operating as a switch in this mode, certain assumptions are usually made to make the calculations easier.

  • VCE ≈ 0 V
  • β ≈ 10

This means we can effectively ignore the transistor in our load current calculations and the GPIO pin current (IB) is about a tenth of the load current (IC). As a simple example, let’s say the load is just a 150 Ω resistor with a 5 V supply. The assumptions imply the load current will be about 33.3 mA (5 / 150) and the GPIO pin current will be about 3.33 mA (33.3 / 10). I set out to verify these assumptions by performing a circuit simulation in KiCad.

My development system consists of KiCad 7 running on macOS. If you are using a different computer setup, the vast majority of this tutorial should still apply, however, some minor changes may be necessary.

Setting Up The Schematic

Create a schematic named TransistorSwitchSimulation in the KiCad Eeschema schematic layout editor to match the one shown above. Vcc and Vin are global input labels and Vb, Vc, and Ve are net labels.

KiCad uses ngspice internally to perform SPICE based circuit simulations. Therefore, in order to run a simulation, we need to add the appropriate SPICE related information to the schematic so that ngspice knows what to do.

To begin, we need to add power sources that SPICE understands. The typical power supplies used when creating schematics will not be understood by the SPICE engine. Place a new VDC voltage source component (located within the Simulation_SPICE component library) into the schematic. Wire a global output label named Vcc to the positive side and a GND power port to the negative side of the VDC component. Change the value of VDC to 5, meaning it will provide a 5 V DC supply.

Place an additional VDC voltage source in the same fashion, but this time use a label of Vin with a value of 0. This corresponds to a GPIO output pin being in an “off” state. The global labels provide the connections of the voltage sources to the rest of the circuit. My Vcc and Vin sources were referenced as V1 and V2 respectively.

SPICE uses models to describe the behavior of electronic components. KiCad implicitly assigns models to passive components, such as resistors and capacitors, however, models for semiconductor devices, such as diodes and transistors, need to be explicitly assigned.

We need to associate a SPICE model with the PN2222A transistor used in our schematic. SPICE models for particular components can usually be found from manufacturers or suppliers. I am using the following 2N2222 model for the transistor that I obtained from the LTspice standard BJT library. The PN2222A transistor is a less expensive version of the original 2N2222, but is considered equivalent except for pinouts and when operated under extreme conditions.

* Modified from LTspice BJT standard library (2N2222)
.model PN2222A NPN (IS=1E-14 VAF=100
+ BF=200 IKF=0.3 XTB=1.5 BR=3
+ CJC=8E-12 CJE=25E-12 TR=100E-9 TF=400E-12
+ ITF=1 VTF=2 XTF=3 RB=10 RC=.3 RE=.2)

Create a file named PN2222A.lib with the contents of the above model and place it inside the KiCad TransistorSwitchSimulation project directory.

Now that the model is created, we need to attach it to the transistor. Right-click on the transistor and select Properties… from the contextual menu. Click the Simulation Model… button to open the Simulation Model Editor and then open the Model tab. Select the SPICE model from file option, choose the PN2222A.lib file, and then select PN2222A as the Model. The entries should look like those in the window shown below.

Model Tab Of KiCad Simulation Model Editor Window For PN2222A Transistor
Model Tab Of KiCad Simulation Model Editor Window For PN2222A Transistor

KiCad uses different pin assignments for semiconductor components than is expected by the ngspice engine. For example, the standard SPICE pin assignments for a BJT transistor are pin 1 = collector, pin 2 = base, and pin 3 = emitter, whereas KiCad uses 1 = E, 2 = B, and 3 = C. This can cause great confusion for users wondering what the heck is going on. To fix this issue, click on the Pin Assignments tab and swap the collector and emitter pin assignments as shown below. Click OK when you’re done and also close the Symbol Properties window as well by clicking OK again.

Pin Assignments Tab Of KiCad Simulation Model Editor Window For PN2222A Transistor
Pin Assignments Tab Of KiCad Simulation Model Editor Window For PN2222A Transistor

Now we need to tell SPICE (ngspice) what we want to simulate. The best way to provide SPICE control statements is simply to add them via placing text directly in the schematic itself. There is another way we can do this that is much more user friendly, as I will show you later, but those statements are not retained between separate simulations and will need to be reentered each time. Placing them directly in the schematic not only allows you to retain the SPICE statements, but also gives you the benefit of seeing your statements up front when you open the schematic in the future.

Let’s run a transient analysis simulating the circuit from 0 to 1 ms using 1 us for each step. This corresponds to the SPICE control statement shown below.

.tran 1u 1m

Add the above text somewhere in your schematic. If you are not familiar with placing text in a schematic, this can be accomplished by selecting Place > Add Text from the main menu, adding the text in the popup window, clicking the OK button, and then clicking where you want the text to be placed.

Your completed schematic should now look like that shown below.

Schematic Diagram Of Transistor Switch Circuit With SPICE Elements
Schematic Diagram Of Transistor Switch Circuit With SPICE Elements

Save your schematic when you are done.

Running The Simulation

Select Inspect > Simulator… from the main menu and you will see the Spice Simulator window appear.

Let’s take a look at the simulation settings. Click the Sim Command icon (gear) within the toolbar to see the Simulation Command window. The Transient tab should already be selected and populated with the control statement data it obtained from the text within the schematic. Here you can see the correlation of 1u to Time step and 1m to Final time. This is the place I mentioned earlier where you can enter your SPICE control statements in a more user friendly way, but your entries will not be saved between simulations. The other tabs provide for different simulation control statements. If you click the Custom tab, you will see the exact control statement retrieved from the schematic. We also need to make sure the Compatibility mode is set to PSpice and LTSpice if it is not already. Click the OK button when done.

The big moment arrives. Click the blue arrow button (Run/Stop Simulation) in the toolbar to run the simulation. A blank Plot1 – TRAN waveform viewer will appear at the top and the simulation output will be shown at the bottom with the following contents.

Note: Compatibility modes selected: ps lt a
Circuit: KiCad schematic
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
Initial Transient Solution
--------------------------
Node                                   Voltage
----                                   -------
vin                                          0
probe_int_/vb_r1                   5.00333e-09
/vb                                5.00333e-09
vcc                                          5
probe_int_/vc_r2                             5
/vc                                          5
probe_int_/vc_q1_1                           5
probe_int_/vb_q1_2                 5.00333e-09
probe_int_gnd_q1_3                           0
q1:e#branch                          -1.05e-14
q1:b#branch                       -5.00333e-12
q1:c#branch                        1.00129e-11
v2#branch                          5.00333e-12
v1#branch                         -1.00129e-11
r2#branch                          1.00129e-11
r1#branch                         -5.00333e-12
 Reference value :  0.00000e+00

No. of Data Rows : 1008

We can see from the output that Vcc and Vin have the expected values of 5 and 0 respectively. We can also see that the collector voltage (Vc) is the same as Vcc, meaning no, or negligible, current is flowing through the R2 resistor. This is expected since the transistor is in its “off” (cutoff mode) state.

Now that a simulation has run, the SPICE netlist used for the simulation can be viewed by selecting Simulation > Show SPICE Netlist… from the main menu.

Now let’s turn the transistor on. Close the Spice Simulator window and click the Save button. Change the value of the Vin voltage source to 5, representing the GPIO output pin being in an “on” state. Save your schematic. Run a new simulation. The output this time should look like the following.

Note: Compatibility modes selected: ps lt a
Circuit: KiCad schematic
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
Initial Transient Solution
--------------------------
Node                                   Voltage
----                                   -------
vin                                          5
probe_int_/vb_r1                      0.807997
/vb                                   0.807997
vcc                                          5
probe_int_/vc_r2                     0.0571069
/vc                                  0.0571069
probe_int_/vc_q1_1                   0.0571069
probe_int_/vb_q1_2                    0.807997
probe_int_gnd_q1_3                           0
q1:e#branch                         -0.0371446
q1:b#branch                           0.004192
q1:c#branch                          0.0329526
v2#branch                            -0.004192
v1#branch                           -0.0329526
r2#branch                            0.0329526
r1#branch                             0.004192
 Reference value :  0.00000e+00

No. of Data Rows : 1008

We can see from this output with Vin equal to 5 V that Vc is now 57.1 mV. This means the transistor is now “on” and current is flowing through the 150 Ω (R2) resistor. The v1#branch and v2#branch entries represent the steady state current flowing into the voltage sources. That is why they are negative values. Similarly, the r1#branch and r2#branch represent the current flowing through resistor R1 (into the base of the transistor) and through resistor R2 (into the collector of the transistor) respectively.

If you see a negative current through a resistor, you can change either the orientation of the resistor by 180 degrees in the schematic or swap the pins as we did earlier for the transistor.

The values shown represent

  • VC = 57.1 mV
  • VB = 0.808 V
  • IC = 33.0 mA
  • IB = 4.19 mA

We can now circle back to verify our assumptions made at the beginning of the article that VCE ≈ 0, β ≈ 10, and that we can effectively ignore the transistor in our load calculations.

First, we can verify the BJT transistor is indeed operating in saturation mode: VE < VB > VC equals 0 < 0.808 > 0.0571.

Calculating the VCE and β values, we get VCE = VC – VE = 57.1 – 0 = 57.1 mV and β = IC / IB = 33.0 / 4.19 = 7.86.

For the load, the assumed IC current would be VCC / R2 = 5 / 150 = 33.3 mA and we got a value of 33.0 mA in our simulation.

And finally, the assumed IB transistor base current is IC / 10 = 33.3 / 10 = 3.33 mA and we got a value of 4.19 mA.

Overall, our simulation results are pretty close to our assumptions. Very good!

As an aside, I physically built the circuit and obtained the following measurements that correlate very well with the simulation.

  • VCC = 4.92 V
  • R1 = 0.979 KΩ
  • R2 = 149 Ω
  • VCE = 52.9 mV
  • VBE = 0.767 V
  • IC = 33.5 mA
  • IB = 4.37 mA
  • β = 7.67

Viewing The Waveforms

In the previous section, we determined the circuit values from the SPICE simulation output text. A simpler way to see the values is to use the KiCad waveform viewer. Since we ran a transient analysis with .tran 1u 1m, the time frame for the waveforms will cover from 0 seconds (when the circuit turned on) up to 1 ms.

To view a signal, such as a voltage or current, click the Add Signals icon in the toolbar and select a signal you want to view in the popup window. Let’s start with choosing the current flowing through resistor R1. Click on I(R1) and then the OK button. The waveform will be shown in the waveform viewer on the left and the I(R1) signal will be listed in the Signals list on the right. The current will be about 4.19 mA.

To remove a signal from the viewer, double click the signal name in the Signals list.

Let’s next look at the transistor’s collector voltage by adding the V(/Vc) signal like we did earlier for I(R1). The value will be around 57.1 mV. Play around and look at some of the other signals as well. Close the Spice Simulator window when you are done.

Although we are seeing the expected values in the waveform viewer, the waveforms themselves are not very interesting. Let’s change it up a little by adding a 100 mV ripple to the input voltage.

Add a new VSIN voltage source (from the Simulation_SPICE library) to the schematic and edit the simulation properties to be those shown below.

Model Tab Of KiCad Simulation Model Editor Window For VSIN Voltage Source
Model Tab Of KiCad Simulation Model Editor Window For VSIN Voltage Source

This means we are applying a sine wave with a DC offset value of 5 V, an amplitude of 100 mV, and a frequency of 10 KHz.

Then, attach a global output label and a ground to the new VSIN voltage source similar to what we did for the VDC voltage sources. However, this time name the label as Vin2.

Finally, rename the Vin input of the transistor circuit to Vin2 so that we properly input the new sine wave voltage source. The new circuit should now look like the following.

Schematic Diagram Of Transistor Switch Circuit With Additional VSIN SPICE Element
Schematic Diagram Of Transistor Switch Circuit With Additional VSIN SPICE Element

Save the schematic and run the simulation again and the output text should be identical to the previous simulation, except for the inclusion of the new VSIN voltage source, since the ripple voltage we applied is about the 5 V DC offset.

View the V(Vin2) signal this time and we should see a sinusoidal waveform that oscillates between 4.9 and 5.1 V.

Remove the V(Vin2) waveform and view the I(R1) signal. You will see the current oscillating between 4.09 and 4.29 mA.

To determine a value along the sine wave, right-click on I(R1) in the Signals list and select Show Cursor from the contextual menu. A dashed axis will appear in the waveform viewer with I(R1) also showing up in the Cursors list. Click and hold around the origin of the axis and you can “ride” the waveform watching the Time and Voltage/Current values in the Cursors list change. Release the click when you are at an interesting point along the curve. To remove a cursor, right-click on the signal in the Signals list, and this time select Hide Cursor.

KiCad Spice Simulator Window With I(R1) Waveform
KiCad Spice Simulator Window With I(R1) Waveform

Again, play around and view some of the other signals.

Summary

In this tutorial, we learned how to run a SPICE based circuit simulation in KiCad. Specifically, we learned how to

  • set up a schematic for simulation,
  • create and apply models,
  • add SPICE control statements,
  • run a circuit simulation, and
  • view circuit waveforms and determine certain values along the curves.

Running a circuit simulation is a good way to verify your design will operate as expected and enables you to discover and correct possible problems before implementing your circuit.

The final schematic used for this tutorial is available on GitHub.

Thank you for joining me on this journey and I hope you enjoyed the experience. Please feel free to share your thoughts in the comments section below.

About the author

John Woolsey

John is an electrical engineer who loves science, math, and technology and teaching it to others even more.
 
He knew he wanted to work with electronics from an early age, building his first robot when he was in 8th grade. His first computer was a Timex/Sinclair 2068 followed by the Tandy 1000 TL (aka really old stuff).
 
He put himself through college (The University of Texas at Austin) by working at Motorola where he worked for many years afterward in the Semiconductor Products Sector in Research and Development.
 
John started developing mobile app software in 2010 for himself and for other companies. He has also taught programming to kids for summer school and enjoyed years of judging kids science projects at the Austin Energy Regional Science Festival.
 
Electronics, software, and teaching all culminate in his new venture to learn, make, and teach others via the Woolsey Workshop website.

53 Comments

  • Hi John
    Thank you for your sharing, it’s earier to understand than KICAD’s manual, but I still have some troubles on simulation with a quadruple operational amplifiers, like LM324, it has 5 banks in KiCad, then I don’t know how to deal with its spice model, help me please, thank you.

  • Thank you for the clear and helpful article. It helped me a lot.
    I am so new to electronics. Is it something you recommend to use for simulation? For me it has at least the benefit of getting more used to KiCad environment and edit tools, but is the simulation reliable enough?

    • Simulations appear to work well in KiCad, but if you are just looking for general simulation, give LTspice a try. Check out my Getting Started With LTspice For Mac if you are using a Mac. The general LTspice information of the article might also help you if you are using Windows, but please keep in mind the Mac version works much differently than the Windows version. There should be quite a few Windows based tutorials out on the Web, but not much for the Mac, which is why I wrote that article.

  • Thank you so much for clarifying the Simulator process. I’m a teacher for the IBEW/NECA JATC inside apprenticeship, and this tutorial has unlocked a tool that I can use in the classroom to demonstrate electrical concepts to my students. I really appreciate the time taken to create this tutorial.

  • Hi John!

    Many thanks for the great work!!!!
    I am mechanical engineer and “play” with electronics only occasionally in my spare time. I’ve been using LTSpice for the last couple of years, but as I have switched to Linux a few months ago, I am interested in learning KiCad. Thus, I am very happy about your clearly structured and well documented tutorial, that is a good starting point for me 🙂

  • Thank you John! This tutorial provided me with information to get simulations finally working. The pin numbering issue with transistors is not easy to figure out. I have just upgraded from v4 to v5 and I noticed that in my circuit the order was different again (2 1 3). Great work!

  • Thank you John, excellent article. I was searching the web and I found it. I will follow that. It is valuable information.

  • John,

    Hey there, I am working through your tutorial and am hung up on the issue that there is no Vin symbol that I can find in Kicad. I’ve drawn a few other drawings and understand how to connect power, but for the life of me that symbol is not in Kicad 5.0. Is this something that was removed or reimagined in between version 4 and 5? The power flags are also different that what you show in your example drawing, but that is not a big deal. Thanks for the help.

    • Vin is not an actual component symbol, but rather a global label, just like Vcc. It is attached to the VSOURCE voltage component to supply power to the transistor circuit.

  • Thanks for the response…I do understand that Vin is not a component symbol and that its arbatrary. Its that I do not see that tag anywhere where I can select that tag. I’m just trying to follow your example as closely as possible. I’ll set up the sim as close as I can and see what I get. Thanks for the help.

    Cheers

    Phil

    • Jason, disregard my last comment…It was a global label I was looking for. I got it. I’d never used global, or needed to use, global labels for anything so they were not on my radar. I was able to create what I needed so my schematic matches yours. Sorry about the confusion, and thanks again for the help. Great site and great tutorial.

  • Thanks for this! other spice models have their pins annotated but I couldn’t tell if kicad just “knew” the pins for bjt’s or not.

  • Thank you very much for your time and work dedicated to this tutorial.
    I’m trying to replicate it, but I’m facing some issues.
    Spice Simulator gave me this warning:
    “Compatibility modes selected: ps lt
    Circuit: KiCad schematic
    Error on line 6 :
    q1 /vc /vb 0 2n2222
    could not find a valid modelname
    Error: circuit not parsed.”

    It turned out to work well when I changed its name to PN2222 in the .lib code and in KiCad’s Spice Model dialogue. Now I’m curious: Is there any restriction to modelname? I’m using KiCad 6.0 on Windows 10.

    • It appears that the model name was not selected in the Spice Model Editor window. Try to click on the down arrow for the Model: field and select 2N2222 from the drop-down options list (there should only be the one). Hopefully, that will work.

    • How can possible to KiCad 6.0 in simulation model .
      then not to me kicad 6.0 in option to spice model in simulation model editor.

  • Your simulation exercise seemed a good start to KiCad, however I became stuck.
    The first problem was fixed by adding PWR_FLAG to VCC and GND.
    I could not find the Spice Model Editor to look at a transistor (before I attempted modifying the copy you suggested) however I found a BC547 already in the library which was claimed to be very close to the 2N2222 whilst it pins seemed to be `normal’. So I used that but was still wondering if I should be using the transition you mentioned to make KiCad transistor pinouts match Spice expectations.
    On the simulation page, I was being asked to enter some `values’. All I could think of was to set V1 to start and finish at 0V with 0V steps and the same for V2 (at 5V). However `nothing’ seemed to happen.

    So, I wonder what I am missing. I hope you can help some one who started a long time ago, loading computer programs punched out on paper tape with a teletype.

    Damien

    • Well, I’m not surprised you got a little stuck. It took me a little while to figure out too. This tutorial was written with KiCad 5 and things seem to work a little differently with KiCad 6.

      Aside from a few of the menu names and locations changing, e.g.

      • Changed From Properties > Edit Properties… to Properties…
      • Changed From Edit Spice Model… to Spice Model…
      • Changed From Place > Graphic Text to Place > Add Text
      • Changed From Tools > Simulator to Inspect > Simulator…

      we also need to change the way we import the transistor model.

      • Change the name of the model from 2N2222 to PN2222A so that it matches the transistor symbol name in the schematic. This needs to be changed both within the library file and the Spice Model Editor Model field.
      • Update the Compatibility mode within the Simulation settings window to PSpice and LTSpice.

      After making these changes, I found everything else seemed to work as described in the tutorial. If you have any more questions, please let me know.

    • This question does not appear to be related to the tutorial, but perhaps I can point you in the right direction.

      From looking at the datasheet, it appears that the third terminal of the photodiode is just connected to the case, so it can be connected to ground.

      As for some possible circuits to use the photodiode, you may find the following tutorials helpful.
      Intro to IR Circuits and Infrared Sensor

  • Hi and thanks for this tutorial. I need to make an AC sweep analysis to come out with the frequency resopnse of a passive RLC network. I am using the VSIN voltage source to excite my circuit. I would like the frequency of VSIN to swope from fmin to fmax. I do not find any way to do it. How do you indicate in the AC sweeb tab in the simulator that it is the frequency of the voltage source VSIN what you want to change? Thank you.

    • I’m sorry, but I don’t know the answer to your question. However, I believe you can perform a standard AC sweep analysis by adding your DC and AC values in the “DC/AC Analysis” section of the Spice Model Editor for your VSOURCE (sin) device and then replacing the .tran 1u 1m spice command with something like .ac oct 10 10k.

  • This guide does not seem to work for Kicad 7, I can simulate passives perfectly fine but need to simulate transistors and custom devices. When following this guide which uses transistor in Kicad 7 simulation fails with.

    “Error: .include filename missing
    Error: ngspice.dll cannot recover and awaits to be detached
    Note: can’t find the initialization file spinit.
    ******
    ** ngspice-39 shared library
    ** Creation Date: Feb 9 2023 06:17:51
    ******
    Note: No compatibility mode selected!
    Circuit: *
    Warning: No job (tran, ac, op etc.) defined:
    run simulation not started”

    The compatibility mode is selected and the .trans statement is included and everything perfectly matches this guide. After spending hours trying to get it to work and posting here. This is on a new clean Kicad 7 installation.

    • Yeah, I was a little worried about that. KiCad 7 was just recently released and I had not yet verified that the tutorial still works with the new version. Thank you for letting me know. I will put it on my list to research and update.

    • I see that KiCad 7.0.1 was just released that fixed many SPICE simulator issues. Hopefully, this new version will resolve some of the simulation errors you reported.

    • I had to make a few changes to the schematic to implement the new voltage sources and process, but I was able to get it all working in KiCad 7.0.1. I will try to publish a tutorial update next week.

  • Your SPICE simulation is terrible, but you should actually learn Multisim, because their simulation allows the LCD screen to display numbers on the circuit diagram, and the LED light to light up. I can intuitively operate the switch and see the effect. I don’t want to see any tables or command lines, which is terrible. Although I appreciate your open source policy, it is too difficult to use.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.