Example Code

Measure Incremental Quadrature Encoder Rotation using DAQ-STC Counter

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.


  • Data Acquisition (DAQ)


  • LabVIEW


  • NI DAQmx

Code and Documents


This VI demonstrates on measuring the rotation of an incremental quadrature encoder using the DAQ-STC counter on E-series DAQ cards.

Connect phase A of the encoder to the SOURCE of counter 0 and connect phase B to DIO 6 which is the Up/Down line for counter 0.  
If you configure Counter 0 for Hardware Up/Down counting, Counter 0 counts up when the Up/Down line is high and down when the Up/Down line is low.
If counting on the rising edge of phase A, the Up/Down line will be high when rotating clockwise (CW) and low when rotating counter-clockwise (CCW).

LabVIEW 2012 or compatible.
NI-DAQmx 15.5.1 or compatible.
DAQ-STC counter.

Steps to Implement or Execute Code:

  1. Define the Counter Input channel to be used for measurement, the edges of the input signal to increment or decrements the count and also the encoder resolution.
  2. Ensure that the maximum value of the Position indicator is similar to the Encoder Resolution. You may change the maximum value just by double-clicking the indicator.
  3. Run this VI and monitor the indicators to know the position, total revolutions and total distance of the encoder.

Block Diagram Steps:

  1. Creates a channel to count the number of rising or falling edges of a digital signal.
  2. Runs the configured task.
  3. Returns data from the Counter Input terminal in terms of number of edges of the pulse.
  4. The DAQ-STC counters are 24-bit up/down counters which include three input signals (SOURCE, GATE and UP_DOWN) and two output signals (OUT and INTERRUPT).
    Wire the result of the logic AND of hexadecimal constant (800000) and the data from the encoder to the Case Selector to check 8 most significant  values of the acquired data to determine the direction of rotation. Value of 1 means that the encoder is rotating to the opposite direction.  Hence, add the data read from the encoder by a negative 24-bit constant by using logic OR. The green LED indicates the rotation to the opposite direction. When the encoder rotates to the positive direction, this code will directly convert the data read from the encoder to Long Integer and computes the total distance covered, current position and total revolutions.
  5. Displays the Total Distance, Position and Total Revolutions of the encoder.
  6. Stops and clears the task. Then, displays any error that occurs.

Front Panel.PNG




**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.



In my case result is correct if I connect signals A and NOT B (B inv.).


Can someone please mail me this VI that is compatible with LabVIEW 8.0 . My mail id is rajnish14feb@gmail.com