
A rotary encoder is an electromechanical input device that converts rotational movement into electrical pulses. Unlike a regular potentiometer, it can rotate endlessly in either direction while letting you know both the direction and the exact number of steps turned, making it well suited for control interfaces like volume knobs or menu navigation.
Components needed
An Arduino Uno board, a KY-040 rotary encoder (with a built-in push button), an LCD screen to show the value, and a potentiometer to adjust screen contrast.
Wiring it up
The encoder's A and B outputs and its SW push button connect to three separate digital pins (pins 8, 9, and 10 in this project), in addition to power and ground.
How it works
Inside, the encoder has a rotating disc with contact points spaced at regular intervals, and turning the knob makes these points touch outputs A and B in sequence, producing two signals offset in time by roughly a 90-degree phase shift. The code watches for changes on output A only, and whenever a change occurs, it compares it against output B's state at that moment: if B's state matches A's previous state, the rotation is clockwise; otherwise, it's counterclockwise. Based on this comparison, an internal counter increments or decrements, and the current value along with the last movement's direction is shown live on the LCD.
You can find the KY-040 rotary encoder at Juthour Tech.
اترك تعليقا