# Real-World Monitoring with Encoders

Mark Harris
|  Created: April 1, 2021

Electrical devices don't tend to be useful unless they can interact with the real world, taking measures, producing outputs, or responding to events, for example. One functional ability that devices may require is the capability to measure physical movement. This may be a wheeled vehicle traveling across a surface or a fixed machine's shaft rotation.

An encoder is a sensor that turns a position into an electrical signal. This position may be linear (our moving vehicle) or angular (our rotating shaft). The theoretical principles behind these two types are very similar.

When it comes to off-the-shelf encoders, they come in two primary forms, those that measure an absolute position (called Absolute encoders) and those that measure a change in position (called Incremental encoders). Thinking about it, if you know the starting position, then an incremental encoder can, with some simple math, calculate the absolute position.

## Angular Encoders

An angular encoder is an electro-mechanical device that generates an electrical signal representing a rotating object's position or movement. Typically, this would be the shaft of a motor, generator, or gearbox. Or it may be the axle of a wheeled or tracked vehicle. The possible applications are endless. The ability to measure an electric motor's output shaft speed enables the motor to be under closed-loop control. Your controller can now incorporate circuitry to allow you to maintain a constant speed under changing load conditions or allow a simple electric motor to be used as a precision stepper motor.

There is a range of technologies available for measuring the rotation movement; we'll list the most common here.

Conductive encoders rely on electrical contact movement attached to the rotating object being in contact with one of a series of fixed electrical contacts positioned around the object. The two sets of contacts' physical movements relative to each other make and break an electrical circuit. A typical application is a multiple position rotary switch where a dial between a limited number of preset positions is required. While cheap to implement and with no active components, this method is prone to physical wear and corrosion of the contacts.

Optical encoders rely on an object's movement to block light, reflect it, or let it pass through, depending on its position. A typical example is a disk with one or more holes. A light source, such as an LED, and a light detector, such as a photo-electric cell, are positioned on either side of the disk. These require circuitry to detect and process the electrical signals but do not suffer from wear. However, they may be susceptible to airborne contaminants degrading the amount of light being detected. Eventually, a build-up of dirt or dust on the light source and/or the detector may reach a point where the encoder will cease to operate without maintenance action.

Magnetic encoders rely on the movement of material with magnetic properties attached to the rotating object detected by a device that can detect a magnetic field. This can be achieved by attaching a magnetic to a shaft and locating a detector in proximity such that it generates a signal each time the magnetic passes. Alternatively, a simple toothed steel wheel can be attached to the shaft, and a detector generating a magnetic field can detect the movement of the teeth as they pass. It's common for magnetic encoders to utilize a hall-effect sensor as part of the package. A variation of the magnetic encoder is the inductive encoder. Here, the magnetic field change is seen through a measurable difference in the detecting device's inductance.

Capacitive encoders rely on a conductive material's movement attached to the rotating object detected by a device that can measure capacitance changes as the distance between the detector and the material on the shaft changes. While they offer better resolution than a magnetic encoder with the same advantages of maintenance-free life, they are significantly more expensive.

We can see that the optical, capacitive, and magnetic technologies in their simplest form generate an electrical signal when the source moves past the detector. This provides an incremental position measurement.

An important point to bear in mind is that a simple encoder with a single detector that generates an electrical output signal in response to detected movement cannot tell you which direction the object is rotating. A single detector system has only two states, rotating and not rotating. Mathematically, 'not rotating' is actually 'rotating at an infinitely slow rate,' but that's more of a philosophical rather than technical argument. To determine the direction, we need a system with at least three states, either by having at least two detectors or designing a system to detect the difference between the signal generated when the object rotates in different directions. The simplest and most common solution is two detectors producing a twin output signal.

## Direction Decoding

The most common technique for detecting the direction of movement is through quadrature phase shift encoding. This doesn't sound very easy, but it merely means two detectors produce output signals that are phase-shifted by 90 degrees relative to each other. This means that each signal on and off edges will never be aligned; that is, these signals will never change simultaneously. This technique, known as grey-coding, ensures that the two signals' sampling cannot inadvertently produce an invalid reading. The state's change determines the rotation direction by turning the two signals into binary '0' and '1' values.

For example, if the second output is phase shifted by 90 degrees after the first signal, then the movement in one direction will produce digital states in the order 00, 10, 11, 01, 00. In the opposite direction, the movement will make digital states in the order 00, 01, 11, 10, 00. We can see the difference in the transitions and hence can determine direction. The time between each state change also gives us the rate of rotation.

## Absolute Measurement

A more complex combination of source and/or detector signals must obtain an absolute position measurement. Consider the example of a disk with a series of holes aligned with a light source and detector. Suppose the holes are arranged in complex patterns aligned with multiple detectors such that each discrete position of the disk produces a unique combination of detected signals. In that case, we can determine the absolute position. The drawback is that the better the resolution, the more complex the disk and the greater the detectors. For example, eight detectors will allow a maximum of 256 unique signals, allowing the encoder to measure position within around 1.4 degrees. A more precise angular resolution will require more detectors, taking more space, using more power, and increasing costs.

## Linear Encoders

Linear encoders can be thought of as an angular encoder removed from its shaft, cut along one radius line, and then stretched out until it follows a straight path. The detector's movement along the line of source signals is detected and converted into a movement measurement. The linear encoder's biggest drawback is that the source must cover the entire area that the encoder will operate across, limiting its practical application. Conversely, a device with an angular encoder can detect movement across an infinite range of movements.

Linear encoders use the same technologies and techniques as angular encoders and are subject to precisely the same advantages and disadvantages.

## Designing your Encoder Interface

We'll consider the example of a simple angular encoder mounted on an electric motor's output shaft. This encoder could use any of the electromagnetic technologies we've discussed above; which one you choose won't affect the interface implementation. We need to know the pertinent information that the encoder produces two output signals that allow us to determine the shaft's direction and rate of rotation.

The first point to consider is where the encoder is physically located. An electromagnetic rotary encoder mounted on an electric motor's output will be in an environment with high electrical noise levels and mechanical vibrations. The signal coming out of the encoder then needs to connect to your control device, which you're not going to want to have mounted on the motor. It's fair to say that the input signal into your device will be noisy, so the first step is to implement filtering. The simple option is an RC filter where the encoder signal enters your device to ensure that none of the noise can be coupled into your circuit before it's filtered.

Suppose space is at an absolute premium, or you're mass-producing millions of devices. In that case, you could consider forgoing adding these filtering components, and instead, you could connect the encoder's output directly into an MCU and implement software filtering. It's a common approach in the automotive industry, but it's better to stick with a primary first-order RC filter for the hobby designer. Choosing the filter's component values is straightforward so that we won't repeat the math here. All you need to bear in mind is that the encoder's signals are determined by the motor's shaft's maximum rate of rotation. Knowing this allows you to calculate the maximum frequency of the encoder signals, and hence everything at higher frequencies will be noise.

The filtered outputs need to be pulled up to the correct voltage range for your MCU and then be read directly, and the rest is down to the software. From a software processing point of view, one other point to consider is when to read these input signals. The simple option is to poll the inputs at a sufficiently greater rate than the maximum frequency of the encoder signals. However, encoders are available with a signal line indicating when a state change in the encoder outputs occurs. This signal line can be connected to the MCU to initiate a software interrupt so that the encoder outputs are read at the minimum rate. This is particularly useful if processing resources are limited.

The encoder itself will require a power supply connection; typically, the devices you're likely to use need between 3 V and 5 V and less than 100 mA.

## Conclusion

Encoders are powerful components for measuring movement and introducing feedback from the real world into your electrical device. They include all the sensing circuitry within their packaging, meaning that we only have to supply them with power and handle their output signals. As we've seen, a simple electromagnetic rotary encoder can, with minimal effort, allow you to control a motor's speed. If the motor is mounted on a moving object, then counting the number of rotations can determine the distance moved with some basic math. Now you're able to control a car, or an elevator, or even a robot.

Have more questions? Call an expert at Altium and discover how we can help you with your next PCB design.