Generate Signals on NI devices that Output Voltage

This example shows how to generate data using a National Instruments device available to MATLAB® using the Session based interface.

Discover Devices that can Output Voltage

To discover a device that supports analog output voltage subsystems, click the name of the device in the list in the Command window, or access the device in the array returned by daq.getDevices command. This example uses a National Instruments CompactDAQ device NI 9263 with ID 'cDAQ1Mod2' representing the module in slot 2 of Chassis 'cDAQ1'.

devices = daq.getDevices
devices = 

Data acquisition devices:

index Vendor Device ID           Description           
----- ------ --------- --------------------------------
1     ni     cDAQ1Mod1 National Instruments NI 9205
2     ni     cDAQ1Mod2 National Instruments NI 9263
3     ni     cDAQ1Mod3 National Instruments NI 9234
4     ni     cDAQ1Mod4 National Instruments NI 9201
5     ni     cDAQ1Mod5 National Instruments NI 9402
6     ni     cDAQ1Mod6 National Instruments NI 9213
7     ni     cDAQ1Mod7 National Instruments NI 9219
8     ni     cDAQ1Mod8 National Instruments NI 9265
9     ni     Dev1      National Instruments PCIe-6363
10    ni     Dev2      National Instruments NI ELVIS II

ans = 

ni: National Instruments NI 9263 (Device ID: 'cDAQ1Mod2')
   Analog output subsystem supports:
      -10 to +10 Volts range
      Rates from 0.6 to 100000.0 scans/sec
      4 channels ('ao0','ao1','ao2','ao3')
      'Voltage' measurement type
This module is in slot 2 of the 'cDAQ-9178' chassis with the name 'cDAQ1'.

Create a Session

Use the daq.createSession function to create a session associated with a vendor. The session contains information describing the hardware, scan rate, duration, and other properties associated with the generation. When you create a session, assign it to a variable.

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

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

Add Analog Output Channels

Use the addAnalogOutputChannel function to add two analog output channels from this device to the session.


Set the Session Rate

By default the session is configured for 1000 scans/second. Change the scan rate to acquire at 8000 scans/second.

s.Rate = 8000
s = 

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

Generate a Single Scan

Use the outputSingleScan function to generate a single scan. The data is a 1-by-N matrix where N corresponds to the number of output channels. Here you output 2V on each channel.

outputSingleValue = 2;
outputSingleScan(s,[outputSingleValue outputSingleValue]);

Queue the Data

Use the queueOutputData function to generate multiple scans. Data should be a M-by-N matrix where M is the number of scans you want and N is the number of channels in the session. Generate 2 test signals (a 1Hz sine wave and a 1Hz ramp) and output them on to the channels in this session. The plot depicts the data generated by both channels together. (Check if the device you are using supports simultaneous sampling.)

outputSignal1 = sin(linspace(0,pi*2,s.Rate)');
outputSignal2 = linspace(-1,1,s.Rate)';
hold on;
legend('Analog Output 0', 'Analog Output 1');
queueOutputData(s,[outputSignal1 outputSignal2]);

Start the Session in Foreground

Use the startForeground function to start the analog output operation and block MATLAB execution until all data is generated.