PyVISA First Use

Summary

In this article I will show you how to install PyVISA, and use it to control to a Keithley 2230-30-1 Triple Channel DC Power Supply.

The Story

I have been working on a power supply for a new IoT device that I am building you can read about it here.  This power supply uses an Infineon IR3894 DC buck converter to turn 20V from a Type-C wall wart into a usable 5V for my system.  In the IRDC3894 data sheet there is a graph of efficiency versus load current that I would like to duplicate with real measurements.

In order to make this graph you need to:

  1. Turn on the Keithley 2280-30-1 power supply (that is simulating the Type-C power supply)
  2. Set the Keithley 2380-500-15 load to +1.2A (starting at 0)
  3. Read the input voltage and current
  4. Read the output voltage and current
  5. Loop back to (2) until you get to 12A
  6. Calculate the input and output power at each of the point
  7. Plot the data

Obviously this could all be done by hand… but I know that my Power supply and DC load are both programmable over “GPIB”.  I also know that theoretically I could get National Instruments Labview and it would probably know how to do something like that.  BUT… what I really wanted was to write code in Python.

So I posted on forum.tek.com and their excellent Apps engineer “Andrea C.” replied:

I have another (future) article where I will write extensively about the bewildering jungle of Test Automation.  But for today Ill say that VISA stands for Virtual Instrument Software Architecture which is basically a standard way to talk to Lab Instruments.  And, NI-VISA is the National Instrument implementation of VISA.  But wait, a quick google search lead me to the Open Source project PyVISA.  So, I’ll start there.

There will also be a future in-depth discussion of PyVisa, but this is the first step.

PyVISA

For this example, I’ll show you the simple steps of turning on the power supply and printing out the current, voltage and calculated power as retrieved from the power supply.

I always like to use a virtual environment for working with Python on my MacBook (or any computer for that matter).  So, I start by creating the virtual environment.  Then I run the PIP installer to install PyVisa (and all of its requirements).

The specific commands are:

Command Comment
python3 -m venv venv Create a Python3 virtual environment
source venv/bin/activate Turn on the virtual environment
pip install pyusb Install the Python usb interface (so that PyVISA can search for devices on the USB bus)
pip install pyvisa Install the PyVisa environment
pip install pyvisa-py Install the low level python drivers for PyVisa to use to talk to the bus

Then I tested the installation by running

Command Comment
python Startup an interactive Python environment
import visa Load the PyVISA module
rm = visa.ResourceManager() Attach to the interface
rm.list_resources() Get a list of all VISA devices attached to the interface

Here is what the screen looks like:

SCPI & Keithley 2230-30-1

OK, we have a working Python environment that can talk to the Power Supply.  Now what? In the unbelievable chaos of talking to Test Instruments, the vendors got together and agreed to a “Standard Commands for Programmable Instruments” also called SCPI.  In order to learn about SCPI (in the context of this power supply) you can read Chapter 4 of the Keithley Power Supply user manual.  This chapter describes how commands work etc.

After the general section there is an entire chapter (5) that discusses the specific commands for this instrument.

And if you look down into the document you will find a description of each of the commands.  For instance if you issue the command “SYS:REM” it will let you take control of the instrument.

Run the Test

Now that we know everything is working, I write a Python program to do the needful.

When you start the program the power supply is off and the load is set to 2A (but has no power being applied)

When you run the program it turns on the power supply and grabs control of device.  The little symbol next to the 12 says that the device is being control via USB.

Finally here is the source code:

import visa
import time
rm = visa.ResourceManager()
vm = rm.open_resource('USB0::1510::8752::9202095::0::INSTR')

# Turn on remote mode (so that SCPI commands work)
vm.write('SYST:REM')
vm.write('APPL CH1, 12V, 1A')
vm.write('OUTP ON')
print('Power on... waiting for settling')
# need a little delay right here
time.sleep(3)

print('Taking measurements')
voltCH1 = float(vm.query('MEAS:VOLT? CH1'))
currentCH1 = float(vm.query('MEAS:CURR? CH1'))
powerCH1 = voltCH1 * currentCH1

print(f'V={voltCH1:.1f}V I={currentCH1:.2f}A P={powerCH1:.2f}W')

vm.write('OUTP OFF')
print('Turned off power')