In embedded systems, encoders are common sensors used to measure angles, distances, or speeds. The pulse counter (PCNT) module integrated into the ESP32 chip provides a hardware-accelerated encoder interface that can efficiently process signals from encoders. The ESP32Encoder library leverages this feature, offering users a simple and easy-to-use API that supports up to 8 encoders, facilitating rapid project development.
Introduction to ESP32Encoder
The ESP32Encoder library is developed based on the pulse counter (PCNT) module of the ESP32 chip, allowing for easy processing of signals from encoders. This library provides a simple API that supports various encoder modes, including full quadrature, half quadrature, and single-ended counting modes.

Advantages
-
• High Efficiency: By utilizing the hardware pulse counter of the ESP32, the ESP32Encoder library can efficiently process encoder signals and provide accurate counting information.
-
• Low Power Consumption: Due to the use of hardware counters, the ESP32Encoder library consumes minimal CPU resources, helping to reduce system power consumption.
-
• Multi-Encoder Support: The ESP32Encoder library supports up to 8 encoders, making it convenient for users to handle multiple encoder data on a single ESP32 chip.
-
• Easy to Use: This library provides a simple API, allowing users to get started quickly.
Application Scenarios
The ESP32Encoder library is suitable for various applications that require encoders, such as:
-
• Robot Control: Controlling the joint movements of robots.
-
• Motor Control: Measuring motor speed and position.
-
• Human-Machine Interaction: Designing rotary encoders to control system parameters.
-
• Data Acquisition: Collecting data from sensors.
Main Features
-
• Supports full quadrature, half quadrature, and single-ended counting modes.
-
• Provides configuration options such as internal weak pull-up/down resistors and interrupt service routine CPU core selection.
-
• Supports hardware debouncing to reduce noise interference.
Usage Instructions
Using the ESP32Encoder library is very simple, and here are the main steps:
-
1. Install the Library: Install the ESP32Encoder library using the ESP-IDF or Arduino package manager.
-
2. Create an Encoder Object: Use the
<span>ESP32Encoder</span>class to create an encoder object, specifying the encoder’s pins and mode. -
3. Set Configuration: Use the
<span>setFilter()</span>function to set the hardware debouncing filter, use the<span>useInternalWeakPullResistors()</span>function to set internal weak pull-up/down resistors, and use the<span>isrServiceCpuCore()</span>function to set the interrupt service routine CPU core. -
4. Get Counting Information: Use the
<span>getCount()</span>function to retrieve the current counting information from the encoder.
Code Example
#include <Arduino.h>
#include "ESP32Encoder.h"
// Initialize encoder object
ESP32Encoder encoder;
void setup() {
// Initialize serial port
Serial.begin(115200);
// Connect encoder to pins
encoder.attach(25,26,true,false); // Use internal weak pull-up resistor
// Set hardware debouncing filter
encoder.setFilter(1023);
// Print counting information
Serial.println("Encoder count: "+String(encoder.getCount()));
}
void loop() {
// Get encoder counting information
int32_t count = encoder.getCount();
// Print counting information
Serial.println("Encoder count: "+String(count));
// Delay 100 milliseconds
delay(100);
}
Important Details
-
• The number of encoders supported by the ESP32Encoder library is limited by the hardware of the ESP32 chip. The ESP32 and ESP32C2 support 8 encoders, the ESP32S3 supports 2 encoders, and the ESP32C3 does not support hardware-accelerated encoders.
-
• For certain types of encoders, such as the KY-040 encoder, it is necessary to add external debounce capacitors (0.1-2uF) on the encoder signal lines to reduce mechanical jitter.
-
• You can specify the CPU core on which the interrupt service routine runs by setting the
<span>isrServiceCpuCore()</span>function to avoid inaccuracies in counting information due to multiple encoders sharing interrupts.
Conclusion
The ESP32Encoder library is a powerful and easy-to-use library that helps users quickly implement encoder applications based on the ESP32. By utilizing the hardware-accelerated pulse counter, the ESP32Encoder library can provide efficient and stable encoder data processing, offering reliable solutions for various embedded systems.
Project Address: https://github.com/madhephaestus/ESP32Encoder