8 Reasons to use Python for Laboratory Automation

Are you an Engineer working in a Lab? Let Python help you out!

8 Reasons to use Python for Laboratory Automation
UPDATE: I published an extended version of this post on Electronic Design News (EDN). In the article I present another simple but comprehensive Python script and I go over each line of code. The example script is a good starting point to get your feet wet with interfacing Python with instruments, do complex data analysis and plot graphs. Click here to read the article.

Automation can be extremely valuable to verification engineers and to anyone performing measurements in laboratories on a daily basis. I have my fair share of experience with electronic laboratory measurements and I have saved countless hours and days thanks to automated instruments and software like LabView.

LabView (LV) is a system-design platform and development environment for a visual programming language that allows to easily build applications with a friendly Graphical User Interface (GUI). It comes with several libraries and modules that allows it to talk to instruments, and therefore it can be used for laboratory automation. I'm a LabView user and I have built several applications with it in the past, but for the past few years I have started to phase it out and to replace it with Python. Yes, you heard it right: Python can be used not only to analyze data or write backend web applications, it can also be used to talk to instruments and automate lab measurements.

Here are my 8 reasons to prefer Python over LabView for laboratory automation:

1. Python is free

Last time I checked, developing LabView applications requires buying a license. I’m not sure how much does that cost, but I can guarantee it doesn’t beat $0. For Windows users looking to automate their lab I recommend installing Python(X,Y) which comes pre-packaged with all the libraries you could possibly need.

2. Python is easy to learn

Pretty much any Massive Open Online Course (MOOC) such as Udacity, Coursera, Udemy offers basic computer programming courses. The language of choice for introductory courses is often Python because it is an easy-to-understand, modern, object-oriented language used in a lot of real applications. Additionally, most of the offered courses on Python are free of charge and thought by some of the best teachers and engineers in the field.

3. Python code is very legible

Have you ever tried picking into someone else LV project? If the project is big enough, decoding what is going on can be quite difficult. Python, on the contrary, is very legible even for larger projects. This is particularly true if the programmer took care of commenting and keeping the code well organized.

4. Python can talk to instruments

The PyVISA package allows to easily create scripts that talk to all your GPIB and Ethernet instruments. Below you can find a snippet of code I copied from the PyVISA website that shows how little code is needed to discover and talk to GPIB instruments connected to your PC.

>>> import visa
>>> rm = visa.ResourceManager()
>>> rm.list_resources()
('ASRL1::INSTR', 'ASRL2::INSTR', 'GPIB0::12::INSTR')
>>> inst = rm.open_resource('GPIB0::12::INSTR')
>>> print(inst.query("*IDN?"))

5. Most automation scripts are simple

Let’s say you want to write a script that automates measuring the efficiency of a voltage regulator: what you are trying to do is, in simple words, to measure input and output voltages and currents for each value of the output current (your independent variable). This simple task can be automated using a single for loop. If you don’t believe me check out the example script I posted into my public GitHub repository. The script commands the electronic load to sink a pre-defined set of currents and, for each of them, it tells the digital acquisition instrument to measure input/output voltages and currents and it computes the efficiency. Finally the results are saved into a handy CSV file.

Diagram of a setup used to measure a voltage regulator's efficiency. An example of a Python automation script using PyVISA can be found on my GitHub.

6. Making changes to the code is easy

Do you want to measure the efficiency of the voltage regulator discussed before for different input voltages as well (adding another independent variable)? All you have to do is to nest the old code inside another for loop! This is only few extra lines of code.

7. Python is good at Math and Data Analysis

By using the NumPy, SciPy and Pandas libraries it very easy to do very complex analysis on the acquired data… and that can be extremely valuable: by processing the data right after each acquisition it is possible to obtain important insights that could allow to speed up the whole measurement process. Python is also extremely good at making complex graphs, and I often prefer it to Excel, unless the graphs are really simple and the dataset is small.

If you want to get serious about data analysis using Python I recommend reading the book Python for Data Analysis by Wes McKinney and enrolling in the free online course Intro to Data Science on Udacity.

8. You can make GUIs in Python

LabView makes creating GUIs extremely easy. The process is not as straightforward with Python, but it is not very difficult either. My GUI toolkit of choice is usually PyQT. If you are interested in knowing more about this topic I recommend reading the book Rapid GUI protgramming with Python and QT by Mark Summerfield.

Python is an ideal choice to automate your laboratory setup and avoid tedious hours of measurements because it is simple to use, easy to understand and extremely flexible and powerful. LabView isn’t dead though, as it is still the king of the GUI. In general, I think LabView is better suited for applications requiring a nice graphical interface and that don’t require to execute complex loops or data processing: for instance, I still use LabView to design most of the applications that are customer-facing which needs to be pretty and are rarely complicated. For all the other applications and automation needs though, Python is my first choice!