Python for Electrical Engineers

4 minute read

Many developers working close to the metal tend to stick to their knowledge of C/C++ to get things done even when not restricted by their platform, but there are much more powerful tools out there to handle some of the other tasks that come up.

One of the most versatile languages in this domain is Python. It can be viewed as the the go-to tool for what could be considered DevOps for Electrical Engineers. Here are a few ways Python can help with hardware development.

Device Interfacing

Serial interfaces ( like RS-232 and MODBUS), CAN buses, Ethernet and others are so much easier to setup compared to writing something in native C/C++ for the specific operating system you are using. Network interfacing is also very straightforward to use if you need to open up a socket and talk to a device over a network, such as a SocketCAN interface or a networked power supply.

Programming Bench Tools

Programmable tools such as power supplies, DMMs, O-Scopes, Function Generators, and others are simple to set up and run. There are already many libraries out there provided either by open-source projects or by the tool vendors to allow you to automate the data capture or device configurations allowing you to do some really neat things such as Automated Testing.

Automated Testing

It is much cheaper and faster to automate testing with Python than with expensive and inflexible systems like Labview or ATEasy. With a simple serial interface to change data on the controller, programmable test equipment and a creative harness with feedbacks or another sensing device, the integrity of the hardware can be verified. Many production test system don’t need the in-depth verification levels that you can get with a Labview setup. You can easily get 80–90% of the test coverage for about 5–10% of the cost. Of course, if you really need that extra 10%, then that extra cost might be justified.

Software Testing and Simulation

If your code base is reasonably modular and decoupled from the hardware to the point you can build and run parts of it on on your computer, ctypes might be an option. Ctypes allows you to interact with compiled C code using Python! This opens up the possibilities of mocking your hardware, setting up automated unit tests, or modeling the system interfaces using higher level tools. While this setup has the potential to be quite powerful, it can become overly complicated to setup and maintain for smaller projects.

Embedded Data

Embedded data itself is also something that is often overlooked. It requires serious tools to parse and visualize at millisecond resolution from hundreds or thousands of sources on a device. Some data sampling can easily rival the amount of data generated by “Big Data” companies/apps. Often times this data just gets dumped to a CSV file, Excel, or some other cryptic text format.

Don’t be scared to give SQL a try… Consider using a SQLite database to get started and see how powerful it can be to analyze and inspect your data. Even some simple SELECT statements can pull out some useful insights.

If you want to get really ambitious you can even send that data up to a Time Series Database (TSDB) for real time streaming and visualizations. There are tons of databases out there that can be a very useful tool in analyzing and visualizing large sets of static or real-time data.

Analytics

Libraries like SciPy, Numpy, and Pandas are very useful when you need to crunch large data sets to pull useful information out. You can look to the Big Data world for more details on this, but Python has certainly carved out its place in the world of data science.

Visualization

Matplotlib works great for quick static plots of moderate data and complements the analytics tools. There is also PyQtGraph which works great for high resolution or real-time plotting. Even implementing a time-series database with tools traditionally used by DevOps, such as Grafana, can be very useful for doing ad-hoc queries or setting up a monitoring system.

Day to Day tasks

There are also plenty of other day to day uses for Python such as code generation, automating builds, and pulling data from Excel. Using a Python template library like Jinja makes automating code generation for repetitive or dynamically configured code sections very simple.

Most engineer’s tool of choice whether you like it or not is Excel, and Python makes it very easy to pull data in and out of these sheets. Not every engineer is going to be a programmer, so Excel becomes a very important part of some organization’s workflow.

Putting all of this together can give some very powerful potential when in the right hands! While there are plenty of other languages that can do some of these things faster, better, or possibly easier, I’d challenge someone to find a language that can do all of these things as well as Python.