Where can EEs use Python in their day to day? Here's a look at the applications where Python excels.

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.

 

Conclusion

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.

 

Comments

10 Comments


  • jeffl_2 2019-05-10

    From what I can tell here you must work at a company where either nobody who isn’t in engineering wants to work or is qualified to do their job competently (since it is obvious from your description that you alone are expected to do inventory, QA/QC and test, customer service and IT in addition to design). Why on earth would you stay there? Better yet why are you trying to convince the rest of us that companies that behave like this are “normal”?

    • vignesh waran 3 2019-05-12

      you may be don’t worked in small scale industries or worked in company at early stage, (i developed test tool in python for production, so the non technical employee will easily test with this tool ,it just 2 or 3 days work), collecting and processing test equipment data is very useful skill

      • jeffl_2 2019-05-21

        OK I guess but take that “attitude” to a larger (read: successful) company and in your employment review you’ll be designated “unable to focus on designated goals” and you probably won’t survive the first review. You can only “play the game they want you to play” and helping everyone else out would make you a company liability. The alternative is to stay at small companies and never have a career that will make you a decent living. In the final analysis it’s up to you.

        • hoookey 2019-05-21

          There are big companies that different to your experiences I’m working at one. I’m doing some side work and my managers like it as it creates some future potential. I’m in R&D environment. BTW Google is also such a company where 20% of the time you choose what you do.

        • adx 2019-07-22

          I’ve seen it done both ways too, therefore it seems to be a point of view thing. Even in the big, culturally regimented firms, an EE might need to assist track failures (not inventory), assist with jigs, end up talking with customers who have had failures, dig into complex device code, and of course IT where the CAD support isn’t up to scratch. It’s all engineering (except perhaps keeping IT from remoting your CAD workstation).

  • DRJR 2019-05-13

    Yes. For example, here I describe my experience with Python in EE:
    https://youtu.be/1M4swkBvAqc
    I create a graphical Python application for the Raspberry Pi 3B+ for interfacing with a MAXREFDES130 board. Code for handling GUI, SPI (2 channels), I2C (2 devices), I/O and interrupts for RTC and ADC and MAX11300 (programmable 20 port I/O with ADC, DAC, analog switch and GPIO). If you are an electrical engineer who uses C in a daily basis, I recommend Python as the second language to learn.

  • vandens 2019-05-21

    I think nodeJS is more powerfull when you need asynchronus communnication on today. I hope you have got experience about this one and don’t hesitate to share the result too ini here smile .

    • cflin 2019-09-17

      Python3 has the asyncio module, but recently the uvloop module became available as a drop-in replacement to asyncio.  uvloop is written in Cython (compiled Python, which invokes the C compiler and runs as fast as C) and uses libuv, the same library as nodeJS, which makes it very fast.  See resources, below.

      Resources:
      —Getting to know asyncio:  https://www.youtube.com/watch?v=tSLDcRkgTsY
      —uvloop: https://github.com/MagicStack/uvloop
      —Gerenal knowledge about libuv, which makes uvloop “tick”: https://libuv.org/

  • stevebj.ee 2019-07-29

    I’m an EE that uses Python for collecting data from different devices that support RESTful interfaces.  Way better than the old snmp where I basically had to reverse devices to locate meaningful data.  It’s just another tool, but is great for parsing json data.

    Other times, I must write something in C or Perl. It all depends on what the function is and who might end up maintaining it. The last thing that I’d want to do is write something in C which could be performed in Perl or Python and then have people coming to me years later asking questions about something. This happens no matter how good your comments are…

    In the end, I’m automating many things that make our Operations more efficient.

  • Duptho Duptho 2019-08-08

    I am overwhelmed with this article and my interest for python programming is expanding.
    I would love to read similar articles down the line.