Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
E

ESPRotary

> 编程语言
Open source

Arduino/ESP library to simplify reading rotary encoder data.

188 stars0 likes0 views
WebsiteGitHub

About

Arduino/ESP library to simplify reading rotary encoder data.

ESPRotary

Arduino/ESP library to simplify reading rotary encoder data.

  • Author: Lennart Hennigs ()
  • Copyright (C) 2017-2026 Lennart Hennigs.
  • Released under the MIT license.

Description

This library allows you read out interactions with a rotary encoder and act on them. It uses callback functions to be notified when the rotary encoder changes.

It has been tested with Arduino, ESP8266 and ESP32 devices.

To see the latest changes to the library please take a look at the Changelog.

If you find this library helpful please consider giving it a ⭐️ at GitHub and/or buy me a ☕️. Thanks!

Some of the code based of this library is based on code from PJRC.

How to Use

Definition

  • Use the begin() or the parameterized constructor to create a new instance of the class
  • Encoder produce different numbers of "click" on a single turn.
  • You can specify the number of clicks in the constructor, or via a setter function
    • void setStepsPerClick(int steps)
    • int getStepsPerClick() const

Callback Handlers

  • The library provides several callback handlers to detect events
    • void setChangedHandler(CallbackFunction f)
    • void setRightRotationHandler(CallbackFunction f)
    • void setLeftRotationHandler(CallbackFunction f)
    • void setUpperOverflowHandler(CallbackFunction f)
    • void setLowerOverflowHandler(CallbackFunction f)
  • The library does not detect button clicks. You have to use a separate library for this, e.g. my Button2 library.

Ranges

  • In the constructor you can define an upper and a lower threshold. The encoder value will not be bypass these values.
  • There are also getter and setter functions the these values
    • void setUpperBound(int upper_bound)
    • void setLowerBound(int lower_bound)
    • int getUpperBound() const
    • int getLowerBound() const
  • See the RangedCounter example for details.

Retrigger (out of bound) events?

  • If you want that out of bound events are only triggered once a bound is reached, you can set:
    • retriggerEvent(false)
  • Otherwise each (out of bounds) turn will re-trigger the event.
  • If you want to disable rotate events that would go beyond your defined boundary you can use:
    • void triggerOnBounds(false)

Reading out information

  • The class allows you the get the position and the direction after a click using these function:
    • int getPosition() const
    • byte getDirection() const
    • String directionToString(byte direction) const

Speed

  • You can define the speed, i.e. the increment the a single click in the constructor
  • There is also a getter and setter function for this
    • void setIncrement(int inc)
    • int getIncrement() const

Speedup Mode

  • You can also define, that the increment shall change when the encoder is turned fast.
  • Use these functions to define the interval (between clicks) and the increment:
    • void setSpeedupInterval(int time)
    • void setSpeedupIncrement(int inc)
  • Per default the interval is set to 75ms and the increment is set to 5.
  • There are also getter functions for both parameters:
    • int getSpeedupInterval() const
    • int getSpeedupIncrement() const
  • To enable the speedup mode use this member function:
    • void enableSpeedup(bool enable)
  • ...and to check:
    • bool isSpeedupEnabled() const
  • There are also event handlers that let you track if the speedup mode was entered or exited:
    • void setSpeedupStartedHandler(CallbackFunction f)
    • void setSpeedupEndedHandler(CallbackFunction f)
  • Alternatively, you can use this function to determine its state:
    • bool isInSpeedup() const
  • If you have bound defined, the speedup mode will be automatically ended when you hit it.

The Loop

  • For the class to work, you need to call the loop() member function in your sketch's loop() function.
  • Alternatively, you can use a timer interrupt to call the member loop()function.
  • See the examples for more details.

Using it with a timer interrupt

  • Instead of using the main loop() of your sketch you can also use an interrupt timer function.
  • See ESP32Interrupt or ESP8266Interrupt to see how

IDs for Encoder Instances

  • Each encoder instance gets a unique (auto incremented) ID upon creation.
  • You can get a encoders' ID via getID().
  • Alternatively, you can use setID(int newID) to set a new one. But then you need to make sure that they are unique.
  • You can also use the == operator to compare encoders

Context

  • The callback handlers are plain C function pointers, so they cannot capture surrounding state (no lambdas with captures).
  • To give a handler access to custom data — most commonly a pointer to the object that owns the encoder — attach it as the encoder's context:
    • void setContext(void* ctx) stores a void* on the encoder instance.
    • void* getContext() const returns it.
  • Inside a handler you retrieve the context via the passed ESPRotary& and cast it back:
cpp
class Dial {
 public:
  void begin(byte p1, byte p2) {
    r.begin(p1, p2, 4);
    r.setContext(this);                 // hand the encoder a pointer back to us
    r.setChangedHandler(Dial::onRotate);
  }
  void loop() { r.loop(); }

  static void onRotate(ESPRotary& sender) {
    Dial* self = static_cast(sender.getContext());
    self->handle(sender.getPosition());
  }
 private:
  void handle(int pos) { /* ... */ }
  ESPRotary r;
};
  • See the Context example.

Notes

  • To see the latest changes to the library please take a look at the Changelog.
  • And if you find this library helpful, please consider giving it a star at GitHub. Thanks!

Examples

  • SimpleCounter - basic example
  • SimpleCounterWithButton - basic example with a button handler
  • RangedCounter - shows how to define ranges
  • Speedup - shows how to implement the speedup mode
  • Context - shows how to pass a context object to the callbacks
  • ESP8266Interrupt - uses an ESP8266 interrupt instead of the main loop
  • ESP32Interrupt - uses an ESP32 interrupt instead of the main loop

Class Definition

These are the constructor and the member functions the library provides:

…

Installation

Open the Arduino IDE choose "Sketch > Include Library" and search for "ESP Rotary". Or download the ZIP archive (), and choose "Sketch > Include Library > Add .ZIP Library..." and select the downloaded file.

License

MIT License

Copyright (c) 2017-2026 Lennart Hennigs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++arduinoarduino-libraryc-plus-plusembedded

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言