MATLAB Examples

# Measure Pulse Width Using NI Devices

This example shows how to measure the width of an active high pulse. A sensor is used to measure distance from a point. The width of the pulse correlates to the actual distance measured.

## Create a Counter Input Channel

Create a DAQ Session, and add a counter input channel with PulseWidth measurement type. For this example, use CompactDAQ chassis NI c9178 and module NI 9402 with ID cDAQ1Mod5.

```s = daq.createSession('ni'); addCounterInputChannel(s,'cDAQ1Mod5', 'ctr0', 'PulseWidth'); s ```
```s = Data acquisition session using National Instruments hardware: Will run for 1 second (1000 scans) at 1000 scans/second. Number of channels: 1 index Type Device Channel MeasurementType Range Name ----- ---- --------- ------- --------------- ----- ---- 1 ci cDAQ1Mod5 ctr0 PulseWidth n/a ```

## Determine the Terminal of the Counter Input Channel

To connect the input signal to the correct terminal, examine the Terminal property of the channel. The terminal is determined by the hardware.

```s.Channels(1).Terminal ```
```ans = 1×4 char array PFI1 ```

## Measure Distance

To determine if the counter is operational, input a single scan. The sensor output a high pulse of width 0.0010 seconds corresponding a distance of one meter.

```s.inputSingleScan() * 1000 ```
```ans = 5 ```

## Measure Distance over Time

Use the hardware clock to acquire multiple counter measurements over time. NI counter devices require an external clock. By adding an analog input channel for a module on the same chassis, the session shares an internal clock with both modules.

```s = daq.createSession('ni'); addAnalogInputChannel(s,'cDAQ1Mod1', 0, 'Voltage'); addCounterInputChannel(s,'cDAQ1Mod5', 0, 'PulseWidth'); s.DurationInSeconds = 10; s.Rate = 1; [data, time] = s.startForeground(); plot(time, data(:,2)*1000); ```