Control a Tektronix Function Generator in Python

Control a Tektronix Function Generator in Python

View the app user interface

View the app code on GitHub

A function generator is a common testing device that generates a variety of electrical waveforms. This Dash DAQ application facilitates the control of a Tektronix AFG3021 function generator.

Try out the demo app, and keep reading to see how we built it.

We created this app alongside another that we wrote for the Tektronix oscilloscope.

The setup

In this setup, the function generator (middle) is connected to the computer (top) with a  simple USB-B-to-USB connection. Our oscilloscope (bottom) connects to the computer using a GPIB-USB-HS adapter.

To interface with the device, we wrote a driver with PyVISA, taking advantage of its versatility with various instruments. But modules such as PySerial can be used with the proper hardware.

The Dash DAQ app

The first step is to import the  necessary libraries from Plotly’s dash libraries, general Python packages, and helper libraries, like the fgen_afg3021 and osc_tds350 modules. You also need the dash_daq library.

modern layout

First, we create the layout for our app. What will it look like? In this layout, we put the function generator components alongside the oscilloscope’s display. Dash DAQ PowerButton components  will turn on interfacing between the app, function generator, and oscilloscope.

Next, we create Dash DAQ Knob components that will update frequency, amplitude, and offset values, with simple turns of the dials. Using these knob components, you can set default values, min/max ranges, and step amounts.

To display the frequency, amplitude, and offset values, we use Dash DAQ LEDDisplay components. The LED displays will reflect real-time updates thanks to the Dash callbacks, which we’ll introduce later.

Radio buttons, from the dash_core_components module, will  allow the user to change the waveform type displayed by the Tektronix function generator. The waveform has sine, square, and ramp options.

That’s it for our layout – Dash DAQ components are designed to resemble common hardware control and measurement elements, so using them is intuitive and easy.

powerful interactivity

A Dash DAQ app’s interactivity comes from Dash callbacks, which allow the UI to update in real-time as users interact with the controls or data from the device comes in.

In this app, it’s thanks to a Dash callback that the power buttons we created earlier allow the user to turn on the connection between the app and the function generator. Below is an example of a callback that updates the device’s frequency when a knob component is adjusted:

In this example, a Dash callback takes the frequency value from the knob component and returns it to a LED display component. In this process, the fgen_afg3021 module’s set_frequency method is called, as shown here:

The update_frequency_display callback is used similarly to set the device’s amplitude and offset parameters.