DEV Community

Cover image for ESP32 S3 - Rotary Encoder
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 S3 - Rotary Encoder

A rotary encoder lets you read both the direction and the amount of rotation from a knob that spins forever, unlike a potentiometer which stops at its mechanical limit. That makes it a good fit for menu navigation, volume controls, or speed adjustment in your ESP32 S3 projects. This post covers wiring the encoder, understanding how it generates signals, and reading it in code, first by polling, then with an interrupt.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 S3 - Rotary Encoder

What you need

Wiring

ESP32 S3 - Rotary Encoder wiring diagram

The encoder module has five pins. CLK and DT are the two pulse outputs, SW is the built-in pushbutton, and VCC/GND power the module. In the example sketch, CLK goes to pin 19, DT to pin 20, and SW to pin 21 on the ESP32 S3. Connect VCC to the board's 3.3V pin rather than 5V, since that keeps you within the GPIO voltage tolerance, and connect GND to GND. The SW pin needs a pull-up resistor so it reads HIGH when idle and LOW when pressed; most modules already include one onboard, but it's worth checking before assuming it's there.

How it works

Inside the encoder, a slotted disc rotates against a common ground pin. As it turns, the CLK and DT pins each make contact with ground in turn, producing two pulse trains that are 90 degrees out of phase, a pattern called quadrature encoding. The order in which the pulses arrive tells you the direction: if pin A (CLK) contacts ground before pin B (DT), the knob is turning clockwise. Your microcontroller watches for CLK transitioning from LOW to HIGH, then immediately checks the DT pin. If DT reads HIGH at that moment the rotation is counterclockwise, and if it reads LOW the rotation is clockwise.

Code

The first sketch polls the CLK pin in the main loop, comparing its current state to the previous one. On a rising edge it checks DT to decide direction and updates a running counter, printing the direction and count to the Serial Monitor. The ezButton library handles debouncing for the SW pin automatically, so a press is reported cleanly without extra logic. This polling approach is easy to follow but burns CPU cycles continuously and can miss fast pulses if other code in the loop runs too slowly. An interrupt-based version instead lets the hardware trigger on the CLK transition directly, freeing the loop for other work.

esp32-s3-rotary-encoder.ino

/*
 * This ESP32 S3 code was developed by newbiely.com
 *
 * This ESP32 S3 code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/esp32-s3/esp32-s3-rotary-encoder
 */

#include <ezButton.h>  // The library to use for SW pin

#define CLK_PIN 19 // The ESP32 S3 pin 19 connected to the rotary encoder's CLK pin
#define DT_PIN  20 // The ESP32 S3 pin 20 connected to the rotary encoder's DT pin
#define SW_PIN  21 // The ESP32 S3 pin 21 connected to the rotary encoder's SW pin

#define DIRECTION_CW  0   // clockwise direction
#define DIRECTION_CCW 1  // counter-clockwise direction

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;

ezButton button(SW_PIN);  // create ezButton object for pin 21;

void setup() {
  Serial.begin(115200);

  // Configure encoder pins as inputs
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  button.setDebounceTime(50);  // set debounce time to 50 milliseconds

  // read the initial state of the rotary encoder's CLK pin
  prev_CLK_state = digitalRead(CLK_PIN);
}

void loop() {
  button.loop();  // MUST call the loop() function first

  // read the current state of the rotary encoder's CLK pin
  CLK_state = digitalRead(CLK_PIN);

  // If the state of CLK is changed, then pulse occurred
  // React to only the rising edge (from LOW to HIGH) to avoid double count
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
    // if the DT state is HIGH
    // The encoder is rotating in counter-clockwise direction => decrease the counter
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;
      direction = DIRECTION_CCW;
    } else {
      // The encoder is rotating in clockwise direction => increase the counter
      counter++;
      direction = DIRECTION_CW;
    }

    Serial.print("Rotary Encoder:: direction: ");
    if (direction == DIRECTION_CW)
      Serial.print("Clockwise");
    else
      Serial.print("Counter-clockwise");

    Serial.print(" - count: ");
    Serial.println(counter);
  }

  // save last CLK state
  prev_CLK_state = CLK_state;

  if (button.isPressed()) {
    Serial.println("The button is pressed");
  }
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If the count jumps around or skips, check that VCC is wired to 3.3V and not 5V, and that the SW pin actually has a pull-up, either onboard or added externally. Loose or long jumper wires on CLK and DT can introduce noise that looks like extra rotation, so keep those connections short and secure. If direction seems reversed from what you expect, it just means CLK and DT are swapped relative to how the code expects them, which you can fix in wiring or by inverting the logic in code.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-s3/esp32-s3-rotary-encoder

Top comments (0)