How to create a virtual oscilloscope using the Python programming language and ADALM2000

The purpose of this article is to demonstrate how users can develop the required virtual laboratory instruments using the ADI ADALM2000 and the simple open source programming language Python. Through the combination of Python and ADALM2000, a variety of virtual laboratory instruments can be developed, such as oscilloscopes, signal generators, digital multimeters, etc. Among them, the oscilloscope, as one of the basic instruments commonly used in the physical electronics laboratory, is a good choice for entry, and will be discussed in the following sections.

By: Christian Jason Garcia & Arnie Mae Baes, ADI Software Systems Engineers

A full-featured physics laboratory is expensive, and the various experimental instruments are often expensive and complicated to manage. Imagine being able to build a virtual Electronic laboratory that can be put into a pocket and is portable at any time, which will bring infinite possibilities in the future. The virtual electronic laboratory is a simulated laboratory environment composed of simulated electronic instruments realized by a series of software-based applications, and users can carry out a large number of electronic experiments in this environment.

The purpose of this article is to demonstrate how users can develop the required virtual laboratory instruments using the ADI ADALM2000 and the simple open source programming language Python. Through the combination of Python and ADALM2000, a variety of virtual laboratory instruments can be developed, such as oscilloscopes, signal generators, digital multimeters, etc. Among them, the oscilloscope, as one of the basic instruments commonly used in the physical electronics laboratory, is a good choice for entry, and will be discussed in the following sections.

What is an oscilloscope?

Oscilloscopes can be used for signal analysis of common and complex circuits, and are an important part of electronic engineering. Today’s oscilloscopes can be connected to a computer, so the signals captured in the oscilloscope can be stored in digital form for later analysis.

How to create a virtual oscilloscope using the Python programming language and ADALM2000
Figure 1. Schematic diagram of an oscilloscope

Oscilloscopes are used to visualize the voltage and time characteristics of analog or digital waveforms. Front Panel controls (amplifier trigger, sweep time, and Display) are used to adjust the Display for better visualization of the signal.

An oscilloscope shows the behavior of a signal input over a specific period of time, which is critical for analyzing common circuits. Additionally, it helps to verify the functionality of these circuits. This is also the main reason why the oscilloscope has become an indispensable electronic experimental equipment. ADI allows engineers to customize their own oscilloscopes to meet their needs, which can improve the analysis of specific electronic circuits.

What is ADALM2000?

The ADALM2000 is an active learning module featuring a digital oscilloscope, function generator, logic analyzer, voltmeter, spectrum and digital bus analyzer, and two programmable power supplies. For basic users or students, Scopy can be connected with ADALM2000. For application developers, the libm2k library can be used to develop application interfaces. For firmware developers, there is also the option to develop custom software or HDL that can run directly on the ADALM2000.

start using

Install Python and PyCharm

Python is a powerful, easy-to-learn open-source programming language. Python can be downloaded from the official Python website. If you are not sure which version to use, choose Python 3.7.

Python can be used without an integrated development environment (IDE), but for easier library downloading and debugging, PyCharm can be used. PyCharm is an IDE that provides developers with several necessary tools, making it a popular IDE for Python development. Download the latest version of PyCharm Community from the JetBrains official website.

Install the library

Python libraries contain methods or functions that can be used in specific applications. In this article, libm2k, matplotlib and NumPy will be used.

libm2k

To interact with the ADALM2000 using Python, the libm2k library needs to be installed. This is a C++ library with available libraries for Python, C#, MATLAB®and LabVIEW®The binding has the following functions:

• AnalogIn for oscilloscope or voltmeter. We’ll focus on that feature.
• AnalogOut for signal generators.
• Digital for logic analyzers or pattern generators.
• PowerSupply for constant voltage generators.
• DMMs are used in digital multimeters.

Install libm2k

One way to install the library is to follow these steps:

• Go to the publish page.

■Download the latest executable version of the library. Example: Libm2k-0.4.0-Windows-Setup.exe

• Run executable files. When the Settings window prompts you to choose a different task, be sure to choose to install the libm2k Python bindings.


Figure 2. Libm2k installation window

• Installation is complete. Libm2k will be installed in Python’s default environment.

Matplotlib

To create an oscilloscope display, you need to use the matplotlib library. This library is popular and easy to use for customizing and displaying visualizations in Python. For more information on this library, visit the matplotlib website.

NumPy

Simple oscilloscopes will still require a lot of math. The NumPy library can provide simple functions for complex calculations. For details on this library, visit the NumPy website.

Install Matplotlib and NumPy

To install matplotlib and NumPy, follow these steps in PyCharm:

• Go to File > Settings > Project Interpreter.
• Click the + icon on the right side of the Settings window.
• The Available Packages window will appear. In the search box, search for matplotlib and NumPy.
• Specify the version to install (select the latest version).
• Click the Install Package button.


Figure 3. Installing the library package in PyCharm

hardware setup

Before we start coding, let’s set up the hardware components. The following hardware components are required:

• Signal source (or signal generator, if applicable)
• ADALM2000
• Probes and Limiters

If a signal generator is available, use the probe and/or limiter to connect the ADALM2000 device to Channel 1 and Channel 2 in the configuration shown in Figure 4.


Figure 4. Actual setup using signal generator and ADALM2000

Table 1. Pin Configuration

Signal generator

ADALM2000

Ch1 positive lead (+)

1+

Ch1

1C

Ch2 positive lead (+)

2+

Ch2

2C

The same configuration can also be followed for other available sources. Finally, connect the ADALM2000 device to the PC via the USB port.

Simple virtual oscilloscope

In this section, the program will be introduced block by block. It will also discuss what the code does, and explain why it is written in these ways. Subsequent sections demonstrate modifying the underlying code to add more functionality to meet developer use-case requirements.

First, import the three libraries (libm2k, matplotlib, and NumPy) that will be used to develop the virtual oscilloscope.

A Uniform Resource Identifier (URI) is a unique identifier for each ADALM2000 connected to a PC. This code block ensures that the ADALM2000 is connected to the PC. If no ADALM2000 device is plugged into the PC, the code will automatically exit.

Connect to ADALM2000 via the detected URI. “uri[0]” is the URI of the first ADALM2000 device detected with multiple devices connected.

Run calibration on ADC and DAC. This is an important step in ensuring accurate measurements are obtained.

Set the sampling rate and duration. Available sample rates are 1 kHz, 10 kHz, 100 kHz, 1 MHz, 10 MHz, and 100 MHz. Sampling rate is the number of times a sample is obtained in 1 second, and duration is the continuous sampling time that these samples are obtained. For example, if you set the sample rate to 1000 and the duration to 3, you will get 1000 samples per second and keep sampling for 3 seconds. Therefore, there are 3000 samples in total.

Enable and set channel 1 as the analog input of the oscilloscope.

Linspace is used to create equally spaced sample arrays, and this NumPy function can be used to create an array of time x-axis data. The first and second arguments to this function represent the start and end values ​​of the array, respectively. The last parameter is the number of samples you wish to generate within the range of start and end values.

In this example, the start value is 0 and the end value is the set duration, which is 3. For the number of samples, multiply the duration by the sample_rate to get the total number of samples required, which is 3000 samples.These 3000 samples will

Evenly placed between 0 and 3. This array will be stored in time_x.

data_y stores the waveform samples we collected using the ADALM2000 device. The samples for channel 1 are stored in data_y[0]the samples of channel 2 are stored in data_y[1]middle. In order to display the exact waveform frequency, the same number of samples as time_x must be used.

Create a graph that will be processed. The plt.subplots function will return the figure object (stored in g) and the axes object (stored in ax) which will be used to customize the entire figure.

Here you can add grid lines as reference coordinates for the waveform. Add axis labels and y limits to add more detail about the graph.

Display graphics.

Destroy the context at the end of the code.

Run the code and you will see something like Figure 5.


Figure 5. Single Channel Sine Wave Output; One Signal Generator Output: 10 Hz, 2 V pp

Dual channel virtual oscilloscope

In this section, we will use the code from the previous section and add more code blocks to create a two-channel virtual oscilloscope.

To add another channel, copy the ocsi.enableChannel and ocsi.setRange lines of code and change the first parameter from

When creating the graph, add another graph for channel 2. The data of channel 2 is in data_y[1]in the array. You can also customize the colors of the two graphs to easily distinguish them. In this example, channel 1 uses a light coral color and channel 2 uses a steel blue color.

Running the code, you should get results similar to Figure 6.


Figure 6. Dual channel sine wave output. Channel 1 signal generator output: 10 Hz, 2 V pp; Channel 2 signal generator output: 5 Hz, 3 V pp.

Other features of the virtual oscilloscope

In this section, additional functionality will be added to the virtual oscilloscope to improve interactivity. Matplotlib provides several widgets that we can use. In this example, the text label and slider widget will be used, and the code from the previous section will continue.

Add another import for matplotlib sliders.

Convert time and data arrays to NumPy arrays. These arrays will be used in the calculations performed in the next code block.

Once all waveform data has been acquired, extracting the characteristics of these waveforms is a no-brainer. In the following code block, V is extracted from the acquired data for both channelsppVaveand Vrms. To calculate Vpp, adding the absolute values ​​of the maximum and minimum values ​​found in the data_y numpy array. To calculate Vavejust use VppDivide by pi. To calculate Vrmswith VppDivide by 2 and multiply by √2.

This block of code is similar to the previous section. The only difference is that instead of raw arrays, NumPy arrays are used for the graph. A waveform object is also created from the graph. These objects will be used later.

To display the calculated V in the graphppVaveand Vrms, will utilize the text label widget from the matplotlib library. Create the string labels label_ch1 and label_ch2, ​​then concatenate the two strings to create the final label n_label. Create text labels by using plt.text. The first and second arguments (0.2, 3) are the x and y positions of the text. The third parameter is the string to display. The fourth and fifth parameters are the text and box styles, respectively.

Next, create the offset slider. This slider is used to adjust the reference level of the waveform. Adjust the main graphic to the left to make room for the slider. plt.axes defines the size, position and surface color of the slider. The Slider function is used to create objects with specific properties for offset sliders.

Create the update_offset function and register it with the offset_slider object. Every time the value of the slider is changed, the function adds an offset to the waveform.

Run the code and you will see something like Figure 7.


Figure 7. Default two-channel sine wave output with offset slider

Try using the slider to adjust the offset. You will see the waveform move up and down in real time.


Figure 8. Adjust the offset slider (slide to the left) to adjust the offset of the two channel outputs

Summarize

This article explains the importance and convenience of having a virtual electronics lab. The paper also demonstrates how to develop a virtual oscilloscope using ADALM2000 and Python. Software requirements and hardware setup are discussed and 3 examples are provided for reference.

About Analog Devices

ADI is the world’s leading high-performance analog technology company dedicated to solving the toughest engineering design challenges. With outstanding detection, measurement, power, connection and interpretation technology, build intelligent bridges between the real and digital worlds, thereby helping customers to re-understand the world around them. For details, please visit ADI’s official website www.analog.com/cn.

About the author

Arnie Mae Baes joined Analog Devices in December 2019 as a Firmware Engineer. In her first year at the company, she focused on GUI and firmware development. In December 2020, she joined the Consumer Electronics Software Engineering department and now focuses on firmware test development. She graduated from the National University of Batangas in the Philippines with a bachelor’s degree in electrical engineering. Contact: arniemae.baes@analog.com.

Christian Jason Garcia is a Firmware Verification Engineer at Analog Devices in General Trias, Philippines. He holds a bachelor’s degree in electrical and communications engineering from the University of St. Thomas and joined Analog Devices in November 2018. He specializes in software testing and system validation of SmartMesh networks in the e-mobility sector. Contact: christian.garcia@analog.com.

The Links:   SX25S003-ZZA LM150X05-C2