Accessing Your Hardware

Connecting 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® PCI-6024E 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 function properties.

Acquiring Data

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

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

  1. Create a device object — Create the analog input object ai for a sound card.

    ai = analoginput('winsound');
  2. Add channels — Add two hardware channels to ai.

    addchannel(ai,1:2);
  3. Configure property values — Configure the sampling rate to 44.1 kHz and collect 1 second of data (44,100 samples) for each channel.

    ai.SampleRate = 44100)
    ai.SamplesPerTrigger = 44100)
  4. Acquire data — Start the acquisition and issue wait to block the MATLAB Command Window until all data is acquired. When all the data is acquired, wait returns and the data is then available to getdata.

    start(ai)
    wait(ai,2)
    data = getdata(ai);
    plot(data)
  5. Clean up — When you no longer need ai, you should remove it from memory and from the MATLAB workspace.

    delete(ai)
    clear ai

Outputting 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 device object — Create the analog output object ao for a sound card.

    ao = analogoutput('winsound');
  2. Add channels — Add two hardware channels to ao.

    addchannel(ao,1:2);
  3. Configure property values — Configure the sampling rate to 44.1 kHz for each channel.

    ao.SampleRate = 44100
  4. Output data — Create 1 second of output data, and queue the data in the engine for eventual output to the analog output subsystem. You must queue one column of data for each hardware channel added.

    data = sin(linspace(0,2*pi*500,44100)');
    putdata(ao,[data data])

    Start the output. When all the data is output, ao automatically stops executing.

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

    delete(ao)
    clear ao

Reading and Writing Digital Values

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 device object — Create the digital I/O object dio for a National Instruments USB-6212 board with hardware ID Dev1.

    dio = digitalio('nidaq','Dev1');
  2. Add output lines — Add four lines from port 0 to dio, and configure them for output.

    addline(dio,0:3, 0,'out');
  3. Add input lines — Add two lines from port 1 to dio, and configure them for input.

    addline(dio,0:1, 1,'in');

    To display a summary of the digital I/O object, type:

    dio
    
    Display Summary of DigitalIO (DIO) Object Using 'USB-6212'.
    
             Port Parameters:  Port 0 is port configurable for reading and writing.
                               Port 1 is port configurable for reading and writing.
                               Port 2 is port configurable for reading and writing.
                               
               Engine status:  Engine not required.
    
    DIO object contains line(s):
    
       Index:  LineName:  HwLine:  Port:  Direction:  
       1       ''         0        0      'Out'       
       2       ''         1        0      'Out'       
       3       ''         2        0      'Out'       
       4       ''         3        0      'Out'       
       5       ''         0        1      'In'        
       6       ''         1        1      'In'        
  4. Write values — 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.

    pval = [1 1 0 1];
    putvalue(dio.Line(1:4),pval)
    
  5. Read values— To read only the input lines, type:

    gval = getvalue(dio.Line(5:6))
    
    gval =
         0     0
    

    To read both input and output lines, type:

    gval = getvalue(dio)
    
    gval =
         1     1     0     1     0     0
    

    When you read output lines getvalue returns the most recently output value set by putvalue.

  6. Clean up — When you no longer need dio, you should remove it from memory and from the MATLAB workspace.

    delete(dio)
    clear dio

    Note   Digital line values are usually not transferred at a specific rate. Although some specialized boards support clocked I/O, Data Acquisition Toolbox software does not support this functionality.

Acquire Data in a Loop

To make multiple acquisitions using a single analog input object, create a single object and execute the acquisition in a loop. Delete the object at the end of the loop.

% Create the object outside of the loop.
ai = analoginput('nidaq', 'Dev1');
addchannel(ai, 0);
% Execute acquisition.
for ii = 1:num_iterations
		start(ai);
    wait(ai, 2)
    data = getdata(ai);
    plot(data);  
end
% Delete the object out of the loop.
delete(ai)
clear ai

If you are creating the object within the loop, you must delete the object within the loop as well.

% Execute acquisition.
for ii = 1:num_iterations
    % Create the object within the loop.
		ai = analoginput('nidaq', 'Dev1');
    addchannel(ai, 0);
    start(ai);
    wait(ai, 2)
    data = getdata(ai);
    plot(data); 
    % Delete the object within the loop.
		delete(ai)
end
clear ai

    Note:   Make sure you delete the object within the loop as it can consume system resources.

For more information about cleaning up the MATLAB workspace, refer to Cleaning Up.

Was this topic helpful?