How to Configure the Modicon PLC for a Quadrature Encoder

What is a quadrature encoder?

A quadrature encoder is a sensor used to measure parameters associated with shaft rotation. With programming the associated Programmable Logic Controller (PLC) may track the direction of shaft rotation, position, and rotation velocity. In an industrial environment, the quadrature (incremental) encoder is often sold as an independent sensor that may be attached to a motor or mechanism. The combination facilitates fine measurement of a machine’s motion.

In this short article, we will explore a quadrature encoder application featuring a Schneider Electric Modicon M221 PLC model TM221CE24T and a demonstration quadrature encoder featuring Banner DS18VP6LP retroreflective sensors. The assemblies are pictured together in Figure 1.

Tech Tip: The quadrature (incremental) encoder is always used as part of a digital system. The corresponding PLC or microcontroller determines position by counting the phase transitions on the encoder’s A and B lines. The hardware and software speed of this device is critical as a missed transition will corrupt the reckoning of position.

If you are new to this topic, you can learn more about the quadrature encoder from this introductory article.

Figure 1: Picture of the demonstrator quadrature encoder featuring Banner retroreflective sensors. The Modicon PLC is seen in the background.

How popular is the quadrature encoder?

Incorporating a quadrature encoder with a PLC is a common application. In fact, it is so common that PLC manufacturers include software and hardware to accommodate the sensor. This simplifies machine design and integration. It also allows for high performance and high-speed applications. With a quality encoder, this allows fine resolution measurements of fast-moving mechanisms. For example, an industrial machine may include a motor driven linear bed. Incorporation of a quadrature encoder allows for sub-millimeter positioning of the mechanism with performance limited by the backlash in the mechanical components.

How is a quadrature encoder physically connected to a PLC?

The physical connections between the quadrature encoder and the PLC are minimal. The simplest configuration requires 4 wires including 24 VDC and a return to power the sensor along with wires for the A and the B encoder outputs. There may be a shield wire. Some quadrature encoders feature a third “index” output which indicates a zero-degree (top center) position of the encoder’s shaft.

Many quadrature encoders connect directly to the PLC’s 24 VDC inputs. Consequently, it is convenient to select encoders with PNP outputs as this allows a direct connection to most PLC inputs. Be sure to review this engineering note for more information about PNP and NPN type sensors and the connection to a PLC.

Attributes of the Modicon M221

The Modicon M221 PLC is designed for motion applications. It incorporates dedicated high-speed counters that may be configured to monitor and track the quadrature encoder. For example, the PLC could be connected to and then programmed to control a servomotor. This servo motor may have a quadrature encoder directly coupled to its shaft, transmission, or other mechanical component.

A servo feedback loop may be constructed when the PLC controls the motor based on the feedback from the quadrature sensor. PLC speed is critical for an application such as this as missed counts (identification of rising and falling edges of the A and B signals) will cause corruption of the position with great potential to damage equipment or cause harm to personnel. Errant equipment can crash into the machine end stops or erratic high mechanical stress movements.

To prevent this problem the Modicon features High Speed Counters (HSC) that will respond to frequencies as high as 100 kHz. This counter may be configured with one of several different quadrature modes to meet the needs of your specific machine. The position count may use a word (0 to 65535 count) or double words (0 to 4294967295).

Configuration of the Modicon high speed counter

The High Speed Counter (HSC) configuration wizard is shown in Figure 2. To save time we will only explore the attributes directly associated with our Figure 1 Hardware. Recall that the quadrature encoder demonstrator features 16 counts per revolution. For this to be true we must transition on every changing edge of the A and B signal.

The configuration is described as:

  • Type of HSC: select dual phase for the quadrature counter.
  • Input mode: select Quadrature x4 for a transition on every changing edge of the encoder’s A and B outputs.
  • Check the “Double Word” box for a count from 0 to 2^{32}
  • By default the high speed inputs %I0.0 and %I0.1 are used for the A and B signals.

Again, this is the best setup for a demonstration using the given hardware. Be sure to configure the system for your needs.

Figure 2: Configuration of the Modicon high speed counter for a quadrature encoder interface.

Minimalist program to read the quadrature encoder

Figure 3 presents the ladder logic required to read the quadrature encoder. Observe that the HSC block must be instantiated before it can be used. It is “hung on the rung” via the “IN” signal. This must evaluate as true to enable the block. Activating the S input resets the count.

Like many of Modicon’s function blocks, we need to use dotted notation to access the various elements. For example, there are memory elements signaling an overflow (%HSCi.F), direction of rotation (%HSCi.U), and the count itself (%HSCi.V for word with %HSCi.VD for double word).

Tech Tip: if you are new to the Modicon PLC and have some Arduino Experience, you may want to review this article. It provides an introduction to the Modicon memory elements through the lens of the Arduino microcontroller.

Rung 2 of Figure 3 shows how to retrieve the double word value. The HSC0.VD data are copied into the %MD0 memory location. In this image, we note that the quadrature count has advanced to 136. Given our 16-count per revolution hardware show in in Figure 1, we can calculate that the system has completed 8.5 revolutions.

For more information about the HSC and Modicon in general, be sure to explore the EcoStructure Machine Expert documentation. You can find the documentation by clicking the help question mark from with the Machine Expert program itself. You can start by exploring the High Speed Counter (%HSC) chapter.

Figure 3: Ladder Logic program to read the Modicon high speed counter. The block is instantiated on rung 1 and the quadrature encoder’s count is read on rung 2.

Parting thoughts

This engineering brief just scratches the surface when it comes to automating a machine featuring a quadrature encoder. We have yet to explore motor drives and control systems. Perhaps more importantly, we have yet to explore machine behaviors and how to design a machine to complement both operator and technician.

With regards to the Modicon PLC, this note provides a straightforward quadrature encoder demonstration. As an analogy we went as fast as we could from point A to B. Be sure to go back and smell the proverbial roses with all the configuration options for an HSC.

Please leave your comments and suggestions below. Is there any specific content you are looking for?

Best Wishes,

APDahlen

About the Author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.

Return to the Industrial Control and Automation Index