

A rotary encoder knob is a versatile input device that allows users to control variables such as volume, brightness, or menu navigation by rotating the knob. Unlike potentiometers, rotary encoders provide digital signals, making them ideal for applications requiring precise and incremental adjustments. Many rotary encoders also include a built-in push button, which adds an extra layer of functionality, such as selecting options or confirming actions.








The rotary encoder typically has 5 pins: three for the encoder and two for the push button. Below is the pinout:
| Pin Name | Description | Connection |
|---|---|---|
| GND | Ground pin for the encoder and button | Connect to GND |
| VCC | Power supply pin (3.3V or 5V) | Connect to 3.3V or 5V |
| CLK (A) | Clock signal output (Channel A) | Connect to microcontroller GPIO |
| DT (B) | Data signal output (Channel B) | Connect to microcontroller GPIO |
| SW | Push button signal output | Connect to microcontroller GPIO |
Wiring the Rotary Encoder:
Debouncing:
Reading the Encoder:
Using the Push Button:
Below is an example of how to use a rotary encoder with a push button on an Arduino UNO:
// Rotary Encoder Pins
#define CLK 2 // Connect to CLK (A) pin of the encoder
#define DT 3 // Connect to DT (B) pin of the encoder
#define SW 4 // Connect to SW pin of the encoder
int counter = 0; // Variable to store the encoder count
int currentStateCLK;
int lastStateCLK;
bool buttonPressed = false;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP); // Use internal pull-up resistor for the button
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If the state of CLK has changed, check the direction
if (currentStateCLK != lastStateCLK) {
// If DT state is different from CLK state, the encoder is rotating CW
if (digitalRead(DT) != currentStateCLK) {
counter++;
} else {
counter--;
}
// Print the counter value
Serial.print("Counter: ");
Serial.println(counter);
}
// Update lastStateCLK to the current state
lastStateCLK = currentStateCLK;
// Check if the button is pressed
if (digitalRead(SW) == LOW) {
if (!buttonPressed) {
Serial.println("Button Pressed!");
buttonPressed = true;
}
} else {
buttonPressed = false;
}
}
The encoder is not registering rotation:
The push button is not working:
The encoder skips steps or behaves erratically:
The direction of rotation is reversed:
Q: Can I use the rotary encoder with a 3.3V microcontroller?
A: Yes, most rotary encoders are compatible with both 3.3V and 5V systems. Check the datasheet for your specific model.
Q: Do I need external pull-up resistors for the push button?
A: Not necessarily. You can use the internal pull-up resistors of your microcontroller by configuring the pin as INPUT_PULLUP.
Q: How do I debounce the encoder signals in software?
A: Implement a delay or use a state machine to filter out rapid signal changes caused by noise.
Q: Can I use the encoder for high-speed applications?
A: Rotary encoders are suitable for moderate-speed applications. For high-speed use, ensure your microcontroller can process the signals fast enough.