Main Content

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 in this topic illustrates a typical data acquisition workflow. A workflow 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 vendor name and the device ID as needed.

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

Note

If you are connecting to a CompactDAQ devices or a counter/timer device, see Counter and Timer Input and Output.

Examine Your Hardware Resources

You can examine the data acquisition hardware resources visible to the toolbox with the daqvendorlist and daqlist functions. Hardware resources include installed boards, hardware drivers, and adaptors.

For example, to view the available audio devices, type:

daqlist("directsound")

To view available National Instruments devices, type:

daqlist("ni")

To view all available devices, type:

daqlist

To view the operational status of hardware vendors, type:

daqvendorlist

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 DataAcquisition object — Create the DataAcquisition object d for a sound card.

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

    daqlist("directsound")
      7×4 table
    
        DeviceID                           Description                                                Model                                 DeviceInfo        
        ________    __________________________________________________________    ______________________________________________    __________________________
    
        "Audio0"    "DirectSound Primary Sound Capture Driver"                    "Primary Sound Capture Driver"                    [1×1 daq.audio.DeviceInfo]
        "Audio1"    "DirectSound Headset Microphone (Plantronics BT600)"          "Headset Microphone (Plantronics BT600)"          [1×1 daq.audio.DeviceInfo]
        "Audio2"    "DirectSound Primary Sound Driver"                            "Primary Sound Driver"                            [1×1 daq.audio.DeviceInfo]
        "Audio3"    "DirectSound Headset Earphone (Plantronics BT600)"            "Headset Earphone (Plantronics BT600)"            [1×1 daq.audio.DeviceInfo]
        "Audio4"    "DirectSound Speakers (2- Realtek High Definition Audio)"     "Speakers (2- Realtek High Definition Audio)"     [1×1 daq.audio.DeviceInfo]
        "Audio5"    "DirectSound Speakers (Realtek High Definition Audio)"        "Speakers (Realtek High Definition Audio)"        [1×1 daq.audio.DeviceInfo]
        "Audio6"    "DirectSound LEN LT2452pwC (NVIDIA High Definition Audio)"    "LEN LT2452pwC (NVIDIA High Definition Audio)"    [1×1 daq.audio.DeviceInfo]
    
  3. Add channel — Add an audio input channel to d for the microphone device.

    addinput(d,"Audio1","1","Audio");

    To display a summary of the DataAcquisition channels, type:

    d.Channels
    ans = 
    
        Index     Type      Device     Channel    Measurement Type        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 = read(d,seconds(1));
    plot(data)
  5. Clean up — When you no longer need d, you should remove it from memory.

    delete(d)
    clear d

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 DataAcquisition object — Create the DataAcquisition object d for a sound card.

    d = daq('directsound');
  2. Add channel — Add an audio output channel to DataAcquisition d. This example uses the device ID Audio4 for the speakers.

    addoutput(d,"Audio4',"1","Audio");

    To display a summary of the DataAcquisition and its channels, type:

    d,d.Channels
  3. Output data — Create 1 second of output data, and queue the data for output from the device. You queue a matrix with one column of data for each hardware channel.

    data = sin(linspace(0,2*pi*500,44100)');
    preload(d,data)

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

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

    delete(d)
    clear d

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 DataAcquisition object — Create the DataAcquisition interface d for a National Instruments board with hardware device ID cDAQ1Mod1.

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

    addinput(d,"cDAQ1Mod1","Port0/Line0:1","Digital");
  3. Add digital output lines — Add two lines from port 0 to s, and configure them for output.

    addoutput(d,"cDAQ1Mod1","Port0/Line2:3","Digital");

    To display a summary of the channels, type:

    d.Channels
    ans = 
    
        Index    Type     Device            Channel       Measurement Type    Range           Name       
        _____    _____    ___________    _____________    ________________    _____    __________________
    
          1      "dio"    "cDAQ1Mod1"    "port0/line0"      "InputOnly"       "n/a"    "Dev3_port0/line0"
          2      "dio"    "cDAQ1Mod1"    "port0/line1"      "InputOnly"       "n/a"    "Dev3_port0/line1"
          3      "dio"    "cDAQ1Mod1"    "port0/line2"      "OutputOnly"      "n/a"    "Dev3_port0/line2"
          4      "dio"    "cDAQ1Mod1"    "port0/line3"      "OutputOnly"      "n/a"    "Dev3_port0/line3"
  4. Add clock and trigger — To synchronize operations, add a clock and trigger connection.

    addclock(d,"ScanClock","External","cDAQ1/PFI0");
    addtrigger(d,"Digital","StartTrigger","External","cDAQ1/PFI1");
    d.Clocks,d.DigitalTriggers
    ans = 
    
      Clock with properties:
    
             Source: 'External'
        Destination: 'cDAQ1/PFI0'
               Type: ScanClock
    
    
    ans = 
    
      DigitalTrigger with properties:
    
             Source: 'External'
        Destination: 'cDAQ1/PFI1'
               Type: StartTrigger
          Condition: 'RisingEdge'

    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 queue the values. Note that reading and writing digital I/O line values typically does not require that you configure specific property values.

    preload(d,round(rand(4000,2)));
    gval = start(d);
  6. Display input — To read only the input lines, type:

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

    delete(d)
    clear d