Main Content

Acquiring and Generating Data at the Same Time with Digilent Analog Discovery

This example shows you how to synchronously generate and acquire voltage data at a rate of 300 kHz.

Discovery Digilent Device

Discover Digilent devices connected to your system using daqlist

daqlist("digilent")
ans =

  1×4 table

    DeviceID                     Description                            Model                  DeviceInfo       
    ________    _____________________________________________    ____________________    _______________________

     "AD1"      "Digilent Inc. Analog Discovery 2 Kit Rev. C"    "Analog Discovery 2"    [1×1 daq.di.DeviceInfo]

Create a DataAcquisition for a Digilent Device

Discover Digilent devices connected to your system using daqlist

dq = daq("digilent")
dq = 

DataAcquisition using Digilent Inc. hardware:

                     Running: 0
                        Rate: 10000
           NumScansAvailable: 0
            NumScansAcquired: 0
              NumScansQueued: 0
    NumScansOutputByHardware: 0
                   RateLimit: []

Show channels
Show properties and methods

Add an Analog Output Channel

Add an analog output channel using the listed Digilent device with ID AD1, channel ID 1, and measurement type Voltage.

addoutput(dq, "AD1", "1", "Voltage");
addoutput(dq, "AD1", "2", "Voltage");
ch_out = dq.Channels(1:2);
ch_out(1).Name = "AD1_1_out";
ch_out(2).Name = "AD1_2_out"
ch_out = 

    Index    Type    Device    Channel      Measurement Type              Range               Name    
    _____    ____    ______    _______    _____________________    ____________________    ___________

      1      "ao"    "AD1"       "1"      "Voltage (SingleEnd)"    "-5.0 to +5.0 Volts"    "AD1_1_out"
      2      "ao"    "AD1"       "2"      "Voltage (SingleEnd)"    "-5.0 to +5.0 Volts"    "AD1_2_out"

Add an Analog Input Channel

Add an analog input channel with the same device and measurement type Voltage.

addinput(dq, "AD1", "1", "Voltage");
addinput(dq, "AD1", "2", "Voltage");
ch_in = dq.Channels(3:4);
ch_in(1).Name = "AD1_1_in";
ch_in(2).Name = "AD1_2_in"
ch_in = 

    Index    Type    Device    Channel    Measurement Type          Range              Name   
    _____    ____    ______    _______    ________________    __________________    __________

      1      "ai"    "AD1"       "1"      "Voltage (Diff)"    "-25 to +25 Volts"    "AD1_1_in"
      2      "ai"    "AD1"       "2"      "Voltage (Diff)"    "-25 to +25 Volts"    "AD1_2_in"

Set DataAcquisition Properties and Define an Output Waveform

Set the generation rate to 300 kHz.

rate = 300e3;
dq.Rate = rate;

% Specify a 10 Hz sine wave for 1 second.
f = 10;
totalduration = 1;
n = totalduration * rate;
t = (1:n)/rate;
output = sin(2*pi*f*t)';

Generate and Acquire Data

Generate a sine wave with amplitude 1 V on channel 1 and amplitude 2 V on channel 2 and acquire timestamped data at the same rate.

[data, startTime] = readwrite(dq, [output 2*output]);

Plot Acquired Data

plot(data.Time, data.AD1_1_in, data.Time, data.AD1_2_in);
xlabel('Time (s)');
ylabel('Voltage (V)');
title(['Clocked Data Triggered at: ' datestr(startTime)])