DEV Community

Cover image for ESP32 - Rotary Encoder Servo Motor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at esp32io.com

ESP32 - Rotary Encoder Servo Motor

This build lets you turn a rotary encoder knob and watch a servo motor sweep to match it. It is a good first project for connecting a physical input to a physical output through an ESP32, without needing a motor driver since a servo takes a simple PWM-like signal directly.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 - Rotary Encoder Servo Motor

What you need

Wiring

ESP32 - Rotary Encoder Servo Motor wiring diagram

Connect the rotary encoder's CLK pin to GPIO25, DT pin to GPIO26, and SW pin to GPIO27 on the ESP32. The servo motor's signal wire goes to GPIO33. If you plan to drive several servos at once, it is worth using a PCA9685 16-channel PWM driver module instead, since it saves MCU pins and keeps the wiring simpler. Check the referenced power tutorial if you are unsure how to supply power to the ESP32 and the servo properly.

How it works

A rotary encoder has two output pins, CLK and DT, that pulse as you turn the knob. By reading which one changes first, the code figures out whether you turned clockwise or counter-clockwise. Each pulse nudges a counter and an angle value up or down by 2 degrees. That angle is sent to the servo, which moves its shaft to match, so turning the knob one way sweeps the servo one direction and turning it back sweeps it the other way, clamped between 0 and 180 degrees.

Code

The sketch uses the ESP32Servo library to control the servo through servo.attach() and servo.write(). In setup(), the encoder pins are set as inputs and the servo starts centered at 90 degrees. In loop(), the code watches for the CLK pin's rising edge, checks the DT pin to decide direction, adjusts the angle accordingly, clamps it to the 0-180 range, and calls servo.write() to move the motor, while printing the counter and angle to the serial monitor.

esp32-rotary-encoder-servo-motor.ino

/*
 * 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 <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 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;
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://esp32io.com/tutorials/esp32-rotary-encoder-servo-motor

Top comments (0)