https://github.com/dmachard/ky040-rotary
KY-040 rotary encoder library for arduino and esp8266 devices
https://github.com/dmachard/ky040-rotary
arduino esp32 esp8266 iot
Last synced: 4 months ago
JSON representation
KY-040 rotary encoder library for arduino and esp8266 devices
- Host: GitHub
- URL: https://github.com/dmachard/ky040-rotary
- Owner: dmachard
- License: mit
- Created: 2017-09-17T19:12:33.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2026-02-13T17:47:16.000Z (4 months ago)
- Last Synced: 2026-02-14T01:29:10.935Z (4 months ago)
- Topics: arduino, esp32, esp8266, iot
- Language: C++
- Homepage:
- Size: 676 KB
- Stars: 24
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HW-040/KY-040 Rotary Encoder Library
Arduino library for the KY-040 rotary encoder with **software debouncing** and **interrupts** support.

## Features
- Callback functions on click and rotate events
- Software debouncing
- Interrupts support
- Compatible with Arduino, ESP8266, and ESP32
## Tested on hardware
- Arduino Uno
- Wemos D1 R2
- ESP32
## Wiring
| KY-040 Pin | Board Pin |
|------------|-----------|
| VCC | 3.3V |
| GND | GND |
| SW | D7 |
| DT | D6 |
| CLK | D5 |
## Usage
### Basic mode (polling)
```cpp
#include "KY040-rotary.h"
KY040 Rotary(5, 6, 7); // CLK, DT, SW
void OnButtonClicked() { Serial.println("Clicked"); }
void OnButtonLeft() { Serial.println("Left"); }
void OnButtonRight() { Serial.println("Right"); }
void setup() {
Serial.begin(9600);
Rotary.Begin();
Rotary.OnButtonClicked(OnButtonClicked);
Rotary.OnButtonLeft(OnButtonLeft);
Rotary.OnButtonRight(OnButtonRight);
}
void loop() {
Rotary.Process(millis());
}
```
### Interrupt mode
```cpp
#include "KY040-rotary.h"
KY040 Rotary(5, 6, 7);
void OnButtonClicked() { Serial.println("Clicked"); }
void OnButtonLeft() { Serial.println("Left"); }
void OnButtonRight() { Serial.println("Right"); }
void SwitchInterruptHandler() { Rotary.HandleSwitchInterrupt(); }
void RotateInterruptHandler() { Rotary.HandleRotateInterrupt(); }
void setup() {
Serial.begin(9600);
Rotary.Begin(SwitchInterruptHandler, RotateInterruptHandler);
Rotary.OnButtonClicked(OnButtonClicked);
Rotary.OnButtonLeft(OnButtonLeft);
Rotary.OnButtonRight(OnButtonRight);
}
void loop() {
Rotary.Process(millis());
}
```
## API Reference
### Constructor
```cpp
KY040(uint8_t pinClk, uint8_t pinDt, uint8_t pinSw)
```
### Methods
| Method | Description |
|--------|-------------|
| `Begin()` | Initialize in polling mode |
| `Begin(isr sw, isr rotate)` | Initialize in interrupt mode |
| `Process(unsigned long t)` | Call in `loop()` with `millis()` to process events |
| `OnButtonClicked(callback)` | Register callback for button click |
| `OnButtonLeft(callback)` | Register callback for counter-clockwise rotation |
| `OnButtonRight(callback)` | Register callback for clockwise rotation |
| `HandleSwitchInterrupt()` | ISR handler for button — call from your ISR wrapper |
| `HandleRotateInterrupt()` | ISR handler for rotation — call from your ISR wrapper |
## License
MIT License — see [LICENSE](LICENSE) for details.