ESP32_PWM Library

ESP32_PWM libraryGitHub release
arduino-library-badge

Why do we need this ESP32_PWM library

Features

This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2-based board to create and output PWM to pins.

It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint32_t millisecs). The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware PWM channels, 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.


Currently supported Boards

  1. ESP32 boards, such as ESP32_DEV, etc.
  2. ESP32S2-based boards, such as ESP32S2_DEV, ESP32_S2 Saola, etc.
  3. ESP32C3-based boards, such as ESP32C3_DEV, etc. Not yet supported yet


Changelog

Releases v1.0.0

  1. Initial coding for ESP32, ESP32_S2 boards using ESP32 core v2.0.0+

Examples:

  1. ISR_16_PWMs_Array
  2. ISR_16_PWMs_Array_Complex
  3. ISR_16_PWMs_Array_Simple

1. ISR_16_PWMs_Array_Complex on ESP32_DEV

The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex to demonstrate the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods

Starting ISR_16_PWMs_Array_Complex on ESP32_DEV
ESP32_PWM v1.0.0
CPU Frequency = 240 MHz
[PWM] ESP32_TimerInterrupt: _timerNo = 1 , _fre = 1000000
[PWM] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80
[PWM] _timerIndex = 1 , _timerGroup = 0
[PWM] _count = 0 - 20
[PWM] timer_set_alarm_value = 20.00
Starting ITimer OK, millis() = 2054
Channel : 0	Period : 1000000		OnTime : 50000	Start_Time : 2054323
Channel : 1	Period : 500000		OnTime : 50000	Start_Time : 2054323
Channel : 2	Period : 333333		OnTime : 66666	Start_Time : 2054323
Channel : 3	Period : 250000		OnTime : 75000	Start_Time : 2054323
Channel : 4	Period : 200000		OnTime : 80000	Start_Time : 2054323
Channel : 5	Period : 166666		OnTime : 74999	Start_Time : 2054323
Channel : 6	Period : 142857		OnTime : 71428	Start_Time : 2054323
Channel : 7	Period : 125000		OnTime : 68750	Start_Time : 2054323
Channel : 8	Period : 111111		OnTime : 66666	Start_Time : 2054323
Channel : 9	Period : 100000		OnTime : 65000	Start_Time : 2054323
Channel : 10	Period : 66666		OnTime : 46666	Start_Time : 2054323
Channel : 11	Period : 50000		OnTime : 37500	Start_Time : 2054323
Channel : 12	Period : 40000		OnTime : 32000	Start_Time : 2054323
Channel : 13	Period : 33333		OnTime : 28333	Start_Time : 2054323
Channel : 14	Period : 25000		OnTime : 22500	Start_Time : 2054323
Channel : 15	Period : 20000		OnTime : 19000	Start_Time : 2054323
SimpleTimer (ms): 2000, ms : 12150, Dms : 10096
PWM Channel : 0, programmed : 1000, actual : 1000
PWM Channel : 1, programmed : 500, actual : 500
PWM Channel : 2, programmed : 333, actual : 333
PWM Channel : 3, programmed : 250, actual : 250
PWM Channel : 4, programmed : 200, actual : 200
PWM Channel : 5, programmed : 166, actual : 167
PWM Channel : 6, programmed : 142, actual : 143
PWM Channel : 7, programmed : 125, actual : 125
PWM Channel : 8, programmed : 111, actual : 111
PWM Channel : 9, programmed : 100, actual : 100
PWM Channel : 10, programmed : 66, actual : 66
PWM Channel : 11, programmed : 50, actual : 50
PWM Channel : 12, programmed : 40, actual : 40
PWM Channel : 13, programmed : 33, actual : 33
PWM Channel : 14, programmed : 25, actual : 25
PWM Channel : 15, programmed : 20, actual : 20