Using Python to automate measurements
Keywords:software LabVIEW Python GUI data-acquisition system
Don't think that Python is "just" a good teaching or academic language with little or no professional applications. On the contrary, Python is used heavily in web applications and data analysis by many top organisations such as Google, Yahoo and NASA. It is a very attractive language for rapid application development and it can be used to automate complex electronic instruments and make data collection more efficient.
Python's advantages are not limited to its ease of use. Python scripts can be run cross-platform on any major operative system, as long as the free Python interpreter is installed. Python is also extremely powerful and is extensively used for data analysis and complex mathematical calculations.
Why consider Python for laboratory automation? Most of the test setups I implement are quite simple: 95% of the time the task involves measuring one or more signals (such as voltage, current, or temperature) at different times, or over a set of values of another independent variable. Implementing this requires little more than looping through your independent variable, acquiring the signals, and finally saving the data for further analysis. It is really simple to do this in Python, thanks to its straightforward, no-nonsense grammar and its useful, handy libraries.
In addition, a Python script is very easy to modify. If you later decide you want to acquire your signals over two independent variables instead of one, all you need to do is to nest the loop you had designed before inside another loop. This might require only a handful of new lines of code. Thanks to Python's high legibility, you can easily change scripts written by other people as well (something I always dreaded to do with LabVIEW applications).
A programming language has a definite edge over graphical languages as the complexity increases. Python is excellent at math and data analysis and it is used by data scientists to extract trends from gigantic, complex data sets. Many people are used to relying on MATLAB for complex data analysis. As a matter of fact, Python is also an excellent (and free) replacement for MATLAB, thanks to its many MATLAB-compatible libraries (as shown in the example at the end of the article). I often prefer Python over Excel for plotting graphs too, unless the graphs are really simple and the dataset is small. If you are interested in using Python for data analysis, I recommend Python for Data Analysis by Wes McKinney or enrolling in the free online course Intro to Data Science on Udacity.
If you've ever used a programming language, you likely didn't have any problem following me until this point, but you may be wondering how Python communicate with measurement instruments. Not to worry, as there is a library for that, too: PyVISA is an easy-to-use package that connects Python scripts to GPIB, RS232, USB, and Ethernet instruments.
LabVIEW is still the best option to make applications with user friendly GUIs. 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, see Rapid GUI Programming with Python and QT by Mark Summerfield.
If you want to learn Python, I recommend enrolling in a Massive Open Online Course (MOOC) such as Udacity, Coursera or Udemy. Introductory programming courses are usually free of charge and taught by some of the best engineers and teachers in the field. Python has a very minimal setup and shallow learning curve, so you will be able to write useful applications in less than a day.
Mac and Linux users will find Python already available in the terminal and only need to install a few additional libraries using a package management system such as pip. For Windows users, I recommend installing Python(x,y), which contains a scientific-oriented Python distribution with all the libraries you can possibly need in a single package. I usually also install IPython, a command shell that enables interactive computing in Python and makes developing new applications even easier.
About the author
Fabrizio Guerrieri is a Full-Stack Electrical Engineer with experience from IC design to coding and product marketing. Currently Guerrieri is a Sr. System/Application Engineer at Maxim Integrated working on isolated DC-DC regulators and Battery Management Systems. Before Maxim, Guerrieri managed a team of Application Engineers at Volterra Semiconductor, invented of the world's first Single-Photon Counting Camera and worked on quantum imaging at MIT. Guerrieri has a PhD in EE from Politecnico di Milano.
Related Articles | Editor's Choice |
Visit Asia Webinars to learn about the latest in technology and get practical design tips.