Documentation

Access Your Hardware

Connect to Your Hardware

Perhaps the most effective way to get started with Data Acquisition Toolbox™ software is to connect to your hardware, and input or output data.

Each example illustrates a typical data acquisition session. The data acquisition session comprises all the steps you are likely to take when acquiring or outputting data using a supported hardware device. You should keep these steps in mind when constructing your own data acquisition applications.

Note that the analog input and analog output examples use a sound card, while the digital I/O example uses a National Instruments® board. If you are using a different supported hardware device, you should modify the adaptor name and the device ID supplied to the creation function as needed.

If you want detailed information about any functions that are used, refer to the list of functions. If you want detailed information about any properties that are used, refer to the list of properties.

Acquire Audio Data

If you have a sound card installed, you can run the following example, which acquires 1 second of data an audio input hardware channels, and then plots the acquired data.

You should modify this example to suit your specific application needs.

  1. Create a session object — Create the session object s for a sound card.

    s = daq.createSession('directsound');
  2. Identify the system devices and their IDs for audio input and output.

    daq.getDevices
    
    Data acquisition devices:
    
    index   Vendor    Device ID                     Description                     
    ----- ----------- --------- ----------------------------------------------------
    1     directsound Audio0    DirectSound Primary Sound Capture Driver
    2     directsound Audio1    DirectSound HP 4120 Microphone (HP 4120)
    3     directsound Audio2    DirectSound Primary Sound Driver
    4     directsound Audio3    DirectSound Speakers (Realtek High Definition Audio)
    5     directsound Audio4    DirectSound HP 4120 (HP 4120)
  3. Add channel — Add an analog input channel to s, using the microphone device.

    addAudioInputChannel(s,'audio1','1','audio');

    To display a summary of the session, type:

    s
    
    Data acquisition session using DirectSound hardware:
       Will run for 1 second (44100 scans) at 44100 scans/second.
       Number of channels: 1
          index Type Device Channel MeasurementType     Range     Name
          ----- ---- ------ ------- --------------- ------------- ----
          1     audi Audio1 1       Audio           -1.0 to +1.0
       
  4. Acquire data — Start the acquisition. When all the data is acquired, it is assigned to data.

    data = startForeground(s);
    plot(data)
  5. Clean up — When you no longer need s, you should remove it from memory.

    delete(s)
    clear s

Generate Audio Data

If you have a sound card installed, you can run the following example, which outputs 1 second of data to two analog output hardware channels.

You should modify this example to suit your specific application needs.

  1. Create a session object — Create the session object s for a sound card.

    s = daq.createSession('directsound');
  2. Add channel — Add an audio output channel to session s. This example uses the device ID audio4 for the speakers from the previous example.

    addAudioOutputChannel(s,'audio4','1','audio');

    To display a summary of the session, type:

    s
    
    Data acquisition session using DirectSound hardware:
       No data queued.  Will run at 44100 scans/second.
       Number of channels: 1
          index Type Device Channel MeasurementType     Range     Name
          ----- ---- ------ ------- --------------- ------------- ----
          1     audo Audio4 1       Audio           -1.0 to +1.0   
  3. Output data — Create 1 second of output data, and queue the data for output from the device. You must queue one column of data for each hardware channel.

    data = sin(linspace(0,2*pi*500,44100)');
    queueOutputData(s,data)

    Start the output. When all the data is output, s stops generating.

    startForeground(s)
  4. Clean up — When you no longer need s, you should remove it from memory and from the MATLAB® workspace.

    delete(s)
    clear s

Acquire and Generate Digital Data

If you have a supported National Instruments board with at least two digital I/O ports, you can run the following example, which writes and reads digital values.

You should modify this example to suit your specific application needs. Adjust the example if the ports on your device do not support the input/output directions specified here.

  1. Create a session object — Create the data acquisition session s for a National Instruments board with hardware device ID cDAQ1Mod1.

    s = daq.createSession('ni');
  2. Add digital input channels — Add two lines from port 0 to s, and configure them for input.

    addDigitalChannel(s,'cDAQ1Mod1','Port0/Line0:1','InputOnly');
  3. Add digital output lines — Add two lines from port 0 to s, and configure them for output.

    addDigitalChannel(s,'cDAQ1Mod1','Port0/Line2:3','OutputOnly');

    To display a summary of the session, type:

    s
    
    Data acquisition session using National Instruments hardware:
       No data queued.  Will run at 1000 scans/second.
       Number of channels: 4
          index Type  Device     Channel   MeasurementType Range Name
          ----- ---- --------- ----------- --------------- ----- ----
          1     dio  cDAQ1Mod1 port0/line0 InputOnly       n/a
          2     dio  cDAQ1Mod1 port0/line1 InputOnly       n/a
          3     dio  cDAQ1Mod1 port0/line2 OutputOnly      n/a
          4     dio  cDAQ1Mod1 port0/line3 OutputOnly      n/a
    
  4. Add clock and trigger — To synchronize operations, add a clock and trigger connection.

    addClockConnection(s,'External','cDAQ1/PFI0','ScanClock')
    addTriggerConnection(s,'External','cDAQ1/PFI1','StartTrigger')
    

      Note   Digital line values are usually not transferred at a specific rate. Although some specialized boards support clocked I/O.

  5. Queue output data and start device — Create an array of output values, and write the values to the digital I/O subsystem. Note that reading and writing digital I/O line values typically does not require that you configure specific property values.

    queueOutputData(s,round(rand(4000,2)));
    gval = startForeground(s);
    
  6. Display input — To read only the input lines, type:

    gval
    
  7. Clean up — When you no longer need s, you should remove it from memory and from the MATLAB workspace.

    delete(s)
    clear s
Was this topic helpful?