AnalogWrite Library for ESP32-ESP32S2 Arduino core

ESP32 PWM, SERVO and TONE Library 4.2.0

ESP32 PWM, SERVO, TONE and NOTE Library 4.2.1

  • Added note control function, non-blocking
  • Updated documentation and example
  • playngNotes.ino

New!

ESP32 PWM, SERVO, TONE 4.2.2

  • Added new PWM Invert option
  • Updated documentation and examples

Inverting the PWM output:

This allows using a simpler driver for higher voltage servo control.

  • Only one NPN transistor or N-Channel MOSFET needed.
  • No latency added as found with software inversion because the inverted pulse remains at the start of the refresh period rather than being flipped to the end of the refresh period.
  • Servo_Sweep_Invertedimage

ESP32 PWM, SERVO, TONE 4.2.3

ESP32 PWM, Servo, Easing, Tone 4.3.4

Servo Easing

Just 2 easing parameters (speed and easing constant) for unlimited control ...

pwm.writeServo(servoPin1, pos1, speed1, 0.0);  // move 90 deg, 70 deg/s, linear
pwm.writeServo(servoPin2, pos2, speed2, 0.6);  // mpve 180 deg, 140 deg/s, avg sigmoid
pwm.writeServo(servoPin3, pos3, speed3, 0.8);  // move 90 deg, 180 deg/s, steep sigmoid

ServoEasing

1 Like

Very smooooth servo easing and speed control...

Wokwi

ESP32 ESP32S2 AnalogWrite 5.0.0

Now you can easily use code written for the Servo Library for Arduino.

Comparison to Servo Library for Arduino

  • Both libraries use the same header filename: Servo.h

  • Methods in both libraries have identical names.

  • With the Servo Library for Arduino, each servo is instantiated, whereas only one instance is used with the ESP32 ESP32S2 AnalogWrite library to control up to 16 servos. Therefore, the write() method in the ESP32 ESP32S2 AnalogWrite library has a pin parameter to select the attached servo.

Comparison Table

  • Superscript values represent the number of available overload functions .

  • With the ESP32 ESP32S2 AnalogWrite library, both Servo.h and pwmWrite.h have access to all methods. Choose one header only that best suits your application. Note that Servo.huses a Servo class that translates method names to match the Servo Library for Arduino. Each header gives full access to the libraries features.

Library: Servo Library for Arduino ESP32 ESP32S2 AnalogWrite ESP32 ESP32S2 AnalogWrite
Header Servo.h Servo.h pwmWrite.h
Includes ServoTimers.h pwmWrite.h driver/ledc.h
Methods attach() 2 attach() 10 attachServo() 10
write() write() 2 writeServo() 2
writeMicroseconds() writeMicroseconds() n/a
read() read() read()
attached() attached() attached()
detach() detach() detach()
attachedPin() attachedPin() attachedPin()
readMicroseconds() readMicroseconds() readMicroseconds()
attachPwm() 2 attach() 2
attachInvert() 2 attachInvert() 2
writePwm() 4 write() 4
detached() detached()
firstFreeCh() firstFreeCh()
pause() pause()
resume() resume()
printDebug() printDebug()
setFrequency() setFrequency()
setResolution() setResolution()
tone() tone()
note() note()