In the previous article, we posed the question "Why should an engineer even bother learning Python?"
Now, let’s discuss how Python can be relevant in an electrical engineer’s work.
The Swiss-Army Knife Engineer
EEs don’t just deal with circuit design. We face a slew of other tasks we have to take care of, as well. There’s firmware programming, debugging, testing prototype hardware and software, testing production samples, working on quality control, and seemingly never-ending amounts of data collection, evaluation, and analysis.
How can Python help with all this?
Enter Python, the Swiss army knife language
Python is extremely good at being an all-purpose programming language. Languages like HDLs (hardware description languages like VHDL, Verilog) do one specific thing like synthesize to hardware. C/C++ runs lean and is very well suited to embedded programming. But having a Swiss army knife language in your tool belt can help with general-purpose tasks that often make up the majority of things we handle.
Usually, these tasks would be things like testing, data collection, or automation, which constitute a large chunk of an engineer’s time. In my experience, design only takes about 5-10% of an engineer’s time. The other 90% or so is consumed by things like researching, testing prototypes, debugging, creating test jigs and programs, production test, quality control, and of course, the dreaded “D” word… documentation.
A lot of the things that come before the design (like researching parts and implementations and coming up with system architectures) can’t really be automated. Those are the tasks that make us the creative, artisanal engineers that we are. But, once the design is implemented (i.e., the fun part), there’s a lot of drudgery that still needs to be handled.
Here are some of those drudgery-type tasks and how Python can help accomplish them efficiently.
File Processing for Gerber File Submission
One of the applications most often in need of automation is file processing. Process workflows differ depending on the software, vendor, customer, etc. Most of the time, these include processing files in some format. My PCB vendor requires me to submit my Gerber files with specific file extensions so their workers are able to parse, check, and confirm that they conform to the design requirements.
Since my PCB CAD tool doesn’t output Gerbers in the required format, I need to change my file extensions every time I submit a PCB. It’s not a huge task, but since I iterate through many designs and design revisions every year, it gets tiring changing the Gerber file extensions on about 10 different files each time I submit a design to the fab.
A more substantial example of file processing is when you have a file in one format (e.g., XML) and a customer requires it in another (e.g., CSV). This is a common situation that would normally be a huge headache, if not a showstopper for some customers. But using a few libraries and some simple Python code, it’s possible to change text files between multiple formats.
Automating Test Equipment
As an EE, one of the big advantages of using Python is controlling and automating test equipment. It’s becoming more and more common to find low-cost test equipment fitted with USB outputs that can collect data and store them in common formats such as CSV.
Python was originally created for text parsing so it’s amazingly useful to sift through huge amounts of text data to extract useful information. For example, one of my portable oscilloscopes that I take with me in the field is a trusty old Hantek DSO5202P digital storage oscilloscope. It's inexpensive and can output waveforms as raw CSV data.
Hantek DSO5202P digital storage oscilloscope with USB interface
This means you can actually capture the data from some waveform, parse it, process it, and plot the output. This is live, physical data that you can now play with inside your computer and see how it behaves with your digital filters. Better yet, you can format it to feed to an arbitrary waveform generator and recreate the exact waveform for testing on a circuit.
The GPIB Interface on the back of a Keysight digital multimeter. Image from Farnell
If you are using test equipment that supports the GPIB interface, you can also use Python libraries like pyVISA to control them. You used to need to purchase a National Instruments setup to do all that, limiting it to larger companies that could afford it. Now, you can automate a test equipment setup for the price of a computer and a bit of Python know-how.
Once you can automate your test equipment, you’ll feel like a magician. You can set up closed-loop tests like stimulating a device under test with an arbitrary waveform generator and collect data on how it responds through an oscilloscope, multimeter, or datalogger. The possibilities are endless.
Flask, a Python Microframework
It doesn’t just stop with automating test equipment, though. These days, it’s becoming more common to design products that connect to some internet service or communicate with it through a REST API. As you’re developing the hardware and writing the test firmware, you can also use Python to set up a test server with a REST API to connect to.
You don’t need a fancy web developer or learn another language for this because it’s quite easy to set up a REST API using the Python/Flask framework. You can literally set up a custom REST API within an hour.
As a professional electrical engineer, you’ll likely be involved in manufacturing at some point and the ability to automate testing for production is extremely useful. The more consistent and thorough the testing, the better it is for the company’s bottom line, whether those savings come from fewer field failures, less returned stock, or less reputation damage from bad quality control.
For my circuit designs, I get custom test jigs made with the bed-of-nails probes, wiring harness, and clamping mechanism. These test jigs can be interfaced with a custom board like an Arduino or Raspberry Pi and controlled from a computer. My standard production test system consists of something like this with the wiring harness interfaced to an Arduino running the Firmata protocol firmware.
This allows me to communicate with it in Python using pyFirmata. I can set up each pin as an input or output, check the state of each pin, log all the data, generate a unique serial number, and output a big PASS or FAIL message. When the boards are at the factory assembly house, I just need to teach a person how to run the Python test script, wait for the big PASS or FAIL, and the data automatically gets stored along with statistics for that production run.
SQL: Database Management Language
Oh, did I mention storage? That brings me to the subject of databases. Actually working with databases is a pleasure in Python. It comes stock with SQLite3 which, as its name implies, is a lightweight database based on a single file.
If you want the option to migrate your code to different databases, especially server-based ones, then I recommend a library called SQLAlchemy. This allows you to interface to many of the popular relational databases like SQLite, PostgreSQL, and MySQL without having to change your code.
Now how would an EE use a database, you ask?
Well, in the automated test setup I mentioned earlier, the test system put out a serial number as well as other data such as statistics, test results, and miscellaneous notes. These can all be stored in a database that you create so there’s a log tracking the history of each board that’s produced. When a board is returned, the serial number can be looked up and the history of the board can be examined. Perhaps it was previously returned for an RMA or it failed a test once but passed on the next try.
Or even perhaps the op-amp failed on the board and, when you did a simple search of your database, found that it was the fifth time that specific op-amp failed in this production run. Insights like that improve production efficiency, bottom lines, and an engineer’s value to a company.
I could go on and on, and that’s really because Python can open whole new worlds to you as an engineer.
The foundation for electrical engineers is still electronics design. But, these days, it’s getting harder to live purely in the world of electronics. We also have to write firmware, perform testing, collect data, get involved with production and do forensics on returned or failed products.
Along with the bread and butter of electronics and C/C++, knowledge of Python can help a practicing engineer round out their skills from circuit design to playing a part in the complete product lifecycle.
So join us as we start this series and take a hands-on look at Python from the context of an electrical engineer.