Example scripts

The Python examples can be installed locally after installing the pydwf package by executing the following command:

python -m pydwf extract-examples

This will create a local directory called pydwf-examples containing the Python examples that demonstrate many of the capabilities of the Digilent Waveforms devices and pydwf.

Todo

This section is currently incomplete.

Some examples that are currently missing:

  • We need more examples for the DigitalIn and DigitalOut instruments.

  • The four main instruments need examples for all their modes.

  • We need examples using inter-instrument triggering.

  • We need examples for the AnalogImpedance functionality.

  • We need examples for the Spectrum functionality.

  • We need examples for the deviceEnum and DeviceControl APIs.

The following examples are currently provided:

DeviceControl functionality example

DigitalDiscoveryLedBrightnessParameter.py

Modulate the brightness of the Digital Discovery power-on LED.

This example only works with the Digital Discovery device.

DeviceEnumeration functionality example

No example available (yet).

For now, it is recommended to have a look at the list_devices() function in the __main.py__ top-level module.

AnalogIn instrument examples

AnalogInSimple.py

This example demonstrates the easiest way to obtain samples from the analog input channels.

The method used by the example is useful if triggering or precise timing is not important.

AnalogInShiftScanShiftScreenDemo.py

This example demonstrates recording using the AnalogIn instrument, in ScanScreen or ScanShift modes.

The acquisition mode (ScanScreen or ScanShift) can be selected using a command line parameter.

AnalogInRecordMode.py

This example demonstrates acquisition using the AnalogIn instrument.

The script emits sinusoid signals on the first two channels of the AnalogOut instrument, and continuously samples the first two channels of the AnalogIn instrument.

This example assumes that the analog output channels are connected to the analog input channels.

Availability of the matplotlib package is assumed for plotting results.

AnalogOut instrument examples

AnalogOutShowChannelAndNodeInfo.py

This program starts by selecting the device configuration that has the largest count of analog output channels.

It then enumerates all these output channels and shows their capabilities, including all sub-nodes of each channel, and their capabilities.

This example is mostly interesting to show the capabilities of the rarely used third and fourth analog output channels of the Analog Discovery 2, which are only available in one device configuration. These two extra channels are essentially the V+ and V- power supply outputs.

AnalogOutSimple.py

Show simple control of the analog output channels of the AnalogOut instrument.

This example programs an AnalogOut output channel for a square output waveform, but doesn’t actually start it. By configuring the channel to drive the Initial waveform value on its output while idle, the output voltage can be controlled directly by manipulating the channel’s amplitude setting.

This method is useful if the output needs to change only occasionally, and if triggering or precise timing is not important.

This technique is much faster than the alternative approach of changing the channel’s offset setting, which takes a long time to stabilize after a change due to the presence of an analog low-pass filter. The demonstrated technique can change the analog output value at a rate of several hundred times per second.

AnalogOutPlayFunction.py

Play one of the built-in analog output waveforms using the AnalogOut instrument.

The user can select the waveform, frequency, amplitude, offset, phase, and symmetry parameters using command line options.

AnalogOutShowFunctionSymmetry.py

Show the way the symmetry setting changes the behavior of the built-in analog output waveforms of the AnalogOut instrument.

This example assumes that the first analog output channel is connected to the first analog input channel.

Availability of the matplotlib package is assumed for plotting results.

AnalogOutAmplitudeModulationDemo.py

Show the way Amplitude Modulation (AM) changes a carrier sine wave using AnalogOut instrument.

This example assumes that the first analog output channel is connected to the first analog input channel.

Availability of the matplotlib package is assumed for plotting results.

AnalogOutPlayCustomWaveform.py

Show a custom waveform on CH1 of the AnalogOut instrument. The custom waveform can be given as a file containing human-readable numbers. If no filename is specified, a default custom waveform is generated.

Hook up the first analog output channel of your device to an oscilloscope to see the custom waveform.

AnalogOutContinuousPlay.py

Show either a circle or a polygon on CH1 (X) and CH2 (Y) of the AnalogOut instrument.

To properly appreciate this example, hook up these two channels to a second oscilloscope that is configured for X-vs-Y display mode.

AnalogOutSpinningGlobe.py

Show a spinning globe on CH1 (X) and CH2 (Y) of the AnalogOut instrument.

To properly appreciate this example, hook up these two channels to a second oscilloscope that is configured for X-vs-Y display mode.

AnalogIO functionality example

AnalogIO.py

This example enumerates all AnalogIO channels and their nodes. After that, it continuously reports the quantities associated with the “USB Monitor” channel.

Analog Impedance examples

Not yet available.

DigitalIn instrument examples

Not yet available.

DigitalOut instrument examples

DigitalOutShowStatusDuringPulsePlayback.py

Demonstrate the behavior of the DigitalOut instrument status during Pulse playback.

DigitalIO functionality example

DigitalIO.py

Demonstrate static monitoring and control of the digital input and output pins.

Protocol examples

ProtocolUART.py

An example showing loopback transmission and reception using the UART protocol.

ProtocolCAN.py

An example showing loopback transmission and reception of messages using the CAN bus protocol.

ProtocolSPI.py

This example demonstrates continuous readout of an ADXL345 triple-axis accelerometer using the SPI protocol.

The program expects to find the accelerometer IC attached to the proper pins. See the source code for a description on how to hook up an ADXL345 to make this work.

ProtocolI2C.py

This example demonstrates continuous readout of an ADXL345 triple-axis accelerometer using the I²C protocol.

The program expects to find the accelerometer IC attached to the proper pins. See the source code for a description on how to hook up an ADXL345 to make this work.