How Do Electrical Engineers Use Python?

How Do Electrical Engineers Use Python?

, Senior Back-end Developer | | Updated | 6 min read

The brief

An operator at a PCB tester who sets the power supply by hand and copies readings into Excel loses time on every board, and every manual step is a chance for a false reading. In well-run factories, software does those steps: it talks to the instruments, collects the data and builds the reports. In many cases that software runs on Python, with libraries such as PyVISA for the instruments, Pandas for the data and Django or Flask for alerts. This article covers three ways factories use Python (testing boards, monitoring machines and reporting on measurements) and where the same language runs in energy software.

Python in automation and control

One Python program can control the power supplies, relay cards and measuring instruments in a factory, whatever protocol each of them uses.

Communicating between different devices in a factory setting is challenging because machines and devices often speak different languages, with a mix of protocols like GPIB, RS232, USB, and Ethernet.

Libraries such as PyVISA make that possible: they control devices whatever their interface.

Picture a machine designed for PCB testing. When you slot in the PCB, on the reverse side, two types of pins come into play:

  • power pins that ensure the correct power supply is delivered to corresponding points on the PCB,
  • measurement pins, connected to the measuring device, that detect changes in the PCB's electrical signals.

With the right power supply secured at one location via the power pins, the machine then uses the measurement pins to take readings on the PCB, capturing variances in current or voltage.

This allows you to review and confirm the efficiency of electrical flows and voltage maintenance. Such processes or switches might occur multiple times during a single test cycle, so the assessment is thorough.

In such a machine, a Python program acts as the middleman between devices. It sends a message, waits for an answer, and when the data comes in, it processes and archives it, making it ready for immediate distribution.

The same program automates the whole testing process and controls at least three devices: a relay card that connects the appropriate points to the input/output of the corresponding measuring/power supply device, a power supply on which it sets the appropriate voltage value and outputs it, and a measuring device that sets it to the suitable measuring range and takes the reading.

First, the script reduces human mistakes such as data-entry errors, imprecise measurements or incorrect pin placements, which lead to false readings.

Second, nobody has to set power supplies or connect to points on the PCB by hand.

Finally, through API integrations the script keeps the team updated in real time and alerts them when measurements stray from the norm.

Python in maintenance and operations

Python can watch every machine on the network and tell the right person as soon as one stops working as it should.

Traditionally, if a machine faced an issue, the operator would notice and report it. But this method has its pitfalls.

An operator might miss the issue or spot it too late, risking damage to the machine and the batch of products. And even if they do notice, the message might get lost or delayed, especially if the manager isn't immediately available.

With tools like Django and Flask, Python can set up a direct communication line between machines connected to the network. It collects real-time data, so the right people get instant updates, like whether machines are online and working as they should.

When something goes wrong, the information shows up in a web application built with Django or Flask and can also go by email, SMS or notification straight to a technician.

The team knows at once, which reduces downtime and speeds up repairs.

Take PCB testing, for instance. Using Python, you can continuously measure and validate test results. If the system notices a recurring error across several PCBs, it will hint at a possible tester issue, like a bent pin.

Such subtle issues might escape the naked eye, but Python flags them immediately. This prevents production stoppages and saves the company time and material.

Optimizing electrical measurements with Python

Machines routinely gather and store measurements in databases. Engineers then extract this data to compile reports.

Imagine you're tasked with assessing the voltage stability, either the DC voltage or the RMS value of the AC voltage of a device over a set duration. Ideally, the voltage-time graph should show a steady line without oscillations.

Traditionally, a machine takes readings at certain intervals, and then an operator tediously transfers this data to an Excel sheet to create a graph.

This manual method works, but it is time-consuming and prone to errors.

With Python libraries like Pandas for data analysis and Matplotlib for visual representation, it's possible to gather data and build charts automatically. These can range from histograms, medians, and averages to maximum and minimum values.

Python can also collate this data into daily, monthly or yearly reports and convert them to formats like DOC or PDF. These reports can then be sent quickly to the relevant personnel.

Compared with the traditional manual method, the automated Python approach has clear benefits. A single Python command can run a whole series of tasks the same way every time.

Errors also become less likely, because a well-designed script repeats every step exactly, which a manual process cannot.

Python libraries for electrical engineering

For test automation, monitoring and reporting, electrical engineers use PyVISA, Pandas, Matplotlib and Django or Flask. For calculations there are NumPy, SciPy and ElectricPy:

  • PyVISA controls power supplies, relay cards and measuring instruments over GPIB, RS232, USB or Ethernet.
  • Pandas analyzes stored measurements, from averages and medians to minimum and maximum values.
  • Matplotlib plots histograms and voltage-time graphs.
  • Django and Flask run web applications that show machine status and send alerts by email or SMS.
  • NumPy and SciPy handle numerical computing, and SciPy adds a signal processing module.
  • ElectricPy adds Python functions and constants related to electrical engineering.

Python in energy software

Python also runs in energy systems. The edge code of the energy management system we built for Skyfri's solar and battery platform is written with Python asyncio and runs on Azure IoT Edge, with Azure IoT Hub in the cloud. At each site it handles charging and discharging, peak shaving, energy trading, requests from the grid operator and export limits, and it keeps working fully offline. Among its starting requirements were the high-load time windows (HLZF) from the network tariff.

Enerace.Online, where Enerace's consultants and their clients manage energy purchases, is built with Python, Django, GraphQL and React on AWS. Both projects, and the rest of our energy work, are on the Energy Hub.

Is Python always worth it?

From my experience, investing in measurement and reporting systems like these is worth it. There are exceptions, such as small businesses with limited production scales, where the cost of implementing a complex system might outweigh its benefits.

In most larger industrial plants, though, Python-based tools pay off, and engineers, operators and managers get data they can rely on.

Systems built with Python automate tasks traditionally done by humans, allowing faster and more reliable information exchange between devices.

Reports and analyses that take an engineer anywhere from several minutes to several hours can, with a single script, be available in real time or be done in just a few seconds.

Python for electrical engineers: frequently asked questions

Do electrical engineers use Python?

Yes, especially for work that connects instruments and machines to software. In factories, Python drives test equipment, watches whether machines are online and alerts technicians when something fails, and turns stored measurements into charts and reports. Energy software uses it too: the edge code of the EMS we built for Skyfri runs on Python asyncio.

Which Python libraries do electrical engineers use?

For test automation, monitoring and reporting, the usual set is PyVISA, Pandas, Matplotlib and Django or Flask. PyVISA talks to instruments, Pandas analyzes measurement data, Matplotlib plots histograms and voltage-time graphs, and Django or Flask show machine status and send alerts by email or SMS. For formulas and constants there are dedicated packages such as ElectricPy.

Can Python control lab instruments such as power supplies and multimeters?

Yes. PyVISA controls measurement devices whatever their interface: GPIB, RS232, USB or Ethernet. In a PCB test bench, Python switches the relay card, sets the supply voltage, selects the meter's measuring range, takes the reading and archives it, then repeats the cycle without manual steps.

Is Python or MATLAB better for electrical engineering?

Both are used. MATLAB is a paid product from MathWorks, extended with add-on toolboxes for areas such as control systems and signal processing (MathWorks). Python is open source and free, and one language covers instrument control with PyVISA, data analysis with Pandas and web applications with Django. For test automation and reporting, Python handles the whole chain.

Work with high-tech specialists

Let's talk about your project!

Accessibility settings

Text size

100%