Free wiring guides
Wire the KY-040 rotary encoder to the ESP32-S3
Rotary encoder module, KY-040. A knob that turns forever and clicks when pressed.
Which pin goes where
| On the part | On the ESP32-S3 |
|---|---|
| GND | GND |
| + | 3V3 |
| SW | GPIO 7 |
| DT | GPIO 6 |
| CLK | GPIO 5 |
Wire it with the USB cable unplugged. Follow the names printed on your own part: makers sometimes reorder the pins.
In 3D
Example sketch
// Rotary encoder module, KY-040
// on the ESP32-S3-DevKitC-1 (N8R2 or N16R8)
// https://stem.manusignal.com/en/parts/ky-040-rotary-encoder-esp32-s3
const int PIN_CLK = 5;
const int PIN_DT = 6;
const int PIN_SW = 7;
int count = 0;
int lastClk = HIGH;
void setup() {
Serial.begin(115200);
pinMode(PIN_CLK, INPUT_PULLUP);
pinMode(PIN_DT, INPUT_PULLUP);
pinMode(PIN_SW, INPUT_PULLUP);
lastClk = digitalRead(PIN_CLK);
}
void loop() {
int clk = digitalRead(PIN_CLK);
if (clk != lastClk && clk == LOW) {
count += digitalRead(PIN_DT) == HIGH ? 1 : -1;
Serial.println(count);
}
lastClk = clk;
if (digitalRead(PIN_SW) == LOW) {
Serial.println("pressed");
delay(250);
}
delay(1);
}
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
This sketch is compiled for the board before the page is published. It has not been run on real hardware by us.
What you should see
Open the Serial Monitor at 115200. The count goes up one way and down the other, and “pressed” prints when you push the knob.
If it does not work
- Turning the knob counts the wrong way.
- Swap the CLK and DT wires.
Build something with it
These Build Packs use this part on this board, with the same pins.
- Interval timerWork, rest, repeat. Turn the knob to set the work minutes and press it to start; it beeps when each part ends.
- Macro pad with a knobTwelve keys that fire shortcuts on your computer such as copy, paste and undo, and a knob for the volume.
- Smart knobA knob with a round screen in the middle and a ring of light round it. Turn to set brightness, press to switch to colour.
More parts on the ESP32-S3
- ST7789 1.54 in TFT screenESP32-S3 · 7 wires
- ST7789 2.0 in TFT screenESP32-S3 · 7 wires
- GC9A01 round screenESP32-S3 · 7 wires
- SSD1306 OLED 0.96 in screenESP32-S3 · 4 wires
- SSD1306 OLED 0.91 in screenESP32-S3 · 4 wires
- 4.2 in e-paper screenESP32-S3 · 8 wires
- TTP223 touch sensorESP32-S3 · 3 wires
- Push buttonESP32-S3 · 2 wires
- Foot switchESP32-S3 · 2 wires
- Slide switchESP32-S3 · 2 wires
- SW-520D tilt switchESP32-S3 · 2 wires
- MC-38 door switchESP32-S3 · 2 wires
- AM312 PIR motion sensorESP32-S3 · 3 wires
- HC-SR04 ultrasonic sensorESP32-S3 · 4 wires
- Capacitive soil moisture sensorESP32-S3 · 3 wires
- BME280 sensorESP32-S3 · 4 wires
- MLX90614 infrared thermometerESP32-S3 · 4 wires
- SGP30 air quality sensorESP32-S3 · 4 wires
- HX711 load cellESP32-S3 · 4 wires
- 3×4 keypadESP32-S3 · 7 wires
- Passive buzzerESP32-S3 · 2 wires
- WS2812 LED ringESP32-S3 · 3 wires
- WS2812 LED stickESP32-S3 · 3 wires
- WS2812B LED stripESP32-S3 · 3 wires
- KY-016 RGB LEDESP32-S3 · 4 wires
- 2-channel relay moduleESP32-S3 · 4 wires
- 28BYJ-48 stepper motor (ULN2003)ESP32-S3 · 6 wires
- MAX98357A I2S amplifierESP32-S3 · 5 wires
- SX1262 LoRa module (Ra-01SH)ESP32-S3 · 9 wires