Electronics

Measuring Circuit Quantities In LTspice Simulations

LTspice Measuring Quantities Graphic
Written by John Woolsey

Skill Level: Intermediate

Table Of Contents

Introduction

In this tutorial, we will explore the use of the .MEASURE SPICE directive to learn how to measure various circuit quantities in LTspice simulations.

It is expected that you have a basic understanding of the SPICE simulator in general and the LTspice application in particular. If you are new to LTspice, or would just like to refresh your knowledge, please see our Getting Started With LTspice For Mac tutorial (if you are using macOS) or the LTspice IV Getting Started Guide (if you are using Windows) before proceeding with this one. The Windows based tutorial, listed on the LTspice page, was written for an earlier version of LTspice and may not contain the latest LTspice functionality. There are quite a few LTspice tutorials available out there on the ‘net for Windows systems. However, not much is available for the Mac version, which is why I wrote our tutorial.

The schematic for this tutorial is available on GitHub for your reference.

What Is Needed

  • macOS Or Windows Based Computer
  • LTspice Application

Background Information

I am using LTspice on macOS for this tutorial, but if you are using Windows, the vast majority of this tutorial should still apply, however, some minor changes may be necessary.

If you need assistance with your particular setup, post a question in the comments section below and I, or someone else, can try to help you.

Setting Up The Schematic

We will utilize the BJT amplifier schematic created in our Getting Started With LTspice For Mac tutorial as the basis for this tutorial. Retrieve the amplifier.asc schematic, available on GitHub, and save it in your default LTspice folder on your computer. Copy the schematic to a new amplifier_measure.asc file; we will be modifying this one during the course of this tutorial.

Open the amplifier_measure.asc schematic in LTspice. As described in the Getting Started With LTspice For Mac tutorial, this is a simple small signal bipolar junction transistor (BJT) common emitter amplifier with an expected gain of around 9.

Let’s run a quick simulation to make sure everything is working properly before proceeding. Click the Run! button to execute the SPICE simulation. View the Vout signal in the waveform viewer by going back to the schematic and clicking on the Vout label with the probe tool. You should see a sine wave with an amplitude just shy of 9 mV being displayed in the viewer over a time period of 5 ms.

The current list of SPICE directives is shown in the upper-left-hand corner of the schematic. Move them to the right of the schematic diagram so that we have more room to work with longer lines. On a Mac, right-click in an empty area of the schematic and then select Edit > Move from the contextual menu that appears. Create a bounding box around all the SPICE directives by clicking (and holding) the mouse button to the left and above the directives, the cursor will change to a hand, and then releasing the mouse button after moving the cursor to the right and below the directives. Then, drag and drop (with the mouse) the selected directives to the right of the schematic diagram. Press the ESC key after the operation is completed.

Save the amplifier_measure.asc schematic when you are happy with it. We are now ready to start exploring the .measure directive.

The .MEASURE Directive

The .meas[ure] SPICE directive enables us to measure, or evaluate, various electrical quantities in our circuit simulation in a textual fashion. Not only can it retrieve specific quantities, but it can also evaluate expressions of multiple quantities based on certain conditions being met. We could use the waveform viewer to plot, measure, and calculate some of these quantities manually, but the .measure directive can do much of it for us automatically.

Let’s begin our exploration with a simple one. Going back to the schematic, add the following line to the end of the current list of SPICE directives.

.meas tran vout_time find V(Vout) at 2.6m

This is accomplished on macOS by

  • right-clicking on the SPICE directives within the schematic,
  • the Edit Text on the Schematic window will appear,
  • placing the cursor at the end of the last directive line within the text box,
  • typing CTRL-<RETURN> to create a new line,
  • entering the new SPICE directive on the new line, and
  • clicking the OK button.

Your full list of directives should now look like the following:

* .op
.tran 100u 5m
.meas tran vout_time find V(Vout) at 2.6m

You can probably guess what this directive statement does, but let’s break it down in detail.

All SPICE directives are generally case insensitive, using either .measure or .MEASURE, tran or TRAN, etc. is perfectly legal.

.meas is the SPICE directive itself. I am using the short version here; you could use the full .measure directive if you prefer.

tran specifies the SPICE analysis to which the .meas statement applies. You can have different measurements applied across multiple analysis types, i.e. AC, DC, OP, TRAN, TF, and NOISE. Here, we are using tran to apply this directive to our existing .tran analysis.

vout_time is a unique user-defined label that will be printed in the simulation log to associate the evaluation output with the .measure statement that produced it. I chose this name because I am requesting the value of Vout at a particular point in time. You could use something like vout_t1 or vout_2.6ms as well; use whatever makes sense to you.

find is used to evaluate a particular quantity or expression when certain conditions are met.

V(Vout) defines the quantity or expression of interest. Here we are stating that we want to retrieve the voltage represented by the Vout label.

at 2.6m specifies the condition to which the evaluation applies. We are requesting that the value be measured at 2.6 ms after the beginning of the simulation. There is nothing magical about why I chose 2.6 ms, it is just a point in time.

Once you’re finished entering the .measure statement, run a new simulation and view the SPICE output. Right-click in an open area of the schematic and select View > SPICE Error Log to view the log file on macOS. You should see the following printed towards the top of the log file.

vout_time: v(vout)=0.00519772 at 0.0026

This is the evaluation output of our .meas statement. It is telling us that the voltage at Vout was 0.00519772 volts (≈ 5.2 mV) at time 0.0026 seconds (= 2.6 ms).

Likewise, let’s measure the current through R5 using the same time as we did for Vout. Add the following statement below the first .meas statement.

.meas tran ir5_time find I(R5) at 2.6m

Again, rerun the simulation and view the log file. The current through R5 should be 5.19772e-08 (≈ 52 nA).

ir5_time: i(r5)=5.19772e-08 at 0.0026

The next statement uses the PARAM option to evaluate expressions of previous .meas statements. Add and run the following to determine the instantaneous power of the R5 resistor at 2.6 ms.

.meas tran power_time param vout_time*ir5_time
power_time: vout_time*ir5_time=2.70163e-10

The following statement finds the voltage at Vout when Vin equals 0.5 mV, but delays 1 ms before it starts checking.

.meas tran vout_vin find V(Vout) when V(Vin)=0.5m td=1m
vout_vin: v(vout)=-0.00438958 at 0.00108296

Measuring Values Over A Range Of Time

The previous section described how to find and evaluate quantities and expressions at particular points in time. We can also use the .measure directive to evaluate over a range of time. Note, many of the subsequent statements will be used later in the tutorial, so just add them to the end of the previous statements, do not delete them.

The MIN and MAX options can be used to determine the minimum and maximum values of a quantity over a range of time.

.meas tran vin_max max V(Vin)
.meas tran vout_min min V(Vout)
.meas tran vout_max max V(Vout)
.meas tran ir4_max max I(R4) from 1m to 4m
vin_max: MAX(v(vin))=0.000998517 FROM 0 TO 0.005
vout_min: MIN(v(vout))=-0.00875624 FROM 0 TO 0.005
vout_max: MAX(v(vout))=0.00878832 FROM 0 TO 0.005
ir4_max: MAX(i(r4))=0.000974201 FROM 0.001 TO 0.004

As you can see from the last evaluation, ir4_max, a range can be specified by using the from/to attributes. If they are not specified, the entire range is used by default, in this case, the full simulation time of 0-5 ms.

We can even request peak-to-peak and root-mean-square values.

.meas tran vin_pp pp V(Vin) from=1m to=4m
.meas tran vout_pp pp V(Vout) from=1m to=4m
.meas tran vin_rms rms V(Vin)
.meas tran vout_rms rms V(Vout)
vin_pp: PP(v(vin))=0.00199703 FROM 0.001 TO 0.004
vout_pp: PP(v(vout))=0.0175446 FROM 0.001 TO 0.004
vin_rms: RMS(v(vin))=0.000707068 FROM 0 TO 0.005
vout_rms: RMS(v(vout))=0.00621205 FROM 0 TO 0.005

Notice the subtle difference in how the ranges of vin_pp and vout_pp were specified. Equal signs (=) can also be used if you prefer that style.

Remember earlier when I mentioned the expected gain of the amplifier circuit was expected to be about 9? Well now that we have some of our quantities measured, let’s utilize LTspice to calculate the simulated gain for us.

.meas tran gain_max param vout_max/vin_max
.meas tran gain_pp param vout_pp/vin_pp
.meas tran gain_rms param vout_rms/vin_rms
gain_max: vout_max/vin_max=8.80138
gain_pp: vout_pp/vin_pp=8.78531
gain_rms: vout_rms/vin_rms=8.78565

Here, we measured the gain in a few different ways: maximum, peak-to-peak, and RMS values. All calculated the simulated gain to be around 8.8.

Measuring The Time Between Two Events

Let’s try a bit more complicated example to show some of the power that can be wielded with the .measure SPICE directive. Enter and simulate the following.

.meas tran tdelay trig V(Vin) val=0.5*vin_max td=1m rise=1 targ V(Vout) val=0.5*vout_max td=1m rise=1
tdelay=0.000499531 FROM 0.00108283 TO 0.00158236

This .measure statement determines the time delay between Vin having a value of half its maximum value while it is rising (the trigger) and Vout having a value of half its maximum value while it is also rising (the target) after an initial delay of 1 ms. Do you need to read that sentence again? It is a whopper.

The tdelay evaluation output shows that the delay was about 0.5 ms that triggered around 1.08 ms and ended around 1.58 ms. It might be easier to visualize this delay by plotting V(Vout) and V(in) in the waveform viewer.

I covered just some of the capabilities that the .meas[ure] SPICE directive provides within LTspice in this tutorial. Review LTspice’s .MEASURE syntax for additional options. Please be aware that not all options seem to be covered on that syntax page. I found, and used, others that are not listed. For instance the from/to attributes used earlier in this tutorial.

Try out some of the other options or even different analysis types. Make sure to save your schematic when you are done playing.

Additional Resources

The following is a list of additional resources you may find helpful.

Summary

In this tutorial, we learned how to evaluate various circuit quantities and expressions using the .measure SPICE directive in LTspice. Specifically, we ran circuit simulations to measure items of interest at particular points in time along with over a range of time. We even explored a more elaborate statement that determines the time delay between two events.

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

Thank you for joining me along 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.

4 Comments

Leave a Comment

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