# Circuit Simulation

I am making a circuit simulation program, and I am not sure how to simulate the behavior of Diodes.

Take this circuit:
------100 Resistor -----LED------
Positive 5V-----470 Resistor-----|                                                     |---------Negative 5V
-----------200 Resistor----------

How would you calculate the voltage and current through all of these components?

Also, is it true that with a simple circuit like this:

Positive 5V------470 Resistor------LED--------Negative 5V

That you would subtract the forward voltage of the LED from the Battery's voltage, then use Ohm's law to calculate the current and resistance through the circuit?

voltage = 5;
newVoltage = voltage - forwardVoltage;
current = newVoltage / 470;
resistance = voltage / current;

I don't have a background in electrical engineering - so I am really clueless. Thanks for the help.

-

Yes, you are correct in your last example, assuming a red LED with 2.0V over it, the current will be $\frac{(10-2)V}{470Ω} \approx 17mA$. For your first example, I assume you already know that the total resistance of something in parallel is $\frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2} + \dots + \frac{1}{R_n}$ and you are struggling of how to calculate the total resistance of 100Ω, 200Ω and the LED, right? I am not sure if it is possible to do it that way. Instead look at each of the current paths. Some current will flow through $R_1$ (470Ω) and further through $R_2$ (200Ω). Let's call this current $I_A$. Then some current will flow through $R_1$, through $R_3$ and the LED, $I_B$.

Let's call the voltages over $R_1$, $R_2$ and $R_3$ for $U_1$, $U_2$ and $U_3$. Then $U_1 = (I_A + I_B) \times R_1$, $U_2 = I_A \times R_2$ and $U_3 = I_B \times R_3$. The voltage over $R_2$ will be the same as over $R_3$ and the LED combined, i.e. $U_2 = U_3 + U_{LED}$. You already know that the total voltage over all components is 10V. So

$U_1 + U_2 = 10V$

$U_1 + U_3 + U_{LED} = 10V$

and this gives you two equations with two unknowns, $I_A$ and $I_B$, which is standard math to solve.

-
YOU ARE AWESOME –  user434565 Sep 4 '11 at 22:49

This depends on how accurate you want things to be, for example you could describe a silicon diode as having a 0.7V forward drop and a roughly exponential I/V characteristic, but what about reverse voltages? junction capacitance? Temperature effects? Looking at the Wiki page on diode modelling gives some good info about how you would go about producing a mathematical model of a diode. The source for SPICE would probably be an excellent place to see a working example of this. Look at the average diode model in SPICE and you will see for a seemingly simple component this can get very involved indeed.

I'm not exactly sure how SPICE works internally, but I would look at nodal and mesh analysis, matrix solvers and suchlike - I found this which seems to give some basic insight into the workings.

EDIT - From further comments it seems you are trying to produce a "learning friendly" circuit simulator (correct me if wrong here)
I think the way to go might be to take the Berkeley SPICE engine (which is based on years of hard work so almost impossible for one person to emulate) and wrap it up in the way you want to, e.g. more instructive help, graphing, etc. Note that this has almost certainly been done somewhere, though it probably won't be free. There are lots of professional tools that took the original Berkeley SPICE (e.g. PSPICE - you may want to take a look at the student evaluation of MultiSim from NI and see if that is close to the type of thing you are thinking of)

As far as the "electronic playground" goes, those type of things have been around for years, and I wouldn't underestimate their usefulness too much - after all a simple transistor circuit or a transformer can take an awful lot of understanding. I think they certainly serve a purpose to spark an interest in things.
I guess the next step up would be breadboards, dev boards and a bit more freedom component wise, then producing PCBs and SMD stuff.

-

If you are doing this for fun and interest then congratulations - it's an immensely challenging task and achieving anything like good and useful results for even a subset of typical components will take you months to years of work - but you'll certainly not be "clueless" in electrical engineering at the end.

If the aim is to obtain a useful tool and you are more interested in the end product than the learning involved in producing it then there are many easier ways to obtain one. There are many "SPICE" circuit analysis programs available and some extremely good ones are completely free. One which is free and also one of the best is LT SPICE from Linear Technology.

While their emphasis is on switching regulators the program is usable for a wide range of analog simulation tasks. They say -

LTspice IV

• LTspice IV is a high performance SPICE simulator, schematic capture and waveform viewer with enhancements and models for easing the simulation of switching regulators. Our enhancements to SPICE have made simulating switching regulators extremely fast compared to normal SPICE simulators, allowing the user to view waveforms for most switching regulators in just a few minutes. Included in this download are LTspice IV, Macro Models for 80% of Linear Technology's switching regulators, over 200 op amp models, as well as resistors, transistors and MOSFET models.

Using a commercial SPICE program does not mean that you miss out on the learning process of "how things work". You can for example build your own models and compare them to the programs models and see how well what you do matches theirs and how changes affect the result. SPICE can be an invaluable aid for analog engineers.

-
+1. Life's too short to skip over off-the-shelf tools when they work for you. –  Jason S Sep 4 '11 at 23:16
Lol I'm definitely not making this to actually use for myself - It's a high school independent study. I'm trying to make a learning tool to teach high school kids simple electronics without using those stupid electric playgrounds. –  user434565 Sep 4 '11 at 23:38
@user434565 - sounds like a worthy objective. But, don't mistake LT SPICE for a stupid electric playground - it's a superb tool whose \$0 price hides its value and capability. –  Russell McMahon Sep 4 '11 at 23:42
I meant this: google.com/products/… –  user434565 Sep 4 '11 at 23:44
It's what we have to use in physics class to learn about simple circuits - I hate it so much –  user434565 Sep 4 '11 at 23:45