The Noiasca Rotary Encoder Library

Arduino rotary encoder library

The Noiasca Rotary Encoder Libary is a lightweight encoder library. This library doesn't use any interupts and you can use it on any two input pins you have available on your Arduino.

A very simple Arduino sketch will look like following:

# include <NoiascaEncoder.h>           // download library from https://werner.rothschopf.net/
Encoder myEnc(2, 3);                   // Change these two numbers to the pins connected to your encoder.

void setup() {
  Serial.begin(115200);
  Serial.println(F("Basic Encoder Test:"));
  myEnc.begin();
}

long oldPosition  = -42;

void loop() {
  long newPosition = myEnc.getCounter();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

 

The default constructor takes two pins.

Additionally you can define two callback methods which will be called when the encoder will be moved.

Encoder myEnc(2, 3, callbackIncrease, callbackDecrease);

Don't forget to implement the callback functions in your user sketch!

.begin() : The begin() must be called in setup(). It will do all hardware specific tasks.

.getCounter() returns the internal counter.

.getDirection() returns 1 or -1 if the encoder was moved clockwise or counter clockwise..

.setCounter() can be used to set the internal counter (a signed 32 bit variable) to a specific value

.upClick() gets true if rotary encoder is moved clockwise.

.downClick() gets true if rotary encoder is moved counter clockwise.

.encode() is the internal "run" method. If you only use the defined callbacks, call encode() in your loop(). If you read events with getCounter(), getDirection(), upClick() or downClick() the encode() function will be called in the background.

Disclaimer

This encoder library is just a fast prototype - use it with caution. As the library is not using any interrupts it relies on a non blocking loop(). Don't use delay() in your user sketch!

Links

(*) Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you I will earn a (little) comission if you click through and make a purchase. I only recommend products I own myself and I'm convinced they are useful for other makers.

History

First upload: 2022-02-12 | Version: 2022-02-13