#define Pin_KNOB_SIG 15
#define Pin_PWM_Write 12
#define Pin_PWM_Read 14

int data = 0;
int speed = 0;
int timer1 = 0;
int timer2 = 0;
int timer3 = 0;
int start = 0;
int current = 0;

void setup() {
pinMode(Pin_PWM_Write, OUTPUT);
pinMode(Pin_PWM_Read, INPUT_PULLUP);
Serial.begin(9600);
ledcSetup(0, 50, 10); // Channel 0, 50 Hz, Resolution 10
ledcAttachPin(Pin_PWM_Write, 0);
attachInterrupt(digitalPinToInterrupt(Pin_PWM_Read), read_PWM_input, CHANGE);
}

void loop() {
data = analogRead(Pin_KNOB_SIG);
speed = map(data, 0, 4095, 26, 123);
/* Speed 26 = 508 us ; Speed 123 = 2404 us */
ledcWrite(0, speed);
Serial.print("Speed: ");
Serial.println(speed);
Serial.print("PWM High : ");
Serial.println(timer1);
Serial.print("PWM Low : ");
Serial.println(timer2);
Serial.print("PWM Total : ");
Serial.println(timer3);
delay(500);
}

void read_PWM_input() {
current = micros();
if (current > start) {
timer1 = current - start;
start = current;
}
timer2 = 20000 - timer1;
timer3 = timer1 + timer2;
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
pot1:GND
pot1:SIG
pot1:VCC
servo1:GND
servo1:V+
servo1:PWM