ESP32 - Rotary Encoder Servo Motor

In this tutorial, We are going to learn how to program ESP32 and the rotary encoder to control the angle of the servo motor.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Servo Motor
1×Rotary Encoder
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 Servo Motor and Rotary Encoder

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

Wiring Diagram

ESP32 Rotary Encoder Servo Motor 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-servo-motor */ #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 SERVO_PIN 33 // ESP32 pin GPIO33 connected to the servo motor #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 angle = 90; Servo servo; // create servo object to control a servo 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); servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); } 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--; angle -= 2; // you can change this resolution } else { // the encoder is rotating in clockwise direction => increase the counter direction = DIRECTION_CW; counter++; angle += 2; // you can change this resolution } if (angle < 0) angle = 0; else if (angle > 180) angle = 180; // sets the servo angle according to the counter servo.write(angle); Serial.print("COUNTER: "); Serial.print(counter); Serial.print(" | ANGLE: "); Serial.println(angle); } // 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
Arduino IDE Upload Code
  • Open Serial Monitor
  • Rotate the rotary encoder
  • See the servo motor's rotation
  • See the result on Serial Monitor
COM6
Send
COUNTER: 0 | ANGLE: 90 COUNTER: 1 | ANGLE: 92 COUNTER: 2 | ANGLE: 94 COUNTER: 3 | ANGLE: 96 COUNTER: 4 | ANGLE: 98 COUNTER: 5 | ANGLE: 100 COUNTER: 6 | ANGLE: 102
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