Control your lab gear without even touching them! Log the data too.

Transcript

G'day friends, I'm going to show you how to automate your lab gear using a Python script. If you're doing an experiment with lots of set points or things to measure, it makes sense to automate the procedure. And if you're unfamiliar with Python, we have a bunch of tutorials to get you started.

Today I'm working with a Rigol power supply and bench multimeter. We're going to use a framework called Visa, which describes commands that we can send to these devices to control them. Visa is great because the commands are standard. Commands that work with one brand of power supply will probably be the same or at least very similar on another brand. Visa also takes care of the physical interfaces. You can see that the multimeter has connections for USB and serial and the power supply has the same but also an ethernet port as well. It doesn't matter what interface we choose, the code stays the same. Today I'm going to use the USB connections.

For a demonstration, I'm going to connect the channel 1 of the power supply to the input of the multimeter. Our script is going to set different voltages on channel 1 and the multimeter will measure those voltages. To set up, I'll connect the gear to my computer with USB cables, power them on and connect channel 1 to the multimeter.

Three things. We need to install a Visa package for Python, we need to install the National Instruments Visa library as a backend and we need to grab the programming manuals for these devices. I just grabbed those from the Rigol website, link in the article.

To find the USB IDs of our connected gear, let's run Python and import PyVisa. Then create a resource manager and use the I'll copy these two IDs into an empty Python script. I'm using a different text editor here, you could use Python's built-in editor if you prefer keeping things vanilla.

After doing the imports and setup, open the resources. You can tell which is which from the first two characters of these serial numbers, DP for power supply and DM for multimeter. Let's initialize the multimeter by putting it into DC voltage mode.

Time to dive into the programming manuals. The function command looks relevant. Copy the command from the manual and use it in the write function. That's it! We've got our first command, so why not test it? If I manually set the meter to current, I can see that the current is set to 0. If I manually set the meter to current mode and then run the script, it jumps back into DC voltage mode. LOL.

Now let's set the power supply to, say, 5 volts, 200 milliamps, but with the output disabled. Same process again, but with the power supply programming manual. The output command section looks promising, and a bit of poking around, I found the output state command. Grab that example, punch it into the supply's write command, and modify the on to and off.

Returning to the manual, I'm looking for a command to set the voltage and current. The apply command lets us set both simultaneously. So grab that example and modify as necessary. Let's make sure we got everything right so far. I'm going to run the script, and the power supply set points should update to 5 volts and 0.2 amps. Nice!

Time to program the test. Enable the output and create a loop to set the voltage from, say, 5 to 10 volts. Now insert the loop variable as a string into the apply command. Since the loop variable is the voltage set point, we just insert it into that part of the command. And let's import time so we can add a delay in the loop. It makes sense to turn the power supply off after our test finishes too. The script currently increases the output voltage in steps.

Now let's include a measurement from the multimeter. Grab the measure voltage dc command. Slap that in as a query. Note the question mark on the end. Create a variableTo store the query result as a number, remember, queries return strings only. So punch the result through float to get a real number out of it. That'll be useful if we want to do some math with the data at some point.

Since we have the voltage set point V and we just got V measured, let's print them to the console just to see what's happening. Here we have the set voltage on the left and the measured voltage from the multimeter on the right. Take a moment to appreciate what just happened. With nothing more than a script on our computer, we drove a power supply to different set points and performed measurements with a multimeter. We're controlling two different instruments and getting that data in just a single place. That's pretty cool.

Since the results are printed to the console, we could just call that done. We could copy this data, go off and use it for science, plotting, whatever. But we can do better. Let's log this data as a nice comma-separated file with a header row and constant column widths.

For file operations, we'll need to import OS and define a file name in the same directory as our script. Now, every loop we can write data to the file. Open the file in append mode and check if the size is zero. This is a neat trick to see if the file didn't exist before and we're about to create it. If so, we can print a header row to define what each column of data means. Don't forget the newline character.

After that check, we'll log the data. I count the number of characters in each column header so I can format the data to the same width. Here the first column needs to be 12 characters wide and the second needs to be 13. Make sure you put your variables into format in the same order as your headings appear. And here I'm changing the structure of the loop to a while loop because I'm trying to fix an error that has nothing to do with that. Finally, I figure out that I just didn't format this string correctly for floating points. Include those Fs. While we're here, let's also make sure the voltage is zero to begin with.

Now for the moment of truth. All right. When we run the script, the log file appears in the file tree. If I open it up, we can see two beautifully formatted columns of test data. One last adjustment. I'm going to increase the precision on the multimeter readings and run the test again. Let's go for five decimal places. Returning to the data file, we can see new data streaming in from this new higher precision test. Oh man, this is kind of a big deal. We can orchestrate a test from our computer with multiple bits of gear and log the results. So we can do some science, make some plots. Oh, I love a good plot.

If you've got an oscilloscope, you could be measuring waveform properties like frequency, amplitude, even like complex statistical operations. Programmable load. We've got, you know, power supply testing and characterization. Whoops. If you've got a function generator, you could program in like custom signal shapes or just generate sine waves at various frequencies. I don't know about you, but I kind of feel like a wizard right now.

If you're not watching this from the article, click the tutorial link to get the example code and documentation. If you found this tutorial useful, let us know. And finally, if you have any questions or uncertainties, the best place for questions is our forums. Thanks for watching.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.