Project Overview
The "Precision Input Interface" project delves into the mechanics and firmware required to handle Incremental Rotary Encoders using Hardware Interrupts. While they share a similar physical form factor with rotary potentiometers, encoders are fundamentally different; they are digital devices that output a series of pulses rather than a variable voltage. This tutorial focuses on using an Arduino Nano to decode Gray code quadrature signals, ensuring zero-latency tracking of rotational direction and speed. This is a critical skill for developing volume knobs, menu navigation systems, and motor feedback controllers.
In this video I will show you how to use Rotary Encodedrs with Arduino Interrupts.It is quite fascinating that this component looking similar to the rotary potentiometer is actually totally different.Hope you would find this tutorial useful.
If you have never used potentiometer with Arduino probably you want to view this tutorial first:
And here is Rotary Encoder tutorial:
Technical System Architecture
- Quadrature Encoding & Gray Code: Rotary encoders use two output pins (A and B). As the shaft turns, these pins output square waves that are $90^\circ$ out of phase. By comparing the state of Pin B at the moment Pin A pulses, the software can determine the direction of rotation (CW or CCW). Gray code ensures that only one bit changes at a time, minimizing errors during rapid rotation.
- Hardware Interrupt Strategy (
attachInterrupt): To avoid missing high-speed pulses (which can happen if the mainloop()is busy), the project employs hardware interrupts. The Arduino Nano's Digital Pins 2 and 3 are mapped toINT0andINT1. TheattachInterrupt()function triggers an Interrupt Service Routine (ISR) as soon as the signal edges change, allowing for "real-time" response regardless of the workload in the main code. - Debouncing Challenges: Encoders are mechanical switches and are prone to "bouncing"—rapid on/off oscillations during contacts. This system handles debouncing either through hardware (RC filters) or, more commonly, through a software-based state machine or a "time-check" logic within the ISR to ignore pulses occurring within a few milliseconds of each other.
- Visual Feedback with 7-Segment Display: To demonstrate the precision of the encoder, the project interfaces with a Custom 7-Segment Display. As the encoder turns, the display increments or decrements numerically, showcasing the low-latency interaction achieved via interrupts.
Engineering & Hardware Features
- Infinite Rotation: Unlike potentiometers, encoders rotate infinitely in either direction. This makes them ideal for UI elements where the range of data is dynamic or unknown.
- Detent vs. Smooth Encoders: The project explains the "feel" of encoders with mechanical detents (clicks) versus smooth optical encoders, and how software needs to be tuned for each (e.g., counting one pulse per detent versus high-resolution counting).
- Push-Button Integration: Most common rotary encoders include a momentary push-button on the shaft. This allows for a "Click to Select" UI mechanism, often used to switch between different menu parameters.
- High-Speed Tracking: By using the
CHANGEtrigger inattachInterrupt, the software achieves 4x Encoding Resolution, counting every rising and falling edge of both Channel A and Channel B, providing ultra-fine control for precision instruments.