ESP32 - Rotary Encoder LED

by DIYables in Circuits > Microcontrollers

7 Views, 0 Favorites, 0 Comments

ESP32 - Rotary Encoder LED

esp32-rotary-encoder-led-cover.jpg

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-rotary-encoder-led

This build uses a rotary encoder to control the brightness of an LED on an ESP32. Turning the encoder clockwise increases brightness, turning it counter-clockwise decreases it. The sketch reads the encoder's CLK and DT pins to determine direction and adjusts the LED's PWM output accordingly.

Supplies

Wiring

esp32-rotary-encoder-led-wiring-diagram.jpg

Connect the rotary encoder's CLK pin to ESP32 GPIO25, DT pin to GPIO26, and SW pin to GPIO27. Connect the LED to GPIO22 through a 220Ω resistor. Build the circuit on a breadboard, and power the ESP32 and encoder as needed.

How It Works

The ESP32 continuously reads the CLK pin of the rotary encoder. When CLK changes from LOW to HIGH, the code checks the DT pin to determine rotation direction: if DT is HIGH, the encoder turned counter-clockwise and brightness decreases; otherwise it turned clockwise and brightness increases. The brightness value is clamped between 0 and 255, then written to the LED pin with analogWrite so the LED dims or brightens as you turn the knob.

Code

The sketch defines pins for CLK, DT, SW and the LED, and keeps track of a brightness variable starting at 125. In the main loop it detects the rising edge of the CLK signal, updates a counter and the brightness value based on direction, keeps brightness within the 0-255 range, applies it to the LED with analogWrite, and prints the counter and brightness to the serial monitor for debugging.

esp32-rotary-encoder-led.ino - the sketch from the tutorial, unchanged.

/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rotary-encoder-led
*/

#include <ESP32Servo.h>

#define CLK_PIN 25 // ESP32 pin GPIO25 connected to the rotary encoder's CLK pin
#define DT_PIN 26 // ESP32 pin GPIO26 connected to the rotary encoder's DT pin
#define SW_PIN 27 // ESP32 pin GPIO27 connected to the rotary encoder's SW pin
#define LED_PIN 22 // ESP32 pin GPIO22 connected to the LED

#define DIRECTION_CW 0 // clockwise direction
#define DIRECTION_CCW 1 // counter-clockwise direction

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
int brightness = 125; // middle value

void setup() {
Serial.begin(9600);

// configure encoder pins as inputs
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);

// read the initial state of the rotary encoder's CLK pin
prev_CLK_state = digitalRead(CLK_PIN);
pinMode(LED_PIN, OUTPUT);
}

void loop() {

// read the current state of the rotary encoder's CLK pin
CLK_state = digitalRead(CLK_PIN);

// If the state of CLK is changed, then pulse occurred
// React to only the rising edge (from LOW to HIGH) to avoid double count
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
// if the DT state is HIGH
// the encoder is rotating in counter-clockwise direction => decrease the counter
if (digitalRead(DT_PIN) == HIGH) {
direction = DIRECTION_CCW;
counter--;
brightness -= 10; // you can change this value
} else {
// the encoder is rotating in clockwise direction => increase the counter
direction = DIRECTION_CW;
counter++;
brightness += 10; // you can change this value
}

if (brightness < 0)
brightness = 0;
else if (brightness > 255)
brightness = 255;

// sets the brightness of LED according to the counter
analogWrite(LED_PIN, brightness);

Serial.print("COUNTER: ");
Serial.print(counter);
Serial.print(" | BRIGHTNESS: ");
Serial.println(brightness);
}

// save last CLK state
prev_CLK_state = CLK_state;
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-rotary-encoder-led