ESP32 C3 Super Mini - Rotary Encoder

Learn how to use a rotary encoder with your ESP32 C3 Super Mini to detect rotation direction and position. This beginner-friendly tutorial covers everything from wiring to coding with interrupt-based techniques.

In this guide, you'll learn:

ESP32 C3 Super Mini - Rotary Encoder

Hardware Preparation

1×ESP32 C3 Super Mini
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Rotary Encoder
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Rotary Encoder

A rotary encoder is a position sensor that converts rotational movement into electrical signals to determine direction and rotation amount.

Key types of rotary encoders:

  • Incremental encoder: Uses pulse signals to measure relative position changes - perfect for tracking rotation
  • Absolute encoder: Provides unique position codes that retain position data even when power is lost

Why rotary encoders are great for beginners:

  • Easy to use with just a few pins
  • Provide both rotation direction and button press detection
  • Can rotate continuously without mechanical limits
  • Work great with interrupts for efficient ESP32 C3 Super Mini projects

This tutorial focuses on incremental rotary encoders, the most common type for Arduino and ESP32 projects.

Rotary Encoder Module Pinout

rotary encoder pinout

The rotary encoder module has 5 pins you'll connect to your ESP32 C3 Super Mini:

  • CLK pin (Output A): Primary pulse output that signals rotation - outputs one complete cycle (LOW HIGH LOW) per detent click
  • DT pin (Output B): Secondary pulse output that lags CLK by 90 degrees to determine rotation direction (clockwise or counterclockwise)
  • SW pin: Push button connection - normally open, reads HIGH when not pressed and LOW when pressed (requires pull-up resistor)
  • VCC pin (+): Power supply connection - connect to 3.3V or 5V
  • GND pin: Ground connection - connect to GND (0V)

Rotary Encoder vs Potentiometer

You might confuse the rotary encoder with a potentiometer, but they serve different purposes.

Key differences:

  • Rotation range: Rotary encoders rotate continuously 360° while potentiometers have limited rotation (typically 270°)
  • Output type: Encoders output digital pulses while potentiometers output analog voltage
  • Position tracking: Encoders track relative movement (how much it turned) while potentiometers track absolute position
  • Best use: Encoders excel at tracking rotation amount and direction; potentiometers are better when you need exact position values
  • Modern design: Rotary encoders are the digital evolution of potentiometers with enhanced capabilities

How Rotary Encoder Works

rotary encoder output

Internal mechanism:

  • A slotted disc inside connects to pin C (common ground)
  • Pins A and B make contact with the common ground as the knob rotates
  • The order of contact between A and B determines rotation direction
  • This creates two pulse signals that are 90 degrees out of phase (quadrature encoding)

Detecting rotation direction:

  • Clockwise rotation: Pin A contacts ground before pin B
  • Counterclockwise rotation: Pin B contacts ground before pin A
  • By monitoring pin B's state when pin A changes, the ESP32 C3 Super Mini determines rotation direction
How rotary encoder works

Direction detection logic:

When pin A changes from LOW to HIGH:

  • If pin B is HIGH: Knob turned counter-clockwise
  • If pin B is LOW: Knob turned clockwise

※ NOTE THAT:

Pins A and B connect to CLK and DT pins. Depending on the manufacturer, the order may vary. The code below is tested with DIYables rotary encoders.

How To Program For Rotary Encoder

Basic ESP32 C3 Super Mini encoder reading process:

  • ESP32 reads the signal from the CLK pin continuously
  • When CLK state changes from LOW to HIGH, ESP32 immediately reads the DT pin state
  • If DT pin is HIGH: Counter-clockwise rotation detected → increase counter by 1
  • If DT pin is LOW: Clockwise rotation detected → decrease counter by 1
  • This method tracks both direction and total rotation amount

Wiring Diagram

This diagram shows how to connect the rotary encoder to your ESP32 C3 Super Mini board.

The wiring diagram between ESP32 C3 Super Mini rotary encoder

This image is created using Fritzing. Click to enlarge image

Connection Table:

Rotary Encoder Pin ESP32 C3 Super Mini Pin
CLK D7
DT D6
SW D5
VCC 3.3V
GND GND

ESP32 C3 Super Mini Code – Rotary Encoder

This ESP32 C3 Super Mini code demonstrates basic rotary encoder functionality:

What this code does:

  • Detects rotation direction (clockwise or counterclockwise)
  • Counts rotation clicks by tracking detents
  • Increases counter when rotated clockwise
  • Decreases counter when rotated anticlockwise
  • Detects button presses on the encoder knob
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-rotary-encoder */ #include <ezButton.h> // The library to use for SW pin #define CLK_PIN D7 // The ESP32 C3 SuperMini pin D7 connected to the rotary encoder's CLK pin #define DT_PIN D6 // The ESP32 C3 SuperMini pin D6 connected to the rotary encoder's DT pin #define SW_PIN D5 // The ESP32 C3 SuperMini pin D5 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 7; void setup() { Serial.begin(9600); // 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"); } }

Note: This code uses the ezButton library to simplify button debouncing for reliable button press detection.

Detailed Instructions

  • New to ESP32 C3 Mini? Complete our Getting Started with ESP32 C3 Mini tutorial first to set up your development environment.
  • Install Library: Add the ezButton library through Arduino IDE Library Manager
  • Open Code: Copy the code above and paste it into Arduino IDE
  • Upload: Click the Upload button to transfer code to your ESP32 C3 Super Mini
  • Test Rotation: Turn the encoder knob clockwise several clicks, then counterclockwise
  • Test Button: Press down on the encoder knob
  • View Results: Open Serial Monitor (115200 baud) to see rotation direction and count
  • Pro Tip: Each "click" you feel when rotating is called a detent - the encoder outputs one complete pulse per detent

Serial Monitor Output:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
Rotary Encoder:: direction: CLOCKWISE - count: 1 Rotary Encoder:: direction: CLOCKWISE - count: 2 Rotary Encoder:: direction: CLOCKWISE - count: 3 Rotary Encoder:: direction: CLOCKWISE - count: 4 Rotary Encoder:: direction: CLOCKWISE - count: 5 Rotary Encoder:: direction: ANTICLOCKWISE - count: 4 Rotary Encoder:: direction: ANTICLOCKWISE - count: 3 Rotary Encoder:: direction: ANTICLOCKWISE - count: 2 Rotary Encoder:: direction: ANTICLOCKWISE - count: 1 Rotary Encoder:: direction: ANTICLOCKWISE - count: 0 The button is pressed The button is pressed
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Code Explanation

Review the detailed line-by-line comments in the code above to understand how each section works.

ESP32 C3 Super Mini Code – Rotary Encoder with Interrupt

The polling method in the previous code constantly checks pin states, which wastes ESP32 C3 Super Mini processing power and may miss rotation counts during slow code execution.

Why use interrupts for rotary encoders:

  • Eliminates continuous polling - frees up ESP32 resources
  • Never misses encoder pulses even during other tasks
  • More efficient and reliable for real-world projects
  • Allows ESP32 C3 Super Mini to multitask effectively

The code below uses hardware interrupts to read the rotary encoder direction and position efficiently:

/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-rotary-encoder */ #include <ezButton.h> // The library to use for SW pin #define CLK_PIN D7 // The ESP32 C3 SuperMini pin D7 connected to the rotary encoder's CLK pin #define DT_PIN D6 // The ESP32 C3 SuperMini pin D6 connected to the rotary encoder's DT pin #define SW_PIN D5 // The ESP32 C3 SuperMini pin D5 connected to the rotary encoder's SW pin #define DIRECTION_CW 0 // clockwise direction #define DIRECTION_CCW 1 // counter-clockwise direction volatile int counter = 0; volatile int direction = DIRECTION_CW; volatile unsigned long last_time; // for debouncing int prev_counter; ezButton button(SW_PIN); // create ezButton object for pin 7; void IRAM_ATTR ISR_encoder() { if ((millis() - last_time) < 50) // debounce time is 50ms return; if (digitalRead(DT_PIN) == HIGH) { // The encoder is rotating in counter-clockwise direction => decrease the counter counter--; direction = DIRECTION_CCW; } else { // The encoder is rotating in clockwise direction => increase the counter counter++; direction = DIRECTION_CW; } last_time = millis(); } void setup() { Serial.begin(9600); // Configure encoder pins as inputs pinMode(CLK_PIN, INPUT); pinMode(DT_PIN, INPUT); button.setDebounceTime(50); // set debounce time to 50 milliseconds // use interrupt for CLK pin is enough // call ISR_encoder() when CLK pin changes from LOW to HIGH attachInterrupt(digitalPinToInterrupt(CLK_PIN), ISR_encoder, RISING); } void loop() { button.loop(); // MUST call the loop() function first if (prev_counter != counter) { Serial.print("Rotary Encoder:: direction: "); if (direction == DIRECTION_CW) Serial.print("CLOCKWISE"); else Serial.print("ANTICLOCKWISE"); Serial.print(" - count: "); Serial.println(counter); prev_counter = counter; } if (button.isPressed()) { Serial.println("The button is pressed"); } // TO DO: your other work here }

When you turn the knob, the Serial Monitor displays the same rotation information as the previous example, but with improved performance.

※ NOTE THAT:

  • One interrupt is enough: Many tutorials use two interrupts for one encoder, but this wastes resources - a single interrupt works perfectly
  • Use volatile keyword: Always declare global variables used in interrupts as volatile to prevent unexpected behavior
  • Keep interrupts simple: Minimize code inside interrupt functions - avoid using Serial.print() or Serial.println() in interrupts
  • Fast execution: Interrupt code should execute as quickly as possible for best performance

ESP32 C3 Super Mini Rotary Encoder Application

Rotary encoders open up many exciting project possibilities for your ESP32 C3 Super Mini.

Popular rotary encoder project ideas:

  • Control servo motor position precisely with encoder rotation
  • Adjust LED brightness smoothly by turning the encoder knob
  • Regulate stepper motor speed for robotics projects
  • Create a digital volume control for audio systems
  • Build a menu navigation system for OLED displays
  • Design a smart thermostat with rotary temperature adjustment
  • Make a rotary-controlled RGB LED color picker

Video Tutorial

Watch the video below for a visual walkthrough of this project.

Challenge Yourself

Test your skills by enhancing this ESP32 C3 Super Mini rotary encoder project.

Try these challenges:

  • Easy: Add an LED that blinks when the encoder button is pressed
  • Easy: Display the counter value on a 7-segment display instead of Serial Monitor
  • Medium: Use the encoder to control servo motor angle (map counter to 0-180 degrees)
  • Medium: Create a rotary-controlled LED brightness dimmer with PWM
  • Advanced: Build a menu system on an LCD with encoder navigation and selection

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!