Python is everywhere. Its capabilities continue to grow. Not only can you create simple scripts, but you can create full-blown applications with it. The core has been scaled down to run on 32-bit microcontrollers like the ESP32 and Adafruit Feather M0. You can even use Python engineer modules to design stuff like circuits. There are electronics Python modules that create schematics, simulate circuits, and make solving math a cinch. Here are some of the modules I found that make Python usable for (electronics) engineering.
Up front, make sure you have a functioning Python environment. Update the package manager “pip” since all of these electronics python modules rely on it. Speaking of dependences, you may need to also install third-party libraries for some of them. From what I can tell, these all should be platform independent. However, I only tested these electronic modules with 64-bit Windows.
pip install skidl
Dave Vandenbout‘s tool creates schematics entirely in text. Or more correctly, describes them as a netlist file. Using Python objects, you instantiate components, connect the pins, and (optionally) check for errors. At first, this method might seem somewhat limiting. For simple schematics that get turned into a PCB, SKiDL enables a shortcut. There are some seemingly intricate designs which are merely connecting multiple ASICs. In that case, is a graphical schematic necessary? Regardless of which use model fits you, having SKiDL in your back pocket of electronics Python tricks seems like a good idea.
pip install pyspice
(License covers the PySpice Module, but not the underlying engine you pick.)
Need to simulate a circuit or part of one? There are plenty of simulation options available. You could use the Falsted Simulator, LTSpice, KiCad, ngspice, Quacs, or xyce. And yet, even with those, there is another option: PySpice. PySpice provides the interface to circuit simulators like Ngspice or Xyce. PySpice is more than just a ngspice wrapper. Although, you could treat it as such.
Before using this electronics Python module, you need to have an understanding of SPICE and how the engine you pick works.
3. Pint makes Units Easy
pip install pint
If you’re doing engineering math, keeping track of units can be cumbersome. While not specific to electronics, Pint is one of the best Python engineering modules. It simplifies manipulating physical quantities. Many of the examples are around other physics disciplines. Pint handles electronics quantities just fine. Here’s an example calculating with Ohm’s law.
from pint import UnitRegistry ureg = UnitRegistry() # Ohm's law example resistance = 5.0 * ureg.ohm current = 2.5 * ureg.milliampere voltage = (resistance * current).to('volt') # default unit print(voltage.to('volt')) # easier to read print(voltage.to('millivolt')) # result: # 0.0125 volt #12.5 millivolt
As the example shows, Pint is more than an engineering notation handler. The parser does need some help when doing calculations with different units. As you can see in the Ohm’s Law example, I need to tell it my result is in volts. However, it is a small price to pay to make handling prefixes straightforward and readable.
pip install numpy
import numpy as np a = np.arange(15).reshape(3, 5) print (a)
The example I used for Pint is simple algebra. When you need to work with linear algebra, Fourier transforms, and array objects, it is time to install NumPy. Recently I did some work with the Zynq development board. Like most computer vision examples, all of the Zynq’s relied on NumPy. The about-page for NumPy says it can store generic data in a multi-dimensional container, like a database. Long story short, if you’re doing anything above basic math, you need to consider using NumPy.
Also, Pint works just fine with NumPy objects!
Even though I wanted to focus on Python engineering modules, I did want to point out this page. It has 10 additional modules expanding beyond NumPy. So if you’re doing anything science or engineering related, you may want to consider some of these modules. And since most math needs plotting, you need to check out the next electronics Python module.
5. Matplotlib – Graph Plotting
pip install matplotlib
Engineering and plotting go hand-in-hand. Matplotlib provides extensive visualization capabilities to any math or engineering based Python program. If you’re familiar with MatLab, you’ll be at home with Matplotlib. An additional benefit is that it is tied tightly into Juypter Notebook, for easy documentation
Here is a sine wave example.
import numpy as np import matplotlib.pyplot as plot # Get x values of the sine wave time = np.arange(0,10,0.1) amplitude = np.sin(time) plot.plot(time, amplitude) # text labels plot.title('Sine wave') plot.xlabel('Time') plot.ylabel('Amplitude = sin(time)') # Generate the grid/axes plot.grid(True, which='both') # Generate the actual data plot plot.axhline(y=0, color='k') # Display plot plot.show()
6. Jupyter Notebook
pip install jupyter
License: Modified BSD
Okay, this last one is not “just” a module. Jupyternotebook (or Juypter if you mispell it like I do) is a fantastic engineering notebook tool. Here is the shortest way I can describe it: Google Docs + Python. Jupyter runs a local web service on your machine. It creates notebook pages that combine rich-text, python code, and binary objects. In a single in-line page, you can write some instructions on how to perform a test, the code used to test it, and have the results. Even better, libraries like matplotlib integrate with Jupyter.
Oh, if it wasn’t clear before, you can execute Python code inline. Before, I mentioned a notebook page could contain a Python code block. Clicking on that block and hitting shift-enter causes the code to execute. For an example of how this amazing tool works, check out the Pynq video review I made. I briefly show how it works there. Later, I plan to come back and do some more in-depth coverage on Jupyter.
In the meantime, I have been experimenting with using it to replace, or at least augment, Evernote. At this point, I like the idea of taking raw notes in Jupyter and then exporting the “final” version to Evernote. To keep my notebooks in sync, I keep the data files in a Dropbox folder.
If you install Jupyter, you can download and use this notebook file. It contains the Ohm’s Law example from the Pint section above. Just keep in mind, you’ll need to have Python and Pint installed for the shift-enter trick to work.
7. and 8. Twitter Mentions for Python Engineering Modules
Last, but not least, I wanted to mention two tools mentioned by Twitter followers. Both of these are modules I heard about from other people. However, I have not spent much time experimenting with them yet. I am including them because I trust who sent them to me.
PCBmodE from @BoldPort
I2cdesvice from @Gadgetoid
If you’re developing I2C devices, i2cdevice may help to keep track of the register addresses. It isn’t intended to be a tool used with production I2C code. Instead, it is a development tool to help develop production code.
When I started putting this list together, I had three electronics python modules in mind. As I started diving into all of the modules I learned about over the past year, the list more than doubled. I suspect that I might have missed a few key engineering Python modules. If so, let us all know what you recommend installing in the comments below.