Using Python to automate measurements
Keywords:software LabVIEW Python GUI data-acquisition system
Note that the imported Numpy and Pandas libraries have been renamed to np and pd to keep the code clean. All the libraries mentioned in this article are either already available with your Python distribution, or they can be easily installed from online repositories.
Lines 4 to 5 create the objects that we will use to access the Chroma electronic load and the Keysight DAQ. This is where PyVISA comes into play: all we need to do is to call the instrument method and provide a string to indicate the GPIB address of the instruments on the bus.
Line 6 creates the results dataframe to store the measurement results. A dataframe is a two-dimensional labelled data structure with columns of potentially different data types. Using a dataframe instead of an array will allow us to reference to columns using an easy-to-remember string instead of number, and to mix numbers and text in the data itself.
Line 7 creates an array of real numbers from 0 to 20 with a step of 2. These numbers will represent the values of the output current in amps at which we want to measure VOUT.
Line 8 is used to construct the "for" loop. Note that the syntax is very easy to understand: every time the loop is executed a variable called load will be generated with a value equal to a new element of the loads array. The loop ends when all the elements of the array have been used. It is interesting to highlight how Python uses indentation to define code hierarchy, without relying on any type of parenthesis. This is very useful because it keeps the code clean and legible.
Now that we have the defined our main for loop, we need to talk to the instruments to program the current, then read the voltage, and save the results.
Talking to instruments saving data, and plotting
Look at the second part of the code:
for load in loads: # 8
chroma.write('CURR:STAT:L1 %.2f' % load) # 9
chroma.write('LOAD ON') # 10
time.sleep(1) # 11
temp = {} # 12
daq.write('MEAS:VOLT:DC? AUTO,DEF,(@101)') # 13
temp['Vout'] = float(daq.read()) # 14
daq.write('MEAS:VOLT:DC? AUTO,DEF,(@102)') # 15
temp['Iout'] = float(daq.read())/0.004 # 16
results = results.append(temp, ignore_index=True) # 17
print "%.2fA\t%.3fV" % (temp['Iout'],temp['Vout']) # 18
chroma.write('LOAD OFF') # 19
results.to_csv('Results.csv') # 20
Lines 9 to 10 configure the desired load current and turn the load on. Communicating with instruments over GPIB is all about using read/write methods and knowing the command strings the instrument accepts as indicated in the instrument manual. Similar to other programming languages, %.2f is a placeholder that is replaced at run-time with the content of the variable load. It also indicates that we want the data to be represented as a real number with two decimal digits. Line 11 generates a one-second delay, useful to make sure the instruments and the circuits have reached a steady-state condition.
Line 12 generates an empty object (called dictionary in Python) that we use to temporarily store the results of one iteration of the loop.
Lines 13 to 16 are used to measure the output voltage and current. The first command tells the instrument what we want to do (measure DC voltage with automatic scale) and the desired acquisition channel. The output voltage and current are acquired on channel 101 and 102, respectively. The second command reads the result back and stores it into temp. The data is returned as a string, so it must be converted to a real number using the float function. Also, because the DAQ is measuring voltages, we need to divide the reading by the shunt resistance (0.004 Ω) to get the correct value for the current.
Note how easy it is to save data in an organised fashion using Python and Pandas: the fields of the temp dictionary don't need to be defined in advance and are accessed using meaningful strings. There's no need to remember the relationship between column number and data as we would have to do if we had instead decided to use an array to store the data.
Related Articles | Editor's Choice |
Visit Asia Webinars to learn about the latest in technology and get practical design tips.