ESP32Encoder: An Efficient and Accurate Library for Reading Rotary Encoders in IoT and Embedded Projects

This article will provide a detailed introduction to the ESP32Encoder library, a software library that utilizes the ESP32 pulse counter hardware peripheral to efficiently read rotary encoder signals. It has many advantages such as high efficiency, low interrupt load, and multi-channel support, making it an ideal choice for handling rotary encoders in IoT and embedded projects.

ESP32Encoder: An Efficient and Accurate Library for Reading Rotary Encoders in IoT and Embedded Projects

What is ESP32Encoder?

ESP32Encoder is an open-source library for the ESP32 microcontroller that uses the built-in pulse counter (PCNT) hardware peripheral of the ESP32 to read rotary encoder signals. Compared to software polling, this library uses hardware interrupt processing, greatly reducing CPU load and improving reading accuracy and efficiency, making it particularly suitable for applications that require real-time processing of rotary encoder data.

ESP32Encoder: An Efficient and Accurate Library for Reading Rotary Encoders in IoT and Embedded Projects

What can ESP32Encoder do?

ESP32Encoder is mainly used to read various types of rotary encoders, such as incremental encoders, and convert them into digital signals for subsequent processing by the microcontroller. This is crucial in many applications, such as:

  • Robot Control: Precisely control the joint angles, speeds, and positions of robots.

  • Industrial Automation: Monitor and control the operating status of various mechanical equipment.

  • Instrumentation: Read values from knobs or dials.

  • User Interface: As a means of human-computer interaction, such as volume adjustment, menu selection, etc.

  • ESP32Encoder: An Efficient and Accurate Library for Reading Rotary Encoders in IoT and Embedded Projects

Core Functions and Features of ESP32Encoder:

  • Hardware Acceleration: Utilizes the PCNT hardware peripheral of the ESP32 for counting, greatly reducing software processing burden and improving efficiency and accuracy.

  • Low Interrupt Load: Only one interrupt is used for handling counter overflow, greatly reducing CPU overhead for interrupt processing and ensuring stable system operation.

  • Multi-Channel Support: Supports up to 8 simultaneously running encoders (ESP32 and ESP32C2), with ESP32S3 supporting 2.

  • Multiple Counting Modes: Supports full quadrature counting, half quadrature counting, and single edge counting modes to accommodate different types of encoders and application needs.

  • Internal Weak Pull-Up/Pull-Down Resistors: Internal weak pull-up/pull-down resistors can be configured to simplify circuit design.

  • Configurable Interrupt Service CPU Core: Allows users to specify the CPU core on which the interrupt service routine runs to avoid concurrency issues.

  • Hardware Debouncing: Provides hardware debouncing functionality to effectively filter out noise from encoder signals. For encoders like KY-040 that are prone to jitter, external capacitors need to be used for debouncing, along with appropriate filtering parameters.

  • Easy to Use: Provides a simple API interface for users to quickly integrate into their projects.

  • Good Documentation: Offers detailed documentation generated by Doxygen for user reference and learning.

Compatibility:

  • • Supports ESP32 and ESP32-C2 chips.

  • • ESP32-C3 is not supported because this chip does not have a pulse counter hardware.

  • • ESP32-S3 only has two PCNT modules, thus only supports two hardware-accelerated encoders.

Usage Example (Simplified):

#include <ESP32Encoder.h>

ESP32Encoder encoder1(2, 4);// Connect encoder using GPIO 2 and 4

void setup() {
Serial.begin(115200);
  encoder1.setFilter(1023);// Set maximum hardware debouncing
  encoder1.setCount(0);// Set initial count to 0
}

void loop() {
long count = encoder1.getCount();
Serial.print("Encoder Count: ");
Serial.println(count);
delay(100);
}

Notes on KY-040 and Similar Encoders:

Switching encoders like KY-040 are prone to significant signal jitter and require external capacitors for debouncing (typically between 0.1uF and 2uF). Additionally, when using the ESP32Encoder library, it is necessary to set setFilter(1023) to achieve maximum hardware debouncing effect.

Conclusion:

The ESP32Encoder library is an efficient, easy-to-use, and powerful rotary encoder reading library that fully utilizes the hardware resources of the ESP32, greatly simplifying the use of encoders and improving system performance. Whether for simple applications or complex projects, ESP32Encoder can provide a reliable solution for reading rotary encoders.

Project Address: https://github.com/madhephaestus/ESP32Encoder

Leave a Comment

×