Contents

Acquire Analog Input Data

Using addAnalogInputChannel

Use the addAnalogInputChannel method to add a channel that acquires analog signals from a device on a National Instruments®. You can acquire data in the foreground or the background. See Session Architecture for more information.

Acquire Data in the Foreground

This example shows how to acquire voltage data from an NI 9205 device with ID cDAQ1Mod1.

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

s = daq.createSession('ni')

s = 

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

By default, the acquisition is configured to run for a duration of 1 second to acquire 1000 scans, at the rate of 1000 scans per second.

Change the duration of the acquisition to 2 seconds:

s.DurationInSeconds = 2.0

s = 

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

The acquisition now runs for 2 seconds acquiring 2000 scans at the default rate.

Add an analog input 'Voltage' channel named 'ai0':

addAnalogInputChannel(s,'cDAQ1Mod1','ai0','Voltage')

ans = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   Operation starts immediately.
      Number of channels: 1
      index Type  Device   Channel InputType      Range       Name
      ----- ---- --------- ------- --------- ---------------- ----
      1     ai   cDAQ1Mod1 ai0     Diff      -10 to +10 Volts

For NI devices, use either a terminal name, like 'ai2', or a numeric equivalent like 2 for the channel ID.

Acquire the data and store it in the variable, data and plot it:

data = startForeground(s);
plot (data)

Change the number of scans to 4096.

s.NumberOfScans = 4096

s = 

Data acquisition session using National Instruments hardware:
   Will run for 4096 scans (4.096 seconds) at 1000 scans/second.
   Operation starts immediately.
      Number of channels: 1
      index Type  Device   Channel InputType      Range       Name
      ----- ---- --------- ------- --------- ---------------- ----
      1     ai   cDAQ1Mod1 ai0     Diff      -10 to +10 Volts

Changing the number of scans changed the duration of the acquisition to 4.096 seconds at the default rate of 1000 scans per second.

Acquire the data and store it in the variable, data and plot it:

data = startForeground(s);
plot (data)

Acquire Data from Multiple Channels

This example shows how to acquire data from multiple channels, and from multiple devices on the same chassis. In this example, you acquire voltage data from an NI 9201 device with ID cDAQ1Mod4 and an NI 9205 device with ID cDAQ1Mod1.

Create a session object and add two analog input 'Voltage' channels on cDAQ1Mod1 with channel ID 0 and 1:

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1', 0:1, 'Voltage');

Add an additional channel on a separate device, cDAQ1Mod6 with channel ID 0. For NI devices, use either a terminal name, like ai0, or a numeric equivalent like 0. Store this channel in the variable ch.

ch = addAnalogInputChannel(s,'cDAQ1Mod6', 'ai0', 'Voltage')

ch = 

Data acquisition analog input channel 'ai0' on device 'cDAQ1Mod16':

     Coupling: DC
    InputType: Differential
        Range: -10 to +10 Volts
         Name: empty
           ID: 'ai0'
       Device: [1x1 daq.ni.CompactDAQModule]
ADCTimingMode: ''

View the session object to see the three channels:

s

s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   Number of channels: 3
      index Type  Device   Channel   MeasurementType        Range       Name
      ----- ---- --------- ------- ------------------- ---------------- ----
      1     ai   cDAQ1Mod1 ai0     Voltage (SingleEnd) -10 to +10 Volts
      2     ai   cDAQ1Mod1 ai1     Voltage (SingleEnd) -10 to +10 Volts
      3     ai   cDAQ1Mod6 ai0     Voltage (Diff)      -10 to +10 Volts

Acquire the data and store it in the variable, data and plot it:

data = startForeground(s);
plot(data)

Change the properties of the channel 'ai0' on cDAQ1Mod6 and display ch:

ch.InputType ='SingleEnded';
ch.Name = 'Velocity sensor';
ch

ch = 

Data acquisition analog input channel 'ai0' on device 'cDAQ1Mod6':

     Coupling: DC
    InputType: SingleEnded
        Range: -10 to +10 Volts
         Name: 'Velocity sensor'
           ID: 'ai0'
       Device: [1x1 daq.ni.CompactDAQModule]
ADCTimingMode: empty

Acquire the data and store it in the variable, data and plot it:

data = startForeground(s);
plot(data)

Acquire Data in the Background

This example shows how to acquire data in the background using events and listeners.

A background acquisition depends on events and listeners to allow your code to access data as the hardware acquires it and to react to any errors as they occur. For more information, see Events and Listeners — Concepts in the MATLAB Object-Oriented Programming documentation. Use events to acquire data in the background. In this example, you acquire data from an NI 9205 device with ID cDAQ1Mod1 using a listener and a DataAvailable event.

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

Create an NI session object and an analog input 'Voltage' channel on cDAQ1Mod1:

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1', 'ai0', 'Voltage');

Add the listener for the DataAvailable event and assign it to the variable lh:

lh = addlistener(s,'DataAvailable', @plotData); 

For more information on events, see Events and Listeners — Concepts in the MATLAB Object-Oriented Programming documentation.

Create a simple callback function to plot the acquired data and save it as plotData.m in your working directory:

 function plotData(src,event)
     plot(event.TimeStamps, event.Data)
 end

Here, src is the session object for the listener and event is a daq.DataAvailableInfo object containing the data and associated timing information.

Acquire the data and see the plot update while MATLAB® is running:

startBackground(s);

When the operation is complete, delete the listener:

delete (lh)

Acquire Data from an Accelerometer

This example shows how to acquire and display data from an accelerometer attached to a vehicle driven under uneven road conditions.

Discover Devices that Support Accelerometers

To discover a device that supports Accelerometers, 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 National Instruments® CompactDAQ Chassis NI cDAQ-9178 and module NI 9234 with ID cDAQ1Mod3.

devices = daq.getDevices
devices(3)
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 9234 (Device ID: 'cDAQ1Mod3')
   Analog input subsystem supports:
      -5.0 to +5.0 Volts range
      Rates from 1000.0 to 51200.0 scans/sec
      4 channels ('ai0','ai1','ai2','ai3')
      'Voltage','Accelerometer','Microphone','IEPE' measurement types
   
This module is in slot 3 of the 'cDAQ-9178' chassis with the name 'cDAQ1'.


Add an Accelerometer Channel

Create a session, and add an analog input channel with the Accelerometer measurement type.

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod3', 0, 'Accelerometer');

Set Session Rate and Duration

Change the scan rate to 4000 scans per second and the duration to 30 seconds.

s.Rate = 4000;
s.DurationInSeconds = 30;
s
s = 

Data acquisition session using National Instruments hardware:
   Will run for 30 seconds (120000 scans) at 4000 scans/second.
   Number of channels: 1
      index Type  Device   Channel   MeasurementType          Range        Name
      ----- ---- --------- ------- -------------------- ------------------ ----
      1     ai   cDAQ1Mod3 ai0     Accelerometer (Diff) -5.0 to +5.0 Volts
   

Set Sensitivity

You must set the Sensitivity value to the value specified in the accelerometer's data sheet. This example uses a ceramic shear accelerometer model 352C22 from PCB Piezotronics is used with 9.22 mV per Gravity.

s.Channels(1).Sensitivity = 0.00922;
s.Channels(1)
ans = 

Data acquisition analog input accelerometer channel 'ai0' on device 'cDAQ1Mod3':

      Sensitivity: 0.00922
ExcitationCurrent: 0.001
 ExcitationSource: None
         Coupling: DC
   TerminalConfig: Differential
            Range: -5.0 to +5.0 Volts
             Name: ''
               ID: 'ai0'
           Device: [1x1 daq.ni.CompactDAQModule]
  MeasurementType: 'Accelerometer'


Start Acquisition and Plot the Data

Use startForeground to acquire and plot the data.

[data,time] = s.startForeground;
plot(time,data)
xlabel('Time (Secs)');
ylabel('Acceleration (Gravities)');

Acquire Bridge Measurements

This example shows how to acquire data from an NI USB-9219 device with ID 'cDAQ1Mod7', and plot the acquired data:

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

s = daq.createSession('ni');

Add an analog input channel with the 'Bridge' measurement type and save it to the variable ch:

ch = addAnalogInputChannel(s,'cDAQ1Mod7', 'ai1', 'Bridge');

You might see this warning:

Warning: The Rate property was reduced to 2 due to the default ADCTimingMode of this device,
which is 'HighResolution'.
To increase rate, change ADCTimingMode on this channel to 'HighSpeed'. 

To allow a higher acquisition rate, change the channel ADCTimingMode to 'HighSpeed':

ch.ADCTimingMode = 'HighSpeed'

You might see this warning:

Warning: This property must be the same for all channels on this device.  All channels
associated with this device were updated.

Change the acquisition rate to 10 scans per second.

s.Rate = 10
s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (10 scans) at 10 scans/second.
   Number of channels: 1
      index Type  Device   Channel MeasurementType              Range             Name
      ----- ---- --------- ------- ---------------- ----------------------------- ----
      1     ai   cDAQ1Mod7 ai1     Bridge (Unknown) -0.025 to +0.025 VoltsPerVolt

Set BridgeMode to 'Full', which uses all four resistors in the device to acquire the voltage values:

ch.BridgeMode = 'Full'
ch = 

Data acquisition analog input channel 'ai1' on device 'cDAQ1Mod7':

             BridgeMode: Full
       ExcitationSource: Internal
      ExcitationVoltage: 2.5
NominalBridgeResistance: 'Unknown'
                  Range: -0.063 to +0.063 VoltsPerVolt
                   Name: empty
                     ID: 'ai1'
                 Device: [1x1 daq.ni.CompactDAQModule]
        MeasurementType: 'Bridge'
          ADCTimingMode: HighSpeed

Set the resistance of the bridge device to 350 ohms:

ch.NominalBridgeResistance = 350
ch = 

Data acquisition analog input channel 'ai1' on device 'cDAQ1Mod7':

             BridgeMode: Full
       ExcitationSource: Internal
      ExcitationVoltage: 2.5
NominalBridgeResistance: 350
                  Range: -0.063 to +0.063 VoltsPerVolt
                   Name: empty
                     ID: 'ai1'
                 Device: [1x1 daq.ni.CompactDAQModule]
        MeasurementType: 'Bridge'
          ADCTimingMode: HighSpeed

Save the acquired data to a variable and start the acquisition:

data = startForeground(s);

Plot the acquired data:

plot(data)

Acquire Sound Pressure Data

This example shows how to acquire sound data using NI cDAQ-9178 chassis with device NI 9234 on slot 3 with ID cDAQ1Mod3.

Create a session, and add an analog input channel with Microphone measurement type:

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod3', 0, 'Microphone');

Set the channels sensitivity to 0.037 v/pa.

s.Channels.Sensitivity = 0.037;

Examine the channel properties:

s.Channels(1)
ans = 

Data acquisition analog input microphone channel 'ai0' on device 'cDAQ1Mod3':

          Sensitivity: 0.037
MaxSoundPressureLevel: 136
    ExcitationCurrent: 0.002
     ExcitationSource: Internal
             Coupling: AC
       TerminalConfig: PseudoDifferential
                Range: -135 to +135 Pascals
                 Name: ''
                   ID: 'ai0'
               Device: [1x1 daq.ni.CompactDAQModule]
      MeasurementType: 'Microphone'
        ADCTimingMode: ''

Change the maximum sound pressure level to 100db and examine channel properties.

s.Channels.MaxSoundPressureLevel=100;
s.Channels(1)
ans = 

Data acquisition analog input microphone channel 'ai0' on device 'cDAQ1Mod3':

          Sensitivity: 0.037
MaxSoundPressureLevel: 100
    ExcitationCurrent: 0.002
     ExcitationSource: Internal
             Coupling: AC
       TerminalConfig: PseudoDifferential
                Range: -135 to +135 Pascals
                 Name: ''
                   ID: 'ai0'
               Device: [1x1 daq.ni.CompactDAQModule]
      MeasurementType: 'Microphone'
        ADCTimingMode: ''

Set acquisition session duration to 4 seconds.

s.DurationInSeconds = 4;

Acquire the data against time and save it in a variable.

[data,time] = startForeground(s);

Plot the data.

plot(time, data)

Acquire IEPE Data

This example shows how to acquire IEPE data using NI cDAQ-9178 chassis with device NI 9234 on slot 3 with ID cDAQ1Mod3.

Create a session, and add an analog input channel with IEPE measurement type.

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod3', 0, 'IEPE');

Change the channel's ExcitationCurrent to .004 volts.

s.Channels(1).ExcitationCurrent = .004;

Acquire the data against time and save it in a variable.

[data,time] = startForeground(s);

Plot the data.

plot(time, data)

Getting Started Acquiring Data with Digilent® Analog Discovery™

This example shows how to acquire analog input voltage data (at a sampling rate of 300kHz). The dynamic range of the incoming signal is -2.5 to 2.5 volts. You will use the session-based interface with the 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 input channel

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

ch = addAnalogInputChannel(s,'AD1', 1, 'Voltage')
ch = 

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

       Coupling: DC
 TerminalConfig: Differential
          Range: -25 to +25 Volts
           Name: ''
             ID: '1'
         Device: [1x1 daq.di.DeviceInfo]
MeasurementType: 'Voltage'


Set session and channel properties

Set the sampling rate to 300kHz and the channel range to -2.5 to 2.5 volts. Set the duration to 0.5 seconds.

s.Rate = 300e3;
s.Channels.Range = [-2.5 2.5];
s.DurationInSeconds = 0.5
s = 

Data acquisition session using Digilent Inc. hardware:
   Will run for 0.5 seconds (150000 scans) at 300000 scans/second.
   Number of channels: 1
      index Type Device Channel MeasurementType       Range        Name
      ----- ---- ------ ------- --------------- ------------------ ----
      1     ai   AD1    1       Voltage (Diff)  -2.5 to +2.5 Volts
   

Acquire a single sample

Acquire a single scan on-demand, measuring the data and trigger time.

[singleReading, triggerTime] = s.inputSingleScan
singleReading =

   -0.0104


triggerTime =

   7.3532e+05

Acquire timestamped data

Start a clocked foreground acquisition.

[data, timestamps, triggerTime] = s.startForeground;

Display the results

plot(timestamps, data);
xlabel('Time (seconds)')
ylabel('Voltage (Volts)')
title(['Clocked Data Triggered on: ' datestr(triggerTime)])

Was this topic helpful?