Documentation Center

  • Trial Software
  • Product Updates

Generate Analog Output Signals

Use addAnalogOutputChannel

Use the addAnalogOutputChannel method to add a channel that generates analog signals from a National Instruments® device, including CompactDAQ chassis. You can generate data in the foreground or in the background. See Session Architecture for more information.

Generate Signals in the Foreground

This example shows how to generate data using an NI 9263 device with ID cDAQ1Mod2.

Create a session object and save it to the variable, s:

s = daq.createSession('ni');

Change the scan rate of the session object to generate 10,000 scans per second:

s.Rate = 10000

s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (10000 scans) at 10000 scans/second.
   Operation starts immediately.
      No channels have been added.   

Add an analog output 'Voltage' channel:

addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage')

ans = 

Data acquisition session using National Instruments hardware:
   No data queued.  Will run at 1000 scans/second.      Number of channels: 1
      index Type  Device   Channel MeasurementType      Range       Name
      ----- ---- --------- ------- --------------- ---------------- ----
      1     ao   cDAQ1Mod2 ao0     Voltage         -10 to +10 Volts

Specify the channel ID on NI devices using a terminal name, like 'ao1', or a numeric equivalent like 1.

Create the data to output:

outputData = linspace(-1, 1, 2200)';

Queue the data:

queueOutputData(s,outputData);

The duration changes to 0.22 seconds based on the length of the queued data and the specified scan rate. When the session contains output channels, duration and number of scans become read-only properties of the session. The number of scans in a session is determined by the amount of data queued and the duration is determined by .

Display the session object to see this change:

s

s = 

Data acquisition session using National Instruments hardware:
   Will run for 2200 scans (0.22 seconds) at 10000 scans/second.
   .All devices synchronized using cDAQ1 CompactDAQ chassis backplane. (Details)
      Number of channels: 1
      index Type  Device   Channel InputType      Range       Name
      ----- ---- --------- ------- --------- ---------------- ----
      1     ao   cDAQ1Mod2 ao0     n/a       -10 to +10 Volts   

Generate the data. MATLAB® returns once the generation is complete.

startForeground(s);

Generate Signals Using Multiple Channels

This example shows how to generate data from multiple channels and multiple devices using the session-based interface. This example generates data using channels from an NI 9263 voltage device with ID cDAQ1Mod2 and an NI 9265 current device with ID cDAQ1Mod8.

Create an NI session object and add two analog output 'Voltage' channels to cDAQ1Mod2:

s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2', 2:3, 'Voltage');

Step 2. Add one output 'Current' channel on cDAQ1Mod8:

addAnalogOutputChannel(s,'cDAQ1Mod8', 'ao2', 'Current')
ans = 

Data acquisition session using National Instruments hardware:
   No data queued.  Will run at 1000 scans/second.
   All devices synchronized using cDAQ1 CompactDAQ chassis backplane. (Details)
      Number of channels: 3
      index Type  Device   Channel InputType      Range       Name
      ----- ---- --------- ------- --------- ---------------- ----
      1     ao   cDAQ1Mod2 ao2     n/a       -10 to +10 Volts
      2     ao   cDAQ1Mod2 ao3     n/a       -10 to +10 Volts
      3     ao   cDAQ1Mod8 ao2     n/a       0 to +0.020 A    

Specify the channel ID on NI devices using a terminal name, like ao1, or a numeric equivalent like 1.

Create one set of data to output for each added channel:

outputData(:,1) = linspace(-1, 1, 1000);
outputData(:,2) = linspace(-2, 2, 1000)';
outputData(:,3) = linspace(0, 0.02, 1000)';

Queue the output data:

queueOutputData(s,outputData);

Step 5. Generate the data:

startForeground(s);

Generate Signals in the Background

This example shows how to generate signals in the background.

Create an NI session object and add an analog output 'Voltage' channel to cDAQ1Mod2:

s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2', 'ao0', 'Voltage');

Specify the channel ID on NI devices using a terminal name, like ao1, or a numeric equivalent like 1.

Create the data to output:

outputData = (linspace(-1, 1, 1000)');

Queue the output data:

queueOutputData(s,outputData);

Generate the signal:

startBackground(s);

You can execute other MATLAB commands while the generation is in progress. In this example, issue a pause(), which causes the MATLAB command line to wait for you to press any key.

pause();

Generate Signals in the Background Continuously

This example shows how to continuously generate signals. A continuous background generation depends on events and listeners to allow your code to enable continuous queuing of data and to react to any errors as they occur. For details, see Events and Listeners — Concepts in the MATLAB Object-Oriented Programming documentation. In this example, you generate from an NI 9263 device with ID cDAQ1Mod2 using a listener on the DataRequired event.

Listeners execute a callback function when notified that the event has occurred. Use Session.addlistener to create the listener object that executes your callback function.

Create an NI session object and add an analog output 'Voltage' channel on cDAQ1Mod2:

s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2', 'ao0', 'Voltage');

Specify the channel ID on NI devices using a terminal name, like 'ao1', or a numeric equivalent like 1.

Create the data to output and queue the output data.

queueOutputData(linspace(s,1, 10, 1000)');

Add the listener to the DataRequired event and assign it to the variable lh:

lh = addlistener(s,'DataRequired',@queueMoreData);

Step 4. Create a simple callback function to generate the data and save it as queueMoreData.m in your working folder:

  function queueMoreData(src,event)
        queueOutputData(linspace(s,1, 10, 1000)');
  end

Generate the signal:

startBackground(s);

You can execute other MATLAB commands while the generation is in progress. In this example, issue a pause(), which causes the MATLAB command line to wait for you to press any key.

pause();

Delete the listener:

delete (lh)

Getting Started Generating Data with Digilent® Analog Discovery™

This example shows how to generate analog output voltage data (at a rate of 300kHz). The output voltage-range of the outgoing signal is -5.0 to +5.0 volts. You will use the session-based interface with Digilent Analog Discovery hardware.

Create a session with a Digilent device

Discover Digilent devices connected to your system using daq.getDevices and create a session using the listed Digilent device.

s = daq.createSession('digilent')
s = 

Data acquisition session using Digilent Inc. hardware:
   Will run for 1 second (10000 scans) at 10000 scans/second.
   No channels have been added.
   

Add an analog output channel

Add an analog output channel with device ID AD1 and channel ID 1. Set the measurement type to Voltage.

ch = s.addAnalogOutputChannel('AD1', 1, 'Voltage')
ch = 

Data acquisition analog output voltage channel '1' on device 'AD1':

 TerminalConfig: SingleEnded
          Range: -5.0 to +5.0 Volts
           Name: ''
             ID: '1'
         Device: [1x1 daq.di.DeviceInfo]
MeasurementType: 'Voltage'


Generate a single sample

Generate a single scan on-demand.

outVal = 2;
s.outputSingleScan(outVal);

Set session and channel properties

Set the generation rate to 300kHz.

rate = 300e3;
s.Rate = rate;

Define the output waveform

Generate a 10 Hz sine-wave for half a second. The length of the output waveform and the specified output rate define the duration of the waveform.

f = 10;
duration = 0.5;
t = (1:(duration*rate))/rate;
output = sin(2*pi*f*t)';

Generate continuous data

Queue some data and start a clocked foreground generation.

s.queueOutputData(output);
s.startForeground;
Was this topic helpful?