Servo Library does not work on ESP32

I have a ESP32-C3 super mini board where i cannot get any servosignal to run.

With the following code i can produce a pwm Signal with 1kHz as it should be (measured with oszilloskop). So the hardware is ok.

void setup() {
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(2,90);  // Creates an PWM Signal with 1kHz on PIN 2 as it should do.
  delay(200);
}

As soon as i want to use a Servo library i do not get any pin to provide a servo signal.

#include <Servo.h>

const int servoPin = 3;

Servo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(servoPin);
}

void loop() {
  for(int posDegrees = 0; posDegrees <= 180; posDegrees++) {
    myServo.write(posDegrees);
    Serial.println(posDegrees);
    delay(20);
  }

  for(int posDegrees = 180; posDegrees >= 0; posDegrees--) {
    myServo.write(posDegrees);
    Serial.println(posDegrees);
    delay(20);
  }
}

Are i’m using the wrong library or do i have to do anything in additon to get it work?

Regards Thomas

I don't see the ESP32 on the compatibility list.
https://docs.arduino.cc/libraries/servo/#Compatibility

1 Like

You have to use a different library. Try this:
https://docs.arduino.cc/libraries/esp32servo/

1 Like

Yeah it is working with the library hallowed31 suggested. i also missed to check the compatibility list. i only searched for a library for the ESP32 processor. The fact that a library is suitable for the ESP32 processor but not for my specific board is a little bit confusing for me.

Thank you very much for your help. Now i can step deeper into the prgramming of my servo control.

2 Likes

My understanding is that it’s not. What made you conclude servo.h is ok for the ESP32 processor?

i searched in the arduino IDE for an servo library and ESP32 processor. What i got was the SimpleServoESP32 library from noeFly where the Servo.h is included. Thats why i assumed it should be ok for my board including the ESP32 processor.

A servo signal frequency should be 50Hz, not 1KHz.
With a very short "HIGH" time of 1-2ms.
Best to use a library for that.

Make sure you connect servo ground to supply ground and ESP ground.
Servos draw a high peak current every time they move.
About 650mA for the tiny SG90 and 2.5Amp for larger ones.
Therefore it's always better to give them their own supply, rated for at least that current.
Leo..