Simulating a KiCad Circuit
by Makerss in Circuits > Electronics
26109 Views, 8 Favorites, 0 Comments
Simulating a KiCad Circuit
Drawing and designing circuits is an old process, as old as the first electronic components. It was easy then. There was a limited number of components and therefore a limited number of configurations, in other words: circuits were simpler. Now, in the so-called information age, there is a myriad—A LOT— of different components, and each electronic component has over a dozen models and each model is manufactured by a handful of companies. Needless to say, each model and each company-specific component differ from each other. They may have their biases, errors with different tolerances, different max, and min operating conditions and of course may slightly change how the circuit responds and works. To top it all off, circuits nowadays are highly complex; consisting of up to dozens of components that interact together to do different tasks based on the input.
As you have correctly guessed, it would be a nightmare to try to analyze these circuits by calculation or by hand. Furthermore, some tolerances and nuances would be lost or altered as they are product specific. This is where simulation comes in. Harnessing the power of modern technology and with cutting-edge speeds, a circuit analysis that would have taken teams of people working for hours is now as simple as setting the
Supplies
-Kicad version 5.0 or later
-Internet connection to download libraries
How Does the Magic Happen?
Let’s preface this by saying that KiCad does not handle simulations. KiCad is merely a UI (User-interface). A comparable analogy would be that KiCad is just a middleman between you and the simulation program, which could be one of multiple software called “SPICE”.
SPICE is short for “Simulation Program with Integrated Circuit Emphasis”. In KiCad’s case, KiCad 5.0 and later comes pre-packaged with a SPICE program called ngspice. Ngspice has its quirks, hiccups and limitations but will be the software we will focus on. Ngspice uses “Components” to model circuit behaviour. This means that aside from drawing the circuit schematics we also must annotate and “assign” models to individual components. To solve the problem of multiple models of the same components, ngspice decided to let each company make “spice models” that replicate the properties and nuances of their real-life counterparts, and then package these models as downloadable libraries, so that drawing a circuit would be as simple as downloading the required libraries and assigning model to our components. But that’s all talk, let’s get our hand dirty and see how this actually works.
Choosing a Circuit and Modelling Passive Components.
We chose a simple circuit that allows us to demonstrate how we can provide our own SPICE values to components and how we can use components that vendors listed
First off, as we can see from the figure; there are 8 components to this circuit.
• 2 resistor
• 1 9v battery
• 1 LDR
• 1 BC 547 npn transistor
• 1 LED
• 1 rheostat •
1 ground
Modelling Resistors of all types
Ngspice “assigns models” to resistances, in other words: it recognizes them. So we don’t need to modify them, or tinker with libraries to make them. We also notice that there is a rheostat and an LDR. In ngspice, they can both be modelled as constant resistors that we will modify their values as we need. In other words, if we need to “increase the light” or increase the rheostat’s load, we will have to stop the simulation, modify the load, and then rerun it.
Modelling Voltage Sources& Grounds
Ngspice doesn’t recognize “standard” voltage sources; those used by KiCad. It provides a library specifically for the voltage sources and grounds
To access the library, first, we have to pick the “Choose symbol” tab, and search for “spice”
*As seen in (figure 1), we have the “pspice” library and the “simulation_spice” one. For voltage sources, we want to scroll down to the simulation_spice library and choose a dc voltage source
Afterward, we have to set its values for the simulator to understand, in this circuit we want a 9v dc source. We click “E” on the voltage source and the following menu opens, shown in (figure 2). We choose a reference name for the voltage source, VoltageMain for example, and then we click “Edit Spice Model.” As shown above
Then we choose a value of dc 9v, and that is about it. As shown in (figure 3)
The Ground
For the ground, we search for “spice” again and the first result is the 0V reference potential as shown in.(figure 4). Unlike normal schematics, spice software needs the ground as it calculates its voltages based on the 0v reference.
Modelling the Transistor
As we can see from the circuit picture, the transistor used is a very specific model, the “BC547”. As a general case, almost all manufactured components will be found on their respective manufacturer’s website. Under their tool or support tab, there will be “simulation models” featuring the model number and a relative spice model. In our case I searched for “bc547” online and found that it was manufactured by a company called “On semiconductors”. I searched for their website “https://www.onsemi.com/” and found the model by doing as follows:
- I opened their “Tools and support” tab, underneath I, found a design resources tab. (figure 1)
- Underneath design resources they asked for the type of document, I chose “Simulation Models” (figure 2)
- I searched for the part by name: “BC547”. We want the library, so we choose “BC547 Lib Model” and downloaded it. (figure 3)
- After downloading it, I put the lib file in my project directory. Now my project directory is shown in the original KiCad window I opened, as seen in (figure 4). I clicked my way to that directory, pasted the library file as shown and returned to find it shown alongside my project’s files
- After all that has been said and done, let’s draw the transistor symbol. I clicked using the “place symbol” menu, and just searched for the name. You find that almost all components exist in the symbol menu as in (figure 5).
- Now, what remains is assigning the model to the symbol. We click “E” as always on the symbol, and click on “Edit spice model”.
As we can see, the only tabs available are model, passive, and source. Since transistors are neither source nor passive, we choose the model and choose to plug-in a library to fill. The menu first opens to the project’s directory, which we lucky have already put the library into it. We click on the lib file.
- Great!! Now ngspice has identified the transistor as “BC547” and is almost ready to operate. There is one tiny detail to be sorted first. We have to enable alternate node sequence and type “3 2 1”. The reason we have to do this step is that ngspice names the 3 transistor terminals in a manner opposite to how KiCad shows them. So, it may have 3 assigned to the collector while KiCad shows 3 as the emitter. To avoid confusion, we reconfigure Spice’s naming order, as shown in (figure 7)
Anddddd that is it! This process is almost identical for all
vendor-supplies models. Once you wrap your head around this tutorial part, you can use any type of electronic model and component with only a little research.
Modeling LEDs
LEDs are a bit trickier in the fact that modeling them requires some knowledge about their parameters and curve-fitting. So, to model them I just looked up “LED ngspice”. I found multiple people posting their “LED models” and I decided to go with this “ *Typ RED GaAs LED: Vf=1.7V Vr=4V If=40mA trr=3uS .MODEL LED1 D (IS=93.2P RS=42M N=3.73 BV=4 IBV=10U + CJO=2.97P VJ=.75 M=.333 TT=4.32U)?”
- We will pick “LED ” from the symbol menu and paste this code into the empty space below the libraries in “Edit spice model ”. We’re also going to switch on alternate node sequence and write “2 1 ”, as shown in figure 1
After adding some final touches, like the resistors and connecting the wires, we are ready to start simulating!!
Simulating
Simulating is complex so in this tutorial we will explain the basics and how you can get started.
- First, we open the simulator from the tools tab in the upper ribbon (figure 1)
- Then we go to the simulation tab in the upper ribbon and click settings, from there we can specify what kind of simulation we want to run, and its parameters. (figure 2)
We want to run a transient simulation. There’s also DC and AC sweep available as simulation options. Dc sweep increments the value of the Dc current and reports the changes in the circles while AC monitors the frequency response.
- However, transient analysis simulates a circuit in real-time. It has 3 parameters, of which we are going to use two. Time step is how often will the simulator record results, and the final time is after how many seconds will the recording stop. We input 1 millisecond and 5 milliseconds and then ok, and then we run the simulation (figure 3)
- As you can see, in the lower text display it showed us the voltage and current values across various components. We could also graph these values by using the “add signals” button and then choosing the voltage or current of a certain component. We can also probe after we start the simulation. Probing allows us to monitor the voltage and current curves in a certain component directly by clicking it. (figure 4)
Wrapping Up
Since this circuit was supposed to be made with an LDR and a resistor, we can alter both of these components resistance and then rerun the circuit to determine the resistance values we would like for this light-controlled LED using a common-emitter npn transistor as a switch circuit.
Anddd the rest is up to you :3