ESP32 / Rotary Encoder direction

I’m in the process of migrating from Arduino microcontrollers to ESP32’s. I have an app written for the Mega 2560 that I want to port over to the ESP32. The app utilizes a rotary encoder for menu navigation and value selection. I found an example routine using the “ESP32RotaryEncoder.h” library. The routine displays a value that’s incremented/decremented when the encoder is turned. What I need is a simple “left / idle / right” value. I found in the “ESP32RotaryEncoder.h” the following:

typedef enum {

    LEFT  = -1,

    STILL =  0,

    RIGHT =  1

} Rotation;

These values would work perfectly, but I’m not versed well enough in digging through the libraries to know how to get these values.

Is there a good reference on how to work with libraries and get what’s needed?

Thanks!

Take a close look, if you use the edge of one of the pins and look at the other it will be high for one direction low for the other. If you want it the other way just swap pins.

I spent several hours trying to get the rotary encoder function I wrote for the Mega 2560 (which works excellently!) to work on the ESP32, but I’m guessing the much higher clock speed of the ESP32 contributes heavily to the problems I encountered. I totally understand how the encoder works. These encoders are anything but high quality, so the contacts are behaving glitchy.

The example sketch I found that uses the “ESP32RotaryEncoder.h” library is pretty much rock solid. I rarely see any glitch at all. It’s just that the value returned in the example sketch returns a numeric value that’s incremented/decremented when the knob is turned. What I need is a value that shows that it was turned “left” or “right”. I found what appears to be what I need in the library, but I need help in learning how to get it to return the “-1, 0, 1” values.

Create a variable in your code of type Rotation. If the typedef you posted is public that is all you need.

The typedef, as the word spells, defines a type, Rotation in this case. Hence, an idea is to seek a variable of type Rotation in the library.

Something as:

Rotation rotationState ;

In this example, rotationState could have the information you need.

The use would be something like

if ( rotationState == LEFT ) { … }
else if ( rotationState == STILL ) { … }
else ( rotationState == RIGHT ) { … }

Or even a switch/case statement:

switch( rotationState )
{
   case LEFT: { … } break ;
   case STILL: { … } break ;
   case RIGHT: { … } break ;
   default: { … } break ;
}

Did you look at the LeftOrRight example included with the library?

I didn't see the enum you mentioned anywhere in the library code.

Well poop! I did see the “LeftOrRight” example yesterday, but when I tried it, everything was flaking out when I attempted to turn the encoder. I discovered today that because I’m using a bare encoder (with no circuit board, pull up resisters, etc.), I needed to change:

“rotaryEncoder.setEncoderType( EncoderType::HAS_PULLUP );”

to

“rotaryEncoder.setEncoderType( EncoderType::FLOATING );”

and that resolved the flakiness. At the point, the LeftOrRight example had completely flown from my head!

It’s working! Thanks again guys…

Anyone have an explanation for this line in the library:

* @param encoderPinVcc Optional; the voltage reference input, could be marked "+" or "V+" or "VCC"; defaults to -1, which is ignored

Why does a rotary encoder need a voltage reference if it’s only generating “high” and “low” values?

Just curious… Thanks…

Edit: Found the answer in a different rotary encoder library. Apparently modules are sometimes powered using unused GPIO’s rather than from the 3.3 or 5 volt pins.

It's in the ESP32RotaryEncoder.h file, line 238.

typedef enum {
LEFT = -1,
STILL = 0,
RIGHT = 1
} Rotation;