1. The structure of the grating ruler
An encoder is a device that can convert angular displacement or linear displacement into electrical signals.


A grating ruler is a type of encoder. It is often used to measure linear displacement. It consists of two parts: the scale grating and the grating read head.

Generally, the scale grating is fixed. The movement of the grating read head is used to detect the displacement. The grating read head is composed of a light source and a lens. An index grating, photosensitive elements, and other components.


Among them, the index grating is a relatively important part. It directly determines the measurement accuracy of the grating ruler.
2. How is the grating ruler measured?
The measurement of the grating ruler utilizes the Moiré fringe phenomenon generated by the grating. A grating is made by engraving a large number of parallel and equidistant marks on glass.
The marks are the opaque parts. The slits between the marks are the light-transmitting parts. For a refined grating, several thousand or even tens of thousands of marks can be engraved within a width of 1 cm.

Moiré fringes are the visual effects of interference between two lines or two objects. At a constant angle and frequency.

In 1874, British physicist Rayleigh. First revealed the scientific and engineering value of the Moiré fringe pattern. Pointed out the possibility of measuring the relative displacement of the grating. By observing the movement of the Moiré fringes.
How are Moiré fringes used in a grating ruler?
Suppose we want to measure the displacement x. Then, as long as we know the grid width d of the grating and the number N of the moving grids, we can obtain the value of the displacement x.

How should the number of moving grids be calculated?
Suppose there is a fixed point. When the light passes through the grating and projects onto the fixed point, due to the movement of the grating, the change of the light at the fixed point is alternating between light and dark. By recording this change, we can know the number N of the moving grids.

However, the slits on the grating are very narrow, and it is difficult to distinguish the light and dark changes at the fixed point during movement. If we can magnify this phenomenon, the problem can be easily solved. Moiré fringes have such a magnifying effect.
3. How to judge the direction of application of the grating ruler
Although the moving distance of the object has been calculated. But how can we determine its moving direction?
We can place four photosensitive elements In the read head, namely A+, B+, A-, and B-.

The output signals of each photosensitive element differ by a quarter of a cycle. Subtracting A- from A+ gives the A signal, and subtracting B- from B+ gives the B signal.

When the A signal leads the B signal, the ruler body moves forward.

When the A signal lags behind the B signal, the ruler body moves backward.

The A signal and the B signal have the function of preventing signal interference. When the rising edge of the A signal arrives, and the system does not receive the rising edge of the B signal, the system will not count. Or when the A signal is interfered with, and two rising edges are generated, the system will also eliminate this interference. Because when the system receives the rising edge of A, it will check whether the rising edge of B arrives, and will not count the other rising edge of the A signal.
The A signal and the B signal can also improve the accuracy of the grating ruler. In a complete signal cycle, the A signal will generate one rising edge and one falling edge, and the B signal will also generate one rising edge and one falling edge.
Whenever the rising edge of the A signal arrives, the system counts once—this is the single frequency multiplication. Using the accuracy of the grid pitch of the grating ruler. When counting the rising edge and the falling edge of the A signal Respectively, it is the double frequency multiplication, which divides one grid pitch into two parts. When counting the rising edges and the falling edges of the A signal and the B signal respectively, it is the quadruple frequency multiplication. So that the grating ruler can measure more precise displacements.

The accuracy of the grating ruler refers to the manufacturing error of the grating ruler. For example, ±0.275μm/10mm, which means that the possible error for a movement of 10mm is ±0.275μm.
It should be noted that, an accuracy of ±0.275μm/10mm, cannot be deduced to ±2.75μm/100mm, And there is no such linear relationship between them. The resolution marked on the grating ruler, is generally obtained through frequency division, including the physical frequency division introduced above.
And an electronic circuit frequency division method. When the grid pitch of the grating ruler is 25μm, whenever the ruler body moves 25μm, it will output one cycle. After being subdivided 50 times by the circuit, for every 25μm movement of the ruler body,it will output 50 cycles. That is, the resolution said by the manufacturer is 0.5μm (25÷50). When the movement of the ruler body does not exceed 25μm, the read head will not output a cycle.
Let’s summarize the characteristics of the grating ruler:
It has a high detection accuracy, and can reach a resolution of 0.1μm after subdivision.
It has a fast response speed and can realize dynamic measurement, which is beneficial to automatic control.
It has high requirements for the environment and is afraid of oil stains, dust, and vibration; it is difficult to install and maintain and has a high cost.
The structure of the encoder and the grating ruler is not very different, and there are many common points in many aspects.
4. Collect grating ruler data
Next, we use a data acquisition cardto collect the displacement data of the grating ruler. The equipment used in the experiment is a grating ruler and a USB-3313 data acquisition card.

First, explain the specific parameters of the grating ruler: its defined stroke is 200mm, the maximum measurable distance is approximately 220mm, the grid pitch is 0.02mm (one signal cycle), and the accuracy is 0.005mm (the minimum measurement step).
The pin definition of the grating ruler:
Red 1: Connect to positive power supply;
Black 2: Connect to negative power supply;
Green 3: Defined as A-phase signal;
White 4: Defined as B-phase signal;
Orange 5: Defined as Z-phase signal.
Now start wiring:
Connect the Ct 0 Src port of the acquisition card to the A phase (green),
Connect the Ct 0 Gate port to the B phase (white),
And connect the Ct 0 Z to the Z phase (orange).
Since the grating ruler provides a power plug, connect the GND terminal (negative pole of the power supply) of the grating ruler to the DGND terminal of the acquisition card, and connect the positive pole of the power supply to the positive pole (red) of the grating ruler.
Finally, connect the acquisition card to the computer.

Open the latest version of the DAQ Software. Click Coder, and start setting the parameters.

Set the sampling rate to 10000,

Select channel CT 0. Select the encoder type QuaEncodeX4, which means that each signal cycle can have four increments or decrements, Corresponding to four increments or decrements of the measurement step (0.005mm) in each cycle.
In the unit conversion, set the value represented by each pulse to 5, with the unit of μm. Click next,

do not check the data storage,

select immediate trigger, and click finish.

Set the abscissa of the software to 0 to 10000, and click start. The starting position of the grating ruler is 0.

When sliding the grating ruler, you can see the software displays the data information of the grating ruler. On the right side of the software, You can directly read out that the grating read head has displaced 224.115mm.

When moving the grating read head quickly, you can see the waveform change accordingly,

and even a slight movement will be reflected in the waveform diagram.
