# 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')
```

## 9 Comments

1. pyusb is necessary？
no need NI-VISA？
in pc，need install usb-driver for 2230？
thank you ，i have a poor english hh

• I think that the answers to your question are yes, yes (no need), Yes…

You are welcome to send me an mail and Ill try to answer.

Alan

2. You wrote:

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

I am very interested … please let me know when you post more.

Thanks!

• Yes… I know I need to do some more work on this… it is on my to-do list… sorry for being slow.
Alan

3. Hello and thank You for this tutoriaI

Is there a way to have a prompt asking for example which channel You want to use and other parameters ?
I am also very new and trying to setup a spectrum analyzer with an app asking you to enter frequency. Etc, rather than defining them already in the source code…

Cheers, Aki

• For sure you could. This would be just a matter of sorting out the Python and using Qt or something for a GUI (which is what I would do next).

You will also need to understand the capabilities of that instrument … but then it would be possible.

Alan

4. Thanks a lot ! I will look into Qt as per your advice.

5. Hi, thank you for sharing this with us. I’m just getting started with PyVISA hoping to control a laser source in our lab via ethernet TCP/IP. I just set up the PyVISA and tried the rm.list_resources() command, but it returns an empty list. I have not yet connected the laser to my macbook, but I would expect it to at least show my USB mouse, and maybe the external monitor. Could you give some insights on if what I’m expecting is wrong and why? Are the mouse and monitor not considered to be “instruments”? Sorry if the answer is too obvious.

Thank you,
Xiao

• Im sorry for the unuseful and late response… but I have no idea