Skill Level: Intermediate

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.

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

This involves operating the transistor in saturation mode, i.e., V E < V B > V C for NPN and V E > V B < V C 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.

V CE ≈ 0 V

≈ 0 V β ≈ 10

This means we can effectively ignore the transistor in our load current calculations and the GPIO pin current (I B ) is about a tenth of the load current (I C ). 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 (5.1.2) running on macOS Mojave. 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 labels and Vb and Vc are net labels.

KiCad uses ngspice internally to perform SPICE based circuit simulation. 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 VSOURCE voltage source component (located within the pspice library) into the schematic. Wire a global label named Vcc to the positive side and a GND power port to the negative side of the VSOURCE component. Change the value of VSOURCE to 5 , meaning it will provide a 5 V DC supply.

Place an additional VSOURCE 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.

Annotate the schematic so that the new voltage sources get referenced. My Vin and Vcc 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 manufactures 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 standard library .model 2N2222 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 2N2222.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 > Edit Properties… from the contextual menu. Click the Edit Spice Model… button to open the Spice Model Editor and then open the Model tab. Load the model file we created by clicking the Select file… button and then choosing the 2N2222.LIB file. The Library field will be populated with the library file name. Select 2N2222 for the Model field and BJT for the Type field. The model data will be shown.

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, enable the Alternate node sequence option with a value of “ 3 2 1 “, effectively swapping the collector and emitter pin assignments. The entries within the Spice Model Editor window should look like those shown below. Click OK when you’re done and also close the Symbol Properties window as well by clicking OK again.

KiCad Spice Model Editor Window

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 load 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 > Graphic Text from the main menu, clicking where you want the text to be placed, adding the text in the popup window, and then clicking OK when done.

Your completed schematic should now look like that shown below.

Schematic Diagram Of Transistor Switch Circuit With SPICE Elements

Save your schematic when you are done.

Running The Simulation

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

Let’s take a look at the simulation settings. Click the Settings icon (gear) within the toolbar to see the Simulation Settings 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 don’t want to change anything here, so just click Cancel when done.

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

Circuit: KiCad schematic Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 Initial Transient Solution -------------------------- Node Voltage ---- ------- vin 0 /vc 5 /vb 5.00333e-09 vcc 5 v2#branch -1.00129e-11 v1#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. 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.

Circuit: KiCad schematic Doing analysis at TEMP = 27.000000 and TNOM = 27.000000 Initial Transient Solution -------------------------- Node Voltage ---- ------- vin 5 /vc 0.0571057 /vb 0.807976 vcc 5 v2#branch -0.0329526 v1#branch -0.00419202 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. The v1#branch is the Vin current flowing through resistor R1 into the base of the transistor. Likewise, the v2#branch is the Vcc current flowing through resistor R2 into the collector of the transistor. The values shown represent

V C = 57.1 mV

= 57.1 mV V B = 0.808 V

= 0.808 V I C = 33.0 mA

= 33.0 mA I B = 4.19 mA

We can now circle back to verify our assumptions made at the beginning of the article that V CE ≈ 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: V E < V B > V C equals 0 < 0.808 > 0.0571.

Calculating the V CE and β values, we get V CE = V C – V E = 57.1 – 0 = 57.1 mV and β = I C / I B = 33.0 / 4.19 = 7.86.

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

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.

V CC = 4.92 V

= 4.92 V R1 = 0.979 KΩ

R2 = 149 Ω

V CE = 52.9 mV

= 52.9 mV V BE = 0.767 V

= 0.767 V I C = 33.5 mA

= 33.5 mA I B = 4.37 mA

= 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.

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 use the Alternate node sequence option like we did earlier for the transistor.

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.

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. Close the Spice Simulator window. Change the value of the Vin voltage source from 5 to sin(5 100m 10k) . This means we are applying a sine wave voltage with a DC offset of 5 V, an amplitude of 100 mV, and a frequency of 10 KHz.

You can also edit the SPICE model for the voltage source using the Spice Model Editor like we did earlier for the transistor. This time, however, select the Source tab instead of the Model tab. Under the Transient analysis section, select the Sinusoidal tab and you will see the sine wave source data we entered previously as a value. The other tabs provide other SPICE based voltage source types available. Model data entered here will override the Value field, but will not be visible on the schematic.

Run the simulation again and the output text should be identical to the previous simulation since the ripple voltage we applied is about the 5 V DC offset.

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

To determine a value along the sine wave, right-click on V(Vin) in the Signals list and select Show Cursor from the contextual menu. A dashed axis will appear in the waveform viewer with V(Vin) 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.

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

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 in this journey and I hope you enjoyed the experience. Please feel free to share your thoughts in the comments section below.