Back to Tutorial

Rotary Encoder with Esp32

  •  23-07-2025

📌 What is a Rotary Encoder?

rotary encoder is an electro-mechanical sensor that converts the angular position or motion of a rotating shaft into digital signals. Unlike a potentiometer, it can rotate infinitely in either direction and is commonly used in digital volume knobs, step selection, or menu navigation.

There are two main types:

  • Incremental (most common)
  • Absolute

The most widely used type with ESP32 is the incremental rotary encoder with a push-button switch.


🧠 Key Features of Rotary Encoder

FeatureDetails
RotationContinuous 360° (no end stops)
Signal OutputDigital (Quadrature: A and B)
Additional ButtonBuilt-in push button (optional)
ResolutionTypically 20 pulses per rotation
Operating Voltage3.3V to 5V
DurabilityLong life (up to 100,000 rotations)

⚙️ Pinout of Rotary Encoder

PinNameDescription
CLKOutput AChannel A (primary output)
DTOutput BChannel B (direction control)
SWSwitchPush-button (active LOW)
VCCPower3.3V or 5V input
GNDGroundCommon ground

📦 How It Works

A rotary encoder generates two square wave signals (CLK and DT) slightly out of phase. By comparing the timing between these signals, the ESP32 can determine the direction of rotation:

  • Clockwise (CW): CLK leads DT
  • Counterclockwise (CCW): DT leads CLK

The SW pin is a built-in push button often used to select or confirm actions.


🧪 Applications of Rotary Encoder

  • Digital volume knobs
  • Menu navigation in embedded systems
  • Motor position tracking
  • CNC machines and robotics
  • Media players and audio mixers
  • IoT interfaces and control panels

🔗 Basic Example with ESP32

✅ Components Needed

  • ESP32 Dev Board
  • Rotary Encoder Module (KY-040 or similar)
  • Jumper Wires
  • Breadboard

📍 Where to Use a Rotary Encoder

  • 🔊 Digital volume control for audio systems
  • 📻 Tuning knobs in radios and receivers
  • 📋 Menu navigation in embedded systems (LCD/OLED)
  • 🔁 Infinite scrolling inputs (brightness, time, settings)
  • 🧭 Direction and position control in robotics

🧾 ESP32 Code With Rotary Encoder

HOW TO OPERATE
#define CLK 19
#define DT  18
#define SW  21  

int counter = 0;
int lastStateCLK;
bool currentStateCLK;
bool currentStateSW;
bool lastStateSW = HIGH;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);
  Serial.begin(115200);

  lastStateCLK = digitalRead(CLK);
}

void loop() {
  currentStateCLK = digitalRead(CLK);


  if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
    if (digitalRead(DT) != currentStateCLK) {
      counter++;  
    } else {
      counter--;
    }
    Serial.print("Counter: ");
    Serial.println(counter);

   
  }

  lastStateCLK = currentStateCLK;

  currentStateSW = digitalRead(SW);
  if (currentStateSW == LOW && lastStateSW == HIGH) {
    Serial.println("Button Pressed");
    delay(50);  
  }
  lastStateSW = currentStateSW;

delay(1);
}

❗ Important Notes

  • Debouncing: Mechanical rotary encoders may need software debouncing or filtering for smooth reading.
  • Interrupts: For better performance and smoother rotation, use hardware interrupts instead of polling.
  • Pull-ups: Some modules have built-in pull-up resistors; if not, use INPUT_PULLUP.
  • Power Compatibility: Works on both 3.3V and 5V logic—perfect for ESP32.
  • Wiring Tip: Keep wires short to reduce noise.
Rotary Encoder with Esp32

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial