What servo library to use with an arduino raspberrypi pico project?

I have a project which uses default arduino RPI pico configuration. What library can I use to control multiple servo (PWM) outputs? I tried the <Servo.h> but it was not found. I tried a few libraries from the libraries registry and they didn’t compile.

I prefer hardware based PWM but at this stage, anything that works should be good.

Simplified platformio.ini

framework = arduino
platform = raspberrypi
board = pico

The full platformio.ini

default_envs = raspberry_pico

# Common definitions.
framework = arduino
lib_deps =
    adafruit/Adafruit NeoPixel @ ^1.12.0

# Plain Raspberry Pico board. 2MB flash.
# https://www.raspberrypi.com/products/raspberry-pi-pico/
platform = raspberrypi
board = pico
debug_tool = cmsis-dap
debug_init_break = tbreak no_such_symbol
build_flags =

# Sparkfun Pro Micro RP2040. 16MB flash, QWIIC connector.
# https://www.sparkfun.com/products/18288
platform = raspberrypi
board = pico    
build_flags =

# Adafruit KB2040. 8MB flash, QWIIC connector.
# https://www.adafruit.com/product/5302
platform = raspberrypi
board = pico 
build_flags =
    -D ADAFRUIT_KB2040

# Adafruit QT PY RP2040. 8MB flash, QWIIC connector.
# https://www.adafruit.com/product/4900
platform = raspberrypi
board = pico 
build_flags =

After additional research, I found a built in class called mbed::PwmOut but it’s broken for pwm period longer than 2ms. However, the low pwm function it uses do work, so I created my own class.

// Based on
// https://forums.mbed.com/t/pwm-period-is-not-good-on-mbed-with-raspberry-pico/22151/5
class Servo {
  Servo(uint8_t gpio, uint16_t initial_pw_us)
      : _gpio(gpio),
        _chan(pwm_gpio_to_channel(gpio)) {
    gpio_set_function(_gpio, GPIO_FUNC_PWM);
    // Raspberry Pico default system clock is 125Mhz.
    // We divide by 125 for 1us resolution.
    pwm_set_clkdiv(_slice, 125);
    // This determines the pwm period. We divide by 20,000 for 50 Hz
    // PWM cycle. This allowed value here is in the range [0, 65535].
    pwm_set_wrap(_slice, 20000 - 1);
    pwm_set_enabled(_slice, true);

  // The standard servo pulse width is in the range 500us to 1500us.
  void set_pulse_width_us(uint16_t pw_us) {
    pwm_set_chan_level(_slice, _chan, pw_us - 1);

  const uint _gpio;
  const uint _slice;
  const uint _chan;

static Servo servo_0(0, 500);
static Servo servo_1(1, 1500);