In this video, I'll be discussing DMM programming in MATLAB. I'll be using the 34460A and 34461A
programming examples as my test code. This video can also be used as an introduction
to instrument programming in MATLAB using SCPI commands. The structure of this video
will be similar to the Visual Studio video that I posted and have linked here.
Please watch that if you are interested in programming in Visual Studio. My name is David
Tu and my avatar for this video will be Joseph Ducreux. I figured if you're using MATLAB,
you have some swagger. I'll be going over some basic instrument communications in MATLAB
and the usage of the instrument control library.
I'll be discussing developing in MATLAB and some of the best practices that are used including
try/catch routines and a function to capture instrument errors. In this exercise, I'll
configure the DMM to measure voltage, current, resistance, frequency, and temperature. And
finally, I'll find an example program in MATLAB to show some basic debugging techniques. Let's
go ahead and get started. I already have MATLAB open.
I have R2012b version. It shouldn't really matter as the program examples are fairly
generic. So, you shouldn't have any problems with other versions of MATLAB. However, the
one thing that you do need in your MATLAB is a toolbox called "the instrument control
toolbox". You can check that by typing in the ver command.
And you can see here, I have the instrument control toolbox installed. So, check that
out and make sure you have that installed or else these examples won't really work out
for you. The next thing I wanted to do is set my working directory to my programming
examples directory. Here I have unzipped the MATLAB programming examples into my thumb
drive.
You can double click here and open up the program examples, but the actual better way
to do that is to set your working directory in MATLAB by pressing the browser folder button
and just navigating to the correct directory. By doing that, you can actually run the programs
a lot easier. Let's go ahead and open the functions examples, .m file, which actually
will configure the DMM for the different measuring settings. Double click on that.
And it brings up the MATLAB editor. So, in the MATLAB editor is a different window than
the MATLAB environment. It is important to note that these example programs are structured
to be a building block that users can understand and modify. It's not really the most efficient
way or the best way that you can program, but it's more structured for readability,
for new users. The first thing that I wanted to discuss in the MATLAB program is addressing.
So, in this line, I have the DUT address as TCP something, something, something. But,
I've given examples for GPIB, LAN, and USB addresses. So, the easiest way to get
the IO address is to open up Agilent Connection Expert and click on the instrument that you
want to talk to, and just copy and paste the VISA address. You can actually also use alias
if you have those set up.
And then once you have the DUT address assigned, then you can actually open up a handle to
your instrument by using the VISA Agilent command. This is a good time to discuss best
practices in instrument coding. Let's go ahead and go back up to the top where I skipped
over a few lines. One of the things that I like to do at the very beginning is to clear
out any connection issue with the instruments.
You can clear the work space in MATLAB, but it doesn't always clear out all the instrument
connections. So, we found that if you do an instrument find, and then close out any of
those objects that are found and deleting them, that'll clear out any handles that MATLAB
might have hidden from you. Another best practice to use is a try/catch routine. So, basically,
a try/catch will try to run your main body of code, and then if there is an error that
is thrown for any reason, it'll actually be caught by this catch routine and it'll just
report on what the error is. And finally, it'll delete all objects.
delete_all_objs is a function that we wrote that basically contains all the code from
this top code right here. This new objects thing. So, it's a function. Another function
that we have is the CheckDMMError. If we go back to the MATLAB window, we will see
that we have the delete all the objects function here.
We also have our check DMM error, which is another function. Let's go ahead and open
that. And we see here that the function basically sends a SYST:ERR?, and then reads
that error back from the instruments and says, "Hey, if there's no error found in that string,
go ahead and continue with your program." However, if there is an error, we'll actually
continue to send over that system error query, and then repeat that until we can find the
no error string.
Once that's finished, we'll actually throw an exception and report on all those errors
as a string. Going back to the function examples .m file. I had mentioned about sending
commands and receiving commands, but I didn't really actually talk about how you do those.
So, the easiest way to do that is actually by sending a fprintf statement to the DMM
and then followed by the command that you want to send over. That'll send the command
to the instrument.
And then if you want to read the instrument response back, then you do a fscanf into a
variable. Let's go ahead and talk about some of the commands to configure DMM. In my first
configuration, I configure the DMM for voltage DC, 100 volts with 100 microvolts of resolution.
And then to make a reading, I do a READ? mark to actually initiate the reading. And
I do a fscanf to get the response back from the DMM. These commands can actually be found
in the Agilent true volt DMM operating and service guide, which is a zip file that you
can download.
And once you open it up, you can find the SCPI commands under the SCPI subsystem. And
the commands that I'll be using are under the configure subsystem. Going back, we can
take a look at the other commands. Here we configured the AC voltage, which is the same
range. But, here we have a bandwidth command that sets it to be 20 hertz bandwidth.
So, the trick with the bandwidth command is to set it to be lower than your input frequency,
which it will allow the DMM to accurately sample your frequency. And then we go through
DC currents and AC currents. And then Ohms 2-wire and then 4-wire Ohms. The 4-wire command
is a little bit special because this is the only one that we actually don't send the range
and the resolution command.
By not having that in there, it'll actually auto range the DMM for the 4-wire resistance,
and then let the DMM choose what it should do. Then we roll through frequency. Frequency
is a little funny because we not only configure the bandwidth, but we also the voltage range
first. So, in frequency, you can figure the bandwidth and then the voltage range separate.
Kind of opposite of the voltage settings.
And then temperature, we have to select which type of thermistor we're using, and then the
range of the thermistor. That's about it for configuring the DMM. Let's go ahead and do
a live run. I'm going to scroll back up and I'm going to set a debug break point right
after we do the *IDN? So, I'm just going to press over here on this left hand margin.
And we're about ready to press run, but I want to be able to view both the editor and
the MATLAB window on the same screen. So, let me set that up real quick. So, back in
editor, let's go ahead and press run. We'll notice that one of the first things that is
echoed back is the *IDN? response. It came back to my instrument, 418, so that's good.
We can actually step through each of the command lines by pressing this step button. And that'll
give me DC voltage results. And check the DMM error. Thankfully no error. And we can
actually run through each of these functions and see that we're getting results.
And that's the end of that function. So, that's about it for MATLAB today. Thank you very
much and I appreciate your time. And Joseph Ducreux reminds you that with Agilent, MATLAB,
and some attitude, you can accelerate your testing.