ESP32 - Rotary Encoder LED

In this tutorial, We are going to learn how to program ESP32 to control the brightness of a LED according to the rotary encoder's output value.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Rotary Encoder
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to LED and Rotary Encoder

Unfamiliar with LED and rotary encoder, including their pinouts, functionality, and programming? Explore comprehensive tutorials on these topics below:

Wiring Diagram

ESP32 Rotary Encoder LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

/* * 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 <Servo.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; }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect ESP32 to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Open Serial Monitor
  • Rotate the rotary encoder
  • See the LED's brightness
  • See the result on Serial Monitor
COM6
Send
COUNTER: 1 | BRIGHTNESS: 135 COUNTER: 2 | BRIGHTNESS: 145 COUNTER: 3 | BRIGHTNESS: 155 COUNTER: 4 | BRIGHTNESS: 165 COUNTER: 5 | BRIGHTNESS: 175 COUNTER: 6 | BRIGHTNESS: 185 COUNTER: 7 | BRIGHTNESS: 195
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Read the line-by-line explanation in comment lines of source code!

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES