Measure Frequency Using NI Devices

This example shows how to measure frequency to determine rate of flow of fluid using a flow sensor. The sensor generates a digital signal with frequency correlates to the rate of flow of fluid.

Create a Counter Input Channel

Create a DAQ Session, and add a counter input channel with Frequency 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', 'Frequency');
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    Frequency       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.

ans =

  1×4 char array


Measure Frequency

To determine if the counter is operational, input a single scan while the motor is rotating.

ans =


Monitor Frequency 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, 'Frequency');
s.DurationInSeconds = 10;
s.Rate = 1;
[data, time] = s.startForeground();
plot(time, data(:,2));