GyverEncoder: an example that runs
Yes. You can use the GyverEncoder library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Rotary encoder, KY-040). The example below, "Turns, clicks and holds with GyverEncoder", runs on the Arduino Uno R3.
Built in on Mokxi: #include <GyverEncoder.h> and it builds, with nothing to install. Free, and no account needed.
What GyverEncoder does
A rotary encoder and its button polled with tick(): turns, turns with the knob held, fast turns, clicks, double clicks and holds. That is what GyverEncoder, by AlexGyver, covers. The sketch below uses it on the Arduino Uno: “Turns, clicks and holds with GyverEncoder”.
The interface of AlexGyver's "GyverEncoder" library (4.10): a rotary encoder and its push switch, polled with tick(): turns, turns with the knob held, fast turns, clicks, double clicks and holds.
Use the rotary part (a KY-040): CLK, DT and SW to three pins, all with the pull-up on (HIGH_PULL, the default). A KY-040 detent is a whole cycle of the two signals, which is TYPE2: one step a detent, counted when both contacts open again.
The calls follow release 4.10.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.
| Library Manager name | GyverEncoder |
|---|---|
| Author | AlexGyver |
| Include | #include <GyverEncoder.h> |
| Boards | every board Mokxi compiles for |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Rotary encoder, KY-040 |
Install it
It builds on every board Mokxi compiles for.
In Mokxi it is built in: #include <GyverEncoder.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "GyverEncoder" by AlexGyver from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add GyverEncoder to lib_deps in PlatformIO. Mokxi follows the calls of release 4.10.0, so a sketch written here builds there too.
What to know in the simulator: Only upstream’s default BINARY_ALGORITHM is here. Its class is called Encoder, as is Encoder.h’s: a sketch includes one header or the other.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Encoder enc1(CLK, DT, SW)
Makes enc1, the Encoder object the calls below are made on.
enc1.setType(TYPE2)
Sets the type to the value you pass in.
enc1.isRight()
Returns true or false for "is right".
enc1.isLeft()
Returns true or false for "is left".
enc1.isRightH()
Returns true or false for "is right h".
enc1.isLeftH()
Returns true or false for "is left h".
enc1.isClick()
Returns true or false for "is click".
enc1.isSingle()
Returns true or false for "is single".
enc1.isDouble()
Returns true or false for "is double".
Example: Turns, clicks and holds with GyverEncoder
On the Arduino Uno R3. It runs in the browser exactly as printed.
#define CLK 2
#define DT 3
#define SW 4
#include "GyverEncoder.h"
Encoder enc1(CLK, DT, SW);
void setup() {
Serial.begin(9600);
enc1.setType(TYPE2); // a KY-040: one step a detent
Serial.println("ready");
}
void loop() {
enc1.tick();
if (enc1.isRight()) Serial.println("Right");
if (enc1.isLeft()) Serial.println("Left");
if (enc1.isRightH()) Serial.println("Right holded");
if (enc1.isLeftH()) Serial.println("Left holded");
if (enc1.isClick()) Serial.println("Click");
if (enc1.isSingle()) Serial.println("Single");
if (enc1.isDouble()) Serial.println("Double");
if (enc1.isHolded()) Serial.println("Holded");
}Wiring
- Arduino Uno R3 2 to Rotary encoder, KY-040 CLK
- Arduino Uno R3 3 to Rotary encoder, KY-040 DT
- Arduino Uno R3 4 to Rotary encoder, KY-040 SW
- Arduino Uno R3 5V to Rotary encoder, KY-040 VCC
- Arduino Uno R3 GND to Rotary encoder, KY-040 GND
Try GyverEncoder without installing anything
Open the example, change the sketch, and keep your version in a free account.