ESP32_S2_ISR_Servo Library

ESP32_S2_ISR_Servo libraryGitHub release
arduino-library-badge

Why do we need this ESP32_S2_ISR_Servo library

Features

Imagine you have a system with a mission-critical function controlling a robot arm or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().

So your function might not be executed, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.

This library enables you to use 1 Hardware Timer on an ESP32_S2-based board to control up to 16 independent servo motors.


Currently supported Boards

  1. ESP32_S2-based boards, such as ESP32S2_DEV, etc

Important Notes about ISR

  1. Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

  2. Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.



Changelog

Releases v1.1.0

  1. Basic 16 ISR-based servo controllers using 1 hardware timer for ESP32_S2.
  2. Tested with ESP32 core v2.0.0-rc1 and v1.0.6

Examples

  1. ESP32_S2_ISR_MultiServos
  2. ESP32_S2_MultipleRandomServos
  3. ESP32_S2_MultipleServos
  4. ISR_MultiServos
  5. MultipleRandomServos
  6. MultipleServos

Example ESP32_S2_ISR_MultiServos on ESP32S2_DEV

The following is the sample terminal output when running example ESP32_S2_ISR_MultiServos to demonstrate how to control multiple Servos using 1 hardware timer.

Starting ESP32_S2_ISR_MultiServos on ESP32S2_DEV
ESP32_S2_ISR_Servo v1.1.0
[ISR_SERVO] ESP32_S2_FastTimerInterrupt: _timerNo = 3 , _fre = 1000000
[ISR_SERVO] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[ISR_SERVO] _timerIndex = 1 , _timerGroup = 1
[ISR_SERVO] _count = 0 - 10
[ISR_SERVO] timer_set_alarm_value = 10.00
[ISR_SERVO] Starting  ITimer OK
Setup Servo1 OK
Setup Servo2 OK
Servo1 pos = 0, Servo2 pos = 180
Servo1 pos = 30, Servo2 pos = 150
Servo1 pos = 60, Servo2 pos = 120
Servo1 pos = 90, Servo2 pos = 90
Servo1 pos = 120, Servo2 pos = 60
Servo1 pos = 150, Servo2 pos = 30
Servo1 pos = 180, Servo2 pos = 0

Why use millis() for an ESP32 library when the esp_timer_get_time() macro returns a 64bit time in microseconds and does not roll over for over 200 years and is native to the ESP32?

Why not use the MCPWM API for servos, you get the same functionality using the ESP32's built in hardware devices over using software and does not consume a hardware timers?

Just curious.

Thanks for your comment.

The library is actually not using millis() function at all. The millis() was mentioned for comparison to other use-cases using software timers based on millis() / micros()

You certainly can use PWM to control servo and write the control code yourself. But these ISR-Servo libraries, such as ESP32Servo, ESP32_ISR_Servo, ESP32_S2_ISR_Servo, etc. will provide easier and faster way for you to write your code.

IMHO, that the intended function of any library, just an optional step-stone for us to reach higher and concentrate on more important matters.

Then if you wrote it you write the other function differently

Using loop() with an ESP32 is a inefficient way to use a ESP32.