Hello!
I'm building a project using an ESP32-S3 devkit and the ESP32Servo library. I need to control six servos but every servo other than the first two don't seem to work whenever more than two servos are .attached in my program. I've confirmed the program works because when I ran it with a regular ESP32 it worked just fine. I think this could be because the two MCPWM pins on the ESP32-S3 are being prioritized? I don't know enough about it.
Any help appreciated, thanks!
How are you powering the servos?
Where is the code?
Interesting, how do you have them connected. An annotated schematic showing all connections, power, ground, and all power sources would help us help you.
Hello! They’re MG996R servos wired up to 6V external battery pack. Grounds are connected, pins are connected as so, code is as below, basic servo sketch. In the code only the first two servos that are attached work. the other two are ignored. If i were to change the attach order in the code then those two would be the two that work. I think this may be because of the ESP32Servo library's configuration to utilize the MCPWM pins? But i don't know. Thanks
#include <ESP32Servo.h>
// create four servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
// Published values for SG90 servos; adjust if needed
int minUs = 1000;
int maxUs = 2000;
int servo1Pin = 32;
int servo2Pin = 33;
int servo3Pin = 25;
int servo4Pin = 26;
//ESP32PWM pwm;
void setup() {
servo3.attach(servo3Pin, minUs, maxUs);
servo4.attach(servo4Pin, minUs, maxUs);
servo1.attach(servo1Pin, minUs, maxUs);
servo2.attach(servo2Pin, minUs, maxUs);
//pwm.attachPin(27, 10000);//10khz
//pwm.attachPin(27, 10000);//10khz
servo1.write(0);
servo2.write(0);
delay(3000);
}
void loop() {
servo1.write(90);
servo2.write(90);
servo3.write(92);
servo4.write(75);
}
Yep I see you have the pins connected but not to the correct pins as far as I can see. From the link you posted they are running wrong. Now the power supply is pouring out water, not electrons. Since I am on the lazy side and want to help others rather than waste my time with google I expect you to post links to technical information, the descriptions you give have several items that match. If you cannot draw a schematic you can get some online tutorials, KiCad a great program is free and of course there is the antique pencil and paper.
I don't think the used pins are really useable.
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/gpio.html
This servo is never called.
Probably a wiring/power issue. This simulation works.
Yeah I just had that from testing and forgot to remove, sry. But I believe I've found the problem. ESP32S3 was updated in board version 2.0.4 to limit LEDc peripheral to not operate under 152Hz, which the ESp32Servo.h library attempts to use but cant as servos need 50Hz. You can make it work with default LEDc commands but the main library hasnt been updated for S3. So it then uses the updated MCPWM peripheral which works fine but there's only two units. This is why it wont work. Will test with ESP32-C3 unit tomorrow which doesnt have MCPWM so it should work with LEDc. Will update
I have the same problem with a Wemos S2 mini, only the first two servos given in the sketch can be controlled. I am not yet skilled enough to be using ledc with all the things that come with it. Could you perhaps test the library ESP32Servo out with a wemos s2 mini if you have one to you disposal? Couldn't I change the Hz in the library files perhaps to fix this issue? I'm sorry, I'm a complete beginner.
I found a cool servo library that can make this work. It is called ESP32 NEW ISR Servo library
Awesome that there are more ESP32 servo libraries available.
I have tried it with some SG90 servos, but it does not seem to work with the example. Expected output is 0 to 180 and back. But it only goes 90 degrees.
I have tested a bit with the min and max uS but that does not seem to make it work. My 180 degree SG90 does not like it, it sporadically responds by moving to a random position, my continuous SG90 does respond with forward and backward but it doesn't seem right, sometimes has trouble keeping the speed. ESP32Servo on the other hand has none of these issues.
How did you make it work?
This is the code I used:
#include <ESP32_New_ISR_Servo.h>
#define USE_ESP32_TIMER_NO 1
#define MIN_MICROS 544 //544
#define MAX_MICROS 2400
int servoIndex1 = 0;
int servoIndex2 = 0;
int servoIndex3 = 0;
int servoIndex4 = 0;
float pos1 = 0;
float pos2 = 180;
void setup()
{
Serial.begin(115200);
ESP32_ISR_Servos.useTimer(USE_ESP32_TIMER_NO);
servoIndex1 = ESP32_ISR_Servos.setupServo(3, MIN_MICROS, MAX_MICROS);
servoIndex2 = ESP32_ISR_Servos.setupServo(5, MIN_MICROS, MAX_MICROS);
servoIndex3 = ESP32_ISR_Servos.setupServo(7, MIN_MICROS, MAX_MICROS);
servoIndex4 = ESP32_ISR_Servos.setupServo(9, MIN_MICROS, MAX_MICROS);
}
void loop()
{
ESP32_ISR_Servos.setPosition(servoIndex1, pos1);
ESP32_ISR_Servos.setPosition(servoIndex2, pos1);
ESP32_ISR_Servos.setPosition(servoIndex3, pos1);
ESP32_ISR_Servos.setPosition(servoIndex4, pos1);
Serial.println("New Pos set on pin 3, 5, 7 9");
delay(2000);
ESP32_ISR_Servos.setPosition(servoIndex1, pos2);
ESP32_ISR_Servos.setPosition(servoIndex2, pos2);
ESP32_ISR_Servos.setPosition(servoIndex3, pos2);
ESP32_ISR_Servos.setPosition(servoIndex4, pos2);
Serial.println("New Pos set on pin 3, 5, 7 9");
delay(2000);
}
Yeah you have to change the max and min micros. I don’t remember the exact value but was something like 800 and 2100 for a MG996R. You just have to mess around with it a bit depending on the servo
I didn't get it to work properly, after some more searching I found another library:
https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
Works like a charm!
I used this code to test it, also easy writing with how its setup.
#include <pwmWrite.h>
Pwm pwm = Pwm();
const int servoPin3 = 3;
const int servoPin5 = 5;
const int servoPin7 = 7;
const int servoPin9 = 9;
void setup() {
}
void loop() {
pwm.writeServo(servoPin3, 180);
pwm.writeServo(servoPin5, 0);
pwm.writeServo(servoPin7, 180);
pwm.writeServo(servoPin9, 0);
delay(1000);
}
But I wanted to thankyou anyway because of your kindness for helping me when I asked a question!
Of course! Thanks for sharing the new library. I’m sure this will be helpful for others.
Happy making!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.