Main Content

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, access the device in the table returned by the daqlist command. This example uses National Instruments® CompactDAQ Chassis NI cDAQ-9178 and module NI 9234 with ID cDAQ1Mod3.

d = daqlist("ni")
d =

  12×4 table

     DeviceID                 Description                    Model             DeviceInfo     
    ___________    __________________________________    _____________    ____________________

    "cDAQ1Mod1"    "National Instruments NI 9205"        "NI 9205"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod2"    "National Instruments NI 9263"        "NI 9263"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod3"    "National Instruments NI 9234"        "NI 9234"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod4"    "National Instruments NI 9201"        "NI 9201"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod5"    "National Instruments NI 9402"        "NI 9402"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod6"    "National Instruments NI 9213"        "NI 9213"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod7"    "National Instruments NI 9219"        "NI 9219"        [1×1 daq.DeviceInfo]
    "cDAQ1Mod8"    "National Instruments NI 9265"        "NI 9265"        [1×1 daq.DeviceInfo]
    "Dev1"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]
    "Dev2"         "National Instruments NI ELVIS II"    "NI ELVIS II"    [1×1 daq.DeviceInfo]
    "Dev3"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]
    "Dev4"         "National Instruments PCIe-6363"      "PCIe-6363"      [1×1 daq.DeviceInfo]

deviceInfo = d{3, "DeviceInfo"}
deviceInfo = 

ni: National Instruments NI 9234 (Device ID: 'cDAQ1Mod3')
   Analog input 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 DataAcquisition, and add an analog input channel with Accelerometer measurement type.

dq = daq("ni");
ch = addinput(dq, "cDAQ1Mod3", "ai0", "Accelerometer");

Set the Scan Rate

Change the acquisition scan rate to 4000 scans per second.

dq.Rate = 4000;

Set the 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 with 9.22 mV per gravity.

ch.Sensitivity = 0.00922;
ch = 

    Index    Type      Device       Channel       Measurement Type              Range                 Name      
    _____    ____    ___________    _______    ______________________    ____________________    _______________

      1      "ai"    "cDAQ1Mod3"     "ai0"     "Accelerometer (Diff)"    "-5.0 to +5.0 Volts"    "cDAQ1Mod3_ai0"

Acquire and Plot Data

Use the read command to acquire data for 30 seconds.

data = read(dq, seconds(30));
plot(data.Time, data.cDAQ1Mod3_ai0);
ylabel("Acceleration (Gravities)");