Please help me get started with rotary encoders

You can think of a rotary encoder like two buttons. If the encoder is turned one position to the right it is exactly as if button A is pressed, for the left the same applies to button B.
It is therefore important to first determine in which direction the encoder is turned.
For an Arduino pro micro the code is like this:
int aState = LOW;
int aLastState = LOW;

int ScanRotary (int encoder0PinA, int encoder0PinB) {
int rotation = 0;

aState = encoder0PinA;
if ((aLastState == HIGH) && (aState == LOW)) {
if (encoder0PinB == HIGH)
rotation = +1;
rotation = -1;
aLastState = aState;
return rotation;

In the loop:
int rotation = ScanRotary (value [7], value [15]);
if (rotation! = 0) {
if (rotation <0) {
SetJoystickButton (PIR_Scan_Left);
else {
SetJoystickButton (PIR_Scan_Right);
Serial.print (“Rotary:”); Serial.println (rotation);

Ask if you didn’t understand something.

I had already programmed a solution with 5 encoders and 20 buttons and then came across the Xtouch mini from Behringer. Together with Lorby’s software, it’s perfect.
Take a look at

1 Like