Note: the P2S Servo is essentially the P1S Servo with feedback. I bought them from the website https://www.petoi.com/products/quadruped-robot-dog-bittle-servo-set Before I begin, I'm using an Arduino Nano ESP32 microcontroller and have installed the 'Arduino ESP32 Boards' package by Arduino through the Board Manager. I've successfully moved the P2S servo to specific angles using the 'ESP32Servo' library (v3.0.7) by Kevin Harrington and John K. Bennett. Version 3.0.8 didn’t work reliably for me.. Now I would like to use the Arduino Nano to read the feedback from the servo but this is where I am having challenges. I will put the code below, but before I do, my understanding from the code in espServo.h (https://github.com/PetoiCamp/OpenCatEsp32-Quadruped-Robot/blob/main/src/espServo.h) is that to read feedback from the servo, we send a PWM of 3500 to the servo. When the servo receives this feedback signal, then it will start sending the PWM signal back to the arduino where the arduino will now detach the servo and then switch from reading the servo signal pin from output to input. The issue that I am facing is that the function readFeedback, also defined in espServo.h also keeps returning the value "-1" which means that the servo is not sending a signal:
int measurePulseWidth(uint8_t pwmReadPin) {
long start = micros();
while (!digitalRead(pwmReadPin)) { // wait for high level
if (micros() - start > waitTimeForResponse)
return -1;
}
long t = micros();
while (digitalRead(pwmReadPin)) {
if (micros() - t > maxPulseWidth)
return -2;
}
return (micros() - t);
}
I have noticed that when I send the signal of 3500 to the arduino using "testServo.writeMicroseconds(3500);" this will actually move the servo to a specific angle, so it seems the servo might be interpreting 3500 µs as a regular position command rather than recognizing it as a feedback trigger signal. Another possibility is that the Arduino Nano isn't sending the 3500 µs PWM signal accurately. However, since I can reliably control the servo between 0 and 270 degrees, it seems unlikely that the PWM signal is the issue. What do you think? Does the servo moving in response to the 3500 µs signal suggest it's not recognizing it as a feedback request?
Has anyone else had success reading feedback from the P2S servo with an esp32 arduino nano?
Here is the most basic script that I came up with to debug the issue on reading servo feedback:
#include <ESP32Servo.h>
Servo testServo;
const int SERVO_PIN = 2; // Make sure this is connected to the servo signal
#define FEEDBACK_SIGNAL 3500
const int waitTimeForResponse = 10000; // µs
const int maxPulseWidth = 2600; // µs
void setup() {
Serial.begin(115200);
delay(1000);
testServo.setPeriodHertz(50);
testServo.attach(SERVO_PIN);
testServo.write(135); // Midpoint
delay(2000); // Give it time to settle
}
void loop() {
Serial.println("Triggering feedback signal...");
testServo.attach(SERVO_PIN);
testServo.writeMicroseconds(FEEDBACK_SIGNAL);
delay(15); // Give time for servo to react
testServo.detach();
pinMode(SERVO_PIN, INPUT);
long start = micros();
while (!digitalRead(SERVO_PIN)) {
if (micros() - start > waitTimeForResponse) {
Serial.println("❌ No rising edge detected — pulse = -1");
return;
}
}
long t = micros();
while (digitalRead(SERVO_PIN)) {
if (micros() - t > maxPulseWidth) {
Serial.println("⚠️ Pulse too long — returning -2");
return;
}
}
int pulseWidth = micros() - t;
Serial.printf("✅ Pulse detected! Width = %d µs\n", pulseWidth);
delay(1000);
}
and just for everyone else that wants to control this servo via Arduino Nano Esp32, here is how I move the servo:
#include <ESP32Servo.h>
Servo myServo;
void setup() {
Serial.begin(115200);
Serial.println("Booting...");
delay(2500);
myServo.setPeriodHertz(50); // Standard 50 Hz servo
bool ok = myServo.attach(2); // try 5 or 18 if 9 fails
Serial.printf("Servo attached: %s\n", ok ? "yes" : "no");
}
void loop() {
Serial.printf("move to 0\n");
myServo.write(0);
delay(1000);
Serial.printf("move to 90\n");
myServo.write(90);
delay(1000);
Serial.printf("move to 180\n");
myServo.write(180);
delay(1000);
}
Thank you for taking the time to read my post, hopefully I have contained enough information, and if not, please let me know! Thanks, Andres M.
Andres, if you are using the ESP32Servo installed via the library manager, you need to go into that library file and modify a few lines: