ESP32_C3_TimerInterrupt Library

ESP32_C3_TimerInterrupt libraryGitHub release
arduino-library-badge

Why do we need this ESP32_C3_TimerInterrupt library

Features

This library enables you to use Interrupt from Hardware Timers on an ESP32-S2-based board.

The ESP32_C3 timer Interrupt control is different from that of ESP32, at least with the ESP32 core v2.0.0-rc1. While the examples of current ESP32TimerInterrupt library can be compiled OK, they can't run yet. That's why this new ESP32_C3_TimerInterrupt library has been created.


As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).

Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds) while the accuracy is nearly perfect compared to software timers.

The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.

The ISR_Timer_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of timers.

Being ISR-based timers, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet and Blynk services. You can also have many (up to 16) timers to use.

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.

You'll see blynkTimer Software is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
in loop(), using delay() function as an example. The elapsed time then is very unaccurate

Why using ISR-based Hardware Timer Interrupt is better

Imagine you have a system with a mission-critical function, measuring water level and control the sump pump 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.

The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:

HOWTO Attach Interrupt


Currently supported Boards

  1. ESP32_C3-based boards, such as ESP32C3_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.3.0

  1. Initial coding to support ESP32_C3
  2. Sync with ESP32_S2_TimerInterrupt library v1.4.0

Examples

  1. Argument_None
  2. Change_Interval
  3. ISR_RPM_Measure
  4. RPM_Measure
  5. SwitchDebounce
  6. TimerInterruptTest
  7. ISR_16_Timers_Array
  8. ISR_16_Timers_Array_Complex

Example Change_Interval on ESP32C3_DEV

The following is the sample terminal output when running example Change_Interval to demonstrate how to change Timer Interval on-the-fly

Starting Change_Interval on ESP32C3_DEV
ESP32_C3_TimerInterrupt v1.4.0
CPU Frequency = 160 MHz
ITimer0: millis() = 287
Starting  ITimer0 OK, millis() = 289
ITimer1: millis() = 291
Starting  ITimer1 OK, millis() = 294
ITimer0: millis() = 2287
ITimer0: millis() = 4287
ITimer1: millis() = 5289
ITimer0: millis() = 6287
ITimer0: millis() = 8287
Time = 10001, Timer0Count = 5, Timer1Count = 2
ITimer0: millis() = 10287
ITimer1: millis() = 10289
ITimer0: millis() = 12287
ITimer0: millis() = 14287
ITimer1: millis() = 15289
ITimer0: millis() = 16287
ITimer0: millis() = 18287
Time = 20002, Timer0Count = 10, Timer1Count = 4
Changing Interval, Timer0 = 4000,  Timer1 = 10000
ITimer0: millis() = 24002
ITimer0: millis() = 28002
ITimer1: millis() = 30002
Time = 30003, Timer0Count = 12, Timer1Count = 5
ITimer0: millis() = 32002
ITimer0: millis() = 36002
ITimer0: millis() = 40002
ITimer1: millis() = 40002
Time = 40004, Timer0Count = 15, Timer1Count = 6
Changing Interval, Timer0 = 2000,  Timer1 = 5000
ITimer0: millis() = 42004
ITimer0: millis() = 44004
ITimer1: millis() = 45004
ITimer0: millis() = 46004
ITimer0: millis() = 48004
ITimer0: millis() = 50004
ITimer1: millis() = 50004
Time = 50005, Timer0Count = 20, Timer1Count = 8
ITimer0: millis() = 52004
ITimer0: millis() = 54004
ITimer1: millis() = 55004
ITimer0: millis() = 56004
ITimer0: millis() = 58004
ITimer0: millis() = 60004
ITimer1: millis() = 60004
Time = 60006, Timer0Count = 25, Timer1Count = 10
Changing Interval, Timer0 = 4000,  Timer1 = 10000
ITimer0: millis() = 64006
ITimer0: millis() = 68006
ITimer1: millis() = 70006
Time = 70007, Timer0Count = 27, Timer1Count = 11
ITimer0: millis() = 72006
ITimer0: millis() = 76006
ITimer0: millis() = 80006
ITimer1: millis() = 80006
Time = 80008, Timer0Count = 30, Timer1Count = 12
Changing Interval, Timer0 = 2000,  Timer1 = 5000
ITimer0: millis() = 82008
ITimer0: millis() = 84008
ITimer1: millis() = 85008
ITimer0: millis() = 86008
ITimer0: millis() = 88008
ITimer0: millis() = 90008
ITimer1: millis() = 90008
Time = 90009, Timer0Count = 35, Timer1Count = 14
ITimer0: millis() = 92008
ITimer0: millis() = 94008
ITimer1: millis() = 95008
ITimer0: millis() = 96008
ITimer0: millis() = 98008
ITimer0: millis() = 100008
ITimer1: millis() = 100008
Time = 100010, Timer0Count = 40, Timer1Count = 16
Changing Interval, Timer0 = 4000,  Timer1 = 10000
ITimer0: millis() = 104010
ITimer0: millis() = 108010
ITimer1: millis() = 110010
Time = 110011, Timer0Count = 42, Timer1Count = 17
ITimer0: millis() = 112010
ITimer0: millis() = 116010
ITimer0: millis() = 120010
ITimer1: millis() = 120010
Time = 120012, Timer0Count = 45, Timer1Count = 18