- Notifications
You must be signed in to change notification settings - Fork10
This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2 or ESP32_C3-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 chann…
License
khoih-prog/ESP32_PWM
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- Important Change from v1.2.0
- Why do we need this ESP32_PWM library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple DefinitionsLinker Error - HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)
- More useful Information
- How to use
- Examples
- Example ISR_16_PWMs_Array_Complex
- Debug Terminal Output Samples
- 1. ISR_16_PWMs_Array_Complex on ESP32_DEV
- 2. ISR_16_PWMs_Array on ESP32_DEV
- 3. ISR_16_PWMs_Array_Simple on ESP32_DEV
- 4. ISR_Modify_PWM on ESP32_DEV
- 5. ISR_Changing_PWM on ESP32_DEV
- 6. ISR_Modify_PWM on ESP32S2_DEV
- 7. ISR_Changing_PWM on ESP32S2_DEV
- 8. ISR_Modify_PWM on ESP32C3_DEV
- 9. ISR_Changing_PWM on ESP32C3_DEV
- 10. ISR_16_PWMs_Array_Complex on ESP32S3_DEVNew
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look atHOWTO FixMultiple Definitions Linker Error
As more complex calculation and checkinside ISR are introduced from v1.2.0, there is possibly some crash depending on use-case.
You can modify to use largerHW_TIMER_INTERVAL_US, (from current 20uS), according to your board and use-case if crash happens.
// Current 20uS#defineHW_TIMER_INTERVAL_US20L
Why do we need thisESP32_PWM library
This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2-based board to create and output PWM to pins. Becayse this library doesn't use the powerful hardware-controlled PWM with limitations, the maximum PWM frequency is currently limited at500Hz, which is suitable for many real-life applications. Now you can also modify PWM settings on-the-fly.
This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2 or ESP32_C3-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.
AsHardware Timers are rare, and very precious assets of any board, this library now enables you to use up to16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new16 ISR-based timers, the maximum interval ispractically unlimited (limited only by unsigned long milliseconds) whilethe accuracy is nearly perfect compared to software timers.
The most important feature is they're ISR-based PWM channels. Therefore, their executions arenot blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
TheISR_16_PWMs_Array_Complex example will demonstrate the nearly perfect accuracy compared to software timers by printing the actual elapsed millisecs of each type of PWM channels.
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.
Imagine you have a system with amission-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 isblocking the loop() or setup().
So your functionmight 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 PWM-channels withInterrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much moreprecise (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 isyour function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
- ESP32 boards, such as
ESP32_DEV, etc. - ESP32_S2-based boards, such as
ESP32S2_DEV,ESP32_S2 Saola, Adafruit QTPY_ESP32S2, ESP32S2 Native USB, UM FeatherS2 Neo, UM TinyS2, UM RMP, microS2, etc. - ESP32_C3-based boards, such as
ESP32C3_DEV, LOLIN_C3_MINI, DFROBOT_BEETLE_ESP32_C3, ADAFRUIT_QTPY_ESP32C3, AirM2M_CORE_ESP32C3, XIAO_ESP32C3, etc.New - ESP32_S3-based boards, such as ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, FEATHER_ESP32S3_NOPSRAM, QTPY_ESP32S3_NOPSRAM, etc.New
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 asvolatile any variables that you modify within the attached function.
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.
Arduino IDE 1.8.19+for Arduino.ESP32 Core 2.0.5+for ESP32-based boards..
SimpleTimer libraryto use with some examples.
The best and easiest way is to useArduino Library Manager. Search forESP32_PWM, then select / install the latest version.You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate toESP32_PWM page.
- Download the latest release
ESP32_PWM-main.zip. - Extract the zip file to
ESP32_PWM-maindirectory - Copy whole
ESP32_PWM-mainfolder to Arduino libraries' directory such as~/Arduino/libraries/.
- InstallVS Code
- InstallPlatformIO
- InstallESP32_PWM library by usingLibrary Manager. Search forESP32_PWM inPlatform.io Author's Libraries
- Use includedplatformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples atProject Configuration File
The current library implementation, usingxyz-Impl.h instead of standardxyz.cpp, possibly creates certainMultiple Definitions Linker error in certain use cases.
You can include this.hpp file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error#include"ESP32_PWM.hpp"//https://github.com/khoih-prog/ESP32_PWM
in many files. But be sure to use the following.h filein just 1.h,.cpp or.ino file, which mustnot be included in any other file, to avoidMultiple Definitions Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error#include"ESP32_PWM.h"//https://github.com/khoih-prog/ESP32_PWM
Check the newmultiFileProject example for aHOWTO demo.
Have a look at the discussion inDifferent behaviour using the src_cpp or src_h lib #80
Please have a look atESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to have more detailed description and solution of the issue.
ADC1controls ADC function for pinsGPIO32-GPIO39ADC2controls ADC function for pinsGPIO0, 2, 4, 12-15, 25-27
Look in fileadc_common.c
In
ADC2, there're two locks used for different cases:
lock shared with app and Wi-Fi:ESP32:When Wi-Fi using the
ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.ESP32S2:The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.lock shared between tasks:when several tasks sharing the
ADC2, we want to guaranteeall the requests will be handled.Since conversions are short (about 31us), app returns the lock very soon,we use a spinlock to stand there waiting to do conversions one by one.adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
- In order to use
ADC2for other functions, we have toacquire complicated firmware locks and very difficult to do - So, it's not advisable to use
ADC2with WiFi/BlueTooth (BT/BLE). - Use
ADC1, and pinsGPIO32-GPIO39 - If somehow it's a must to use those pins serviced by
ADC2(GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27), use thefix mentioned at the end ofESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to work with ESP32 WiFi/BlueTooth (BT/BLE).
- The ESP32, ESP32_S2 and ESP32_S3 has two timer groups, each one with two general purpose hardware timers.
- The ESP32_C3 has two timer groups, each one with only one general purpose hardware timer.
- All the timers are based on64-bit counters (except 54-bit counter for ESP32_S3 counter) and 16-bit prescalers.
- The timer counters can be configured to count up or down and support automatic reload and software reload.
- They can also generate alarms when they reach a specific value, defined by the software.
- The value of the counter can be read by the software program.
Now with these new16 ISR-based PWM-channels (while consuming only1 hardware timer), the maximum interval is practically unlimited (limited only by unsigned long milliseconds). The accuracy is nearly perfect compared to software PWM-channels. The most important feature is they're ISR-based PWM-channels Therefore, their executions are not blocked by bad-behaving functions / tasks.This important feature is absolutely necessary for mission-critical tasks.
TheISR_16_PWMs_Array_Complex example will demonstrate the nearly perfect accuracy compared to software-based PWM-channels by printing the actual elapsedmicrosecs / millisecs of each type of PWM-channels.Being ISR-based PWM-channels, 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) synchronized PWM-channels to use.This non-being-blocked important feature is absolutely necessary for mission-critical tasks.You'll seeSimpleTimer is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking taskinloop(), usingdelay() function as an example. The elapsed time then is very unaccurate
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
Timer0, Timer1, Timer2 and Timer3 are supported for ESP32
- ISR_16_PWMs_Array
- ISR_16_PWMs_Array_Complex
- ISR_16_PWMs_Array_Simple
- ISR_Changing_PWM
- ISR_Modify_PWM
- multiFileProjectNew
ExampleISR_16_PWMs_Array_Complex
ESP32_PWM/examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino
Lines 25 to 471 inbf025f2
| #if !defined( ESP32 ) | |
| #error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting. | |
| #endif | |
| // These define's must be placed at the beginning before #include "ESP32_PWM.h" | |
| // _PWM_LOGLEVEL_ from 0 to 4 | |
| // Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. | |
| #define_PWM_LOGLEVEL_3 | |
| #defineUSING_MICROS_RESOLUTIONtrue//false | |
| // Default is true, uncomment to false | |
| //#define CHANGING_PWM_END_OF_CYCLE false | |
| // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error | |
| #include"ESP32_PWM.h" | |
| #include<SimpleTimer.h>// https://github.com/jfturcot/SimpleTimer | |
| #ifndef LED_BUILTIN | |
| #defineLED_BUILTIN2 | |
| #endif | |
| #ifndef LED_BLUE | |
| #defineLED_BLUE25 | |
| #endif | |
| #ifndef LED_RED | |
| #defineLED_RED27 | |
| #endif | |
| #defineHW_TIMER_INTERVAL_US20L | |
| volatileuint32_t startMicros =0; | |
| // Init ESP32 timer 1 | |
| ESP32TimerITimer(1); | |
| // Init ESP32_ISR_PWM | |
| ESP32_PWM ISR_PWM; | |
| bool IRAM_ATTRTimerHandler(void * timerNo) | |
| { | |
| ISR_PWM.run(); | |
| returntrue; | |
| } | |
| ///////////////////////////////////////////////// | |
| #if ( ARDUINO_ESP32C3_DEV ) | |
| #defineNUMBER_ISR_PWMS4 | |
| #elif ( ARDUINO_ESP32S3_DEV ) | |
| #defineNUMBER_ISR_PWMS16 | |
| #else | |
| #defineNUMBER_ISR_PWMS16 | |
| #endif | |
| #definePIN_D00// Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32 | |
| #definePIN_D11// Pin D1 mapped to pin GPIO1/TX0 of ESP32 | |
| #definePIN_D22// Pin D2 mapped to pin GPIO2/ADC12/TOUCH2 of ESP32 | |
| #definePIN_D33// Pin D3 mapped to pin GPIO3/RX0 of ESP32 | |
| #definePIN_D44// Pin D4 mapped to pin GPIO4/ADC10/TOUCH0 of ESP32 | |
| #definePIN_D55// Pin D5 mapped to pin GPIO5/SPISS/VSPI_SS of ESP32 | |
| #definePIN_D66// Pin D6 mapped to pin GPIO6 of ESP32 | |
| #definePIN_D77// Pin D7 mapped to pin GPIO7 of ESP32 | |
| #definePIN_D88// Pin D8 mapped to pin GPIO8 of ESP32 | |
| #definePIN_D99// Pin D9 mapped to pin GPIO9 of ESP32 | |
| #definePIN_D1010// Pin D10 mapped to pin GPIO10 of ESP32 | |
| #definePIN_D1111// Pin D11 mapped to pin GPIO11 of ESP32 | |
| #definePIN_D1212// Pin D12 mapped to pin GPIO12/HSPI_MISO/ADC15/TOUCH5/TDI of ESP32 | |
| #definePIN_D1313// Pin D13 mapped to pin GPIO13/HSPI_MOSI/ADC14/TOUCH4/TCK of ESP32 | |
| #definePIN_D1414// Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32 | |
| #definePIN_D1515// Pin D15 mapped to pin GPIO15/HSPI_SS/ADC13/TOUCH3/TDO of ESP32 | |
| #definePIN_D1616// Pin D16 mapped to pin GPIO16/TX2 of ESP32 | |
| #definePIN_D1717// Pin D17 mapped to pin GPIO17/RX2 of ESP32 | |
| #definePIN_D1818// Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32 | |
| #definePIN_D1919// Pin D19 mapped to pin GPIO19/VSPI_MISO of ESP32 | |
| #definePIN_D2121// Pin D21 mapped to pin GPIO21/SDA of ESP32 | |
| #definePIN_D2222// Pin D22 mapped to pin GPIO22/SCL of ESP32 | |
| #definePIN_D2323// Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32 | |
| #definePIN_D2525// Pin D25 mapped to pin GPIO25/ADC18/DAC1 of ESP32 | |
| #definePIN_D2626// Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32 | |
| #definePIN_D2727// Pin D27 mapped to pin GPIO27/ADC17/TOUCH7 of ESP32 | |
| typedefvoid (*irqCallback) (); | |
| ////////////////////////////////////////////////////// | |
| #defineUSING_PWM_FREQUENCYfalse//true | |
| ////////////////////////////////////////////////////// | |
| volatileunsignedlong deltaMicrosStart [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| volatileunsignedlong previousMicrosStart [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| volatileunsignedlong deltaMicrosStop [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| volatileunsignedlong previousMicrosStop [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; | |
| // You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11 | |
| // Can't use PIN_D1 for core v2.0.1+ | |
| #if ( ARDUINO_ESP32C3_DEV ) | |
| uint32_t PWM_Pin[] = | |
| // Bad pins to use: PIN_D12-PIN_D24 | |
| { | |
| LED_BUILTIN, PIN_D3, PIN_D4, PIN_D5 | |
| }; | |
| #elif ( ARDUINO_ESP32S3_DEV ) | |
| uint32_t PWM_Pin[] = | |
| // Bad pins to use: PIN_D24 | |
| { | |
| PIN_D1, PIN_D2, PIN_D3, PIN_D4, PIN_D5, PIN_D6, PIN_D7, PIN_D8, | |
| PIN_D9, PIN_D10, PIN_D11, PIN_D12, PIN_D13, PIN_D14, PIN_D15, PIN_D16, | |
| }; | |
| #else | |
| // Bad pins to use: PIN_D24 | |
| uint32_t PWM_Pin[] = | |
| { | |
| LED_BUILTIN, PIN_D25, PIN_D3, PIN_D4, PIN_D5, PIN_D12, PIN_D13, PIN_D14, | |
| PIN_D15, PIN_D16, PIN_D17, PIN_D18, PIN_D19, PIN_D21, PIN_D22, PIN_D23 | |
| }; | |
| #endif | |
| // You can assign any interval for any timer here, in microseconds | |
| uint32_t PWM_Period[] = | |
| { | |
| 1000000,500000,333333,250000,200000,166667,142857,125000, | |
| 111111,100000,66667,50000,40000,33333,25000,20000 | |
| }; | |
| // You can assign any interval for any timer here, in Hz | |
| float PWM_Freq[] = | |
| { | |
| 1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f, | |
| 9.0f,10.0f,15.0f,20.0f,25.0f,30.0f,40.0f,50.0f | |
| }; | |
| // You can assign any interval for any timer here, in milliseconds | |
| float PWM_DutyCycle[] = | |
| { | |
| 5.00,10.00,20.00,30.00,40.00,45.00,50.00,55.00, | |
| 60.00,65.00,70.00,75.00,80.00,85.00,90.00,95.00 | |
| }; | |
| voiddoingSomethingStart(int index) | |
| { | |
| unsignedlong currentMicros =micros(); | |
| deltaMicrosStart[index] = currentMicros - previousMicrosStart[index]; | |
| previousMicrosStart[index] = currentMicros; | |
| } | |
| voiddoingSomethingStop(int index) | |
| { | |
| unsignedlong currentMicros =micros(); | |
| // Count from start to stop PWM pulse | |
| deltaMicrosStop[index] = currentMicros - previousMicrosStart[index]; | |
| previousMicrosStop[index] = currentMicros; | |
| } | |
| //////////////////////////////////// | |
| // Shared | |
| //////////////////////////////////// | |
| voiddoingSomethingStart0() | |
| { | |
| doingSomethingStart(0); | |
| } | |
| voiddoingSomethingStart1() | |
| { | |
| doingSomethingStart(1); | |
| } | |
| voiddoingSomethingStart2() | |
| { | |
| doingSomethingStart(2); | |
| } | |
| voiddoingSomethingStart3() | |
| { | |
| doingSomethingStart(3); | |
| } | |
| voiddoingSomethingStart4() | |
| { | |
| doingSomethingStart(4); | |
| } | |
| voiddoingSomethingStart5() | |
| { | |
| doingSomethingStart(5); | |
| } | |
| voiddoingSomethingStart6() | |
| { | |
| doingSomethingStart(6); | |
| } | |
| voiddoingSomethingStart7() | |
| { | |
| doingSomethingStart(7); | |
| } | |
| voiddoingSomethingStart8() | |
| { | |
| doingSomethingStart(8); | |
| } | |
| voiddoingSomethingStart9() | |
| { | |
| doingSomethingStart(9); | |
| } | |
| voiddoingSomethingStart10() | |
| { | |
| doingSomethingStart(10); | |
| } | |
| voiddoingSomethingStart11() | |
| { | |
| doingSomethingStart(11); | |
| } | |
| voiddoingSomethingStart12() | |
| { | |
| doingSomethingStart(12); | |
| } | |
| voiddoingSomethingStart13() | |
| { | |
| doingSomethingStart(13); | |
| } | |
| voiddoingSomethingStart14() | |
| { | |
| doingSomethingStart(14); | |
| } | |
| voiddoingSomethingStart15() | |
| { | |
| doingSomethingStart(15); | |
| } | |
| ////////////////////////////////////////////////////// | |
| voiddoingSomethingStop0() | |
| { | |
| doingSomethingStop(0); | |
| } | |
| voiddoingSomethingStop1() | |
| { | |
| doingSomethingStop(1); | |
| } | |
| voiddoingSomethingStop2() | |
| { | |
| doingSomethingStop(2); | |
| } | |
| voiddoingSomethingStop3() | |
| { | |
| doingSomethingStop(3); | |
| } | |
| voiddoingSomethingStop4() | |
| { | |
| doingSomethingStop(4); | |
| } | |
| voiddoingSomethingStop5() | |
| { | |
| doingSomethingStop(5); | |
| } | |
| voiddoingSomethingStop6() | |
| { | |
| doingSomethingStop(6); | |
| } | |
| voiddoingSomethingStop7() | |
| { | |
| doingSomethingStop(7); | |
| } | |
| voiddoingSomethingStop8() | |
| { | |
| doingSomethingStop(8); | |
| } | |
| voiddoingSomethingStop9() | |
| { | |
| doingSomethingStop(9); | |
| } | |
| voiddoingSomethingStop10() | |
| { | |
| doingSomethingStop(10); | |
| } | |
| voiddoingSomethingStop11() | |
| { | |
| doingSomethingStop(11); | |
| } | |
| voiddoingSomethingStop12() | |
| { | |
| doingSomethingStop(12); | |
| } | |
| voiddoingSomethingStop13() | |
| { | |
| doingSomethingStop(13); | |
| } | |
| voiddoingSomethingStop14() | |
| { | |
| doingSomethingStop(14); | |
| } | |
| voiddoingSomethingStop15() | |
| { | |
| doingSomethingStop(15); | |
| } | |
| ////////////////////////////////////////////////////// | |
| irqCallback irqCallbackStartFunc[] = | |
| { | |
| doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3, | |
| doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7, | |
| doingSomethingStart8, doingSomethingStart9, doingSomethingStart10, doingSomethingStart11, | |
| doingSomethingStart12, doingSomethingStart13, doingSomethingStart14, doingSomethingStart15 | |
| }; | |
| irqCallback irqCallbackStopFunc[] = | |
| { | |
| doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3, | |
| doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7, | |
| doingSomethingStop8, doingSomethingStop9, doingSomethingStop10, doingSomethingStop11, | |
| doingSomethingStop12, doingSomethingStop13, doingSomethingStop14, doingSomethingStop15 | |
| }; | |
| ////////////////////////////////////////////////////// | |
| #defineSIMPLE_TIMER_MS2000L | |
| // Init SimpleTimer | |
| SimpleTimer simpleTimer; | |
| // Here is software Timer, you can do somewhat fancy stuffs without many issues. | |
| // But always avoid | |
| // 1. Long delay() it just doing nothing and pain-without-gain wasting CPU power.Plan and design your code / strategy ahead | |
| // 2. Very long "do", "while", "for" loops without predetermined exit time. | |
| voidsimpleTimerDoingSomething2s() | |
| { | |
| staticunsignedlong previousMicrosStart = startMicros; | |
| unsignedlong currMicros =micros(); | |
| Serial.print(F("SimpleTimer (ms):")); Serial.print(SIMPLE_TIMER_MS); | |
| Serial.print(F(", us :")); Serial.print(currMicros); | |
| Serial.print(F(", Dus :")); Serial.println(currMicros - previousMicrosStart); | |
| for (uint16_t i =0; i < NUMBER_ISR_PWMS; i++) | |
| { | |
| Serial.print(F("PWM Channel :")); Serial.print(i); | |
| #if USING_PWM_FREQUENCY | |
| Serial.print(1000000 / PWM_Freq[i]); | |
| #else | |
| Serial.print(PWM_Period[i]); | |
| #endif | |
| Serial.print(F(", programmed Period (us):")); Serial.print(PWM_Period[i]); | |
| Serial.print(F(", actual :")); Serial.print(deltaMicrosStart[i]); | |
| Serial.print(F(", programmed DutyCycle :")); | |
| Serial.print(PWM_DutyCycle[i]); | |
| Serial.print(F(", actual :")); Serial.println( (float) deltaMicrosStop[i] *100.0f / deltaMicrosStart[i]); | |
| } | |
| previousMicrosStart = currMicros; | |
| } | |
| voidsetup() | |
| { | |
| Serial.begin(115200); | |
| while (!Serial); | |
| delay(2000); | |
| Serial.print(F("\nStarting ISR_16_PWMs_Array_Complex on")); Serial.println(ARDUINO_BOARD); | |
| Serial.println(ESP32_PWM_VERSION); | |
| Serial.print(F("CPU Frequency =")); Serial.print(F_CPU /1000000); Serial.println(F(" MHz")); | |
| // Interval in microsecs | |
| if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler)) | |
| { | |
| startMicros =micros(); | |
| Serial.print(F("Starting ITimer OK, micros() =")); Serial.println(startMicros); | |
| } | |
| else | |
| Serial.println(F("Can't set ITimer. Select another freq. or timer")); | |
| startMicros =micros(); | |
| // Just to demonstrate, don't use too many ISR Timers if not absolutely necessary | |
| // You can use up to 16 timer for each ISR_PWM | |
| for (uint16_t i =0; i < NUMBER_ISR_PWMS; i++) | |
| { | |
| previousMicrosStart[i] =micros(); | |
| #if USING_PWM_FREQUENCY | |
| // You can use this with PWM_Freq in Hz | |
| ISR_PWM.setPWM(PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc[i], irqCallbackStopFunc[i]); | |
| #else | |
| // Or You can use this with PWM_Period in us | |
| ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i], PWM_DutyCycle[i], irqCallbackStartFunc[i], irqCallbackStopFunc[i]); | |
| #endif | |
| } | |
| // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary. | |
| simpleTimer.setInterval(SIMPLE_TIMER_MS, simpleTimerDoingSomething2s); | |
| } | |
| #defineBLOCKING_TIME_MS10000L | |
| voidloop() | |
| { | |
| // This unadvised blocking task is used to demonstrate the blocking effects onto the execution and accuracy to Software timer | |
| // You see the time elapse of ISR_PWM still accurate, whereas very unaccurate for Software Timer | |
| // The time elapse for 2000ms software timer now becomes 3000ms (BLOCKING_TIME_MS) | |
| // While that of ISR_PWM is still prefect. | |
| delay(BLOCKING_TIME_MS); | |
| // You need this Software timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary | |
| // You don't need to and never call ISR_PWM.run() here in the loop(). It's already handled by ISR timer. | |
| simpleTimer.run(); | |
| } |
The following is the sample terminal output when running exampleISR_16_PWMs_Array_Complex onESP32_DEV to demonstrate the accuracy of ISR Hardware PWM-channels,especially when system is very busy. The ISR PWM-channels isrunning exactly according to corresponding programmed periods and duty-cycles
Starting ISR_16_PWMs_Array_Complex on ESP32_DEVESP32_PWM v1.3.3CPU 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.00Starting ITimer OK, micros() =2058708Channel :0 Period :1000000OnTime :50000Start_Time :2058897Channel :1 Period :500000OnTime :50000Start_Time :2069539Channel :2 Period :333333OnTime :66666Start_Time :2069906Channel :3 Period :250000OnTime :75000Start_Time :2080530Channel :4 Period :200000OnTime :80000Start_Time :2080889Channel :5 Period :166667OnTime :75000Start_Time :2091451Channel :6 Period :142857OnTime :71428Start_Time :2102051Channel :7 Period :125000OnTime :68750Start_Time :2102413Channel :8 Period :111111OnTime :66666Start_Time :2113029Channel :9 Period :100000OnTime :65000Start_Time :2113401Channel :10 Period :66667OnTime :46666Start_Time :2124047Channel :11 Period :50000OnTime :37500Start_Time :2124423Channel :12 Period :40000OnTime :32000Start_Time :2135081Channel :13 Period :33333OnTime :28333Start_Time :2135450Channel :14 Period :25000OnTime :22500Start_Time :2146102Channel :15 Period :20000OnTime :19000Start_Time :2156669SimpleTimer (ms): 2000, us : 12156966, Dus : 10098123PWM Channel : 01000000, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00PWM Channel : 1500000, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00PWM Channel : 2333333, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00PWM Channel : 3250000, programmed Period (us): 250000, actual : 250000, programmed DutyCycle : 30.00, actual : 30.00PWM Channel : 4200000, programmed Period (us): 200000, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00PWM Channel : 5166667, programmed Period (us): 166667, actual : 166680, programmed DutyCycle : 45.00, actual : 45.00PWM Channel : 6142857, programmed Period (us): 142857, actual : 142860, programmed DutyCycle : 50.00, actual : 49.99PWM Channel : 7125000, programmed Period (us): 125000, actual : 125001, programmed DutyCycle : 55.00, actual : 54.99PWM Channel : 8111111, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99PWM Channel : 9100000, programmed Period (us): 100000, actual : 100002, programmed DutyCycle : 65.00, actual : 65.00PWM Channel : 1066667, programmed Period (us): 66667, actual : 66680, programmed DutyCycle : 70.00, actual : 69.98PWM Channel : 1150000, programmed Period (us): 50000, actual : 50001, programmed DutyCycle : 75.00, actual : 74.97PWM Channel : 1240000, programmed Period (us): 40000, actual : 39999, programmed DutyCycle : 80.00, actual : 80.00PWM Channel : 1333333, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85.00, actual : 84.94PWM Channel : 1425000, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90.00, actual : 90.00PWM Channel : 1520000, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95.00, actual : 95.00SimpleTimer (ms): 2000, us : 22312882, Dus : 10155916PWM Channel : 01000000, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00PWM Channel : 1500000, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00PWM Channel : 2333333, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00PWM Channel : 3250000, programmed Period (us): 250000, actual : 250000, programmed DutyCycle : 30.00, actual : 30.00PWM Channel : 4200000, programmed Period (us): 200000, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00PWM Channel : 5166667, programmed Period (us): 166667, actual : 166680, programmed DutyCycle : 45.00, actual : 45.00PWM Channel : 6142857, programmed Period (us): 142857, actual : 142861, programmed DutyCycle : 50.00, actual : 49.99PWM Channel : 7125000, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99PWM Channel : 8111111, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99PWM Channel : 9100000, programmed Period (us): 100000, actual : 100001, programmed DutyCycle : 65.00, actual : 65.00PWM Channel : 1066667, programmed Period (us): 66667, actual : 66680, programmed DutyCycle : 70.00, actual : 69.98PWM Channel : 1150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00PWM Channel : 1240000, programmed Period (us): 40000, actual : 40000, programmed DutyCycle : 80.00, actual : 80.00PWM Channel : 1333333, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85.00, actual : 84.94PWM Channel : 1425000, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90.00, actual : 90.00PWM Channel : 1520000, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95.00, actual : 95.00
The following is the sample terminal output when running exampleISR_16_PWMs_Array onESP32_DEV to demonstrate how to use multiple Hardware PWM channels.
Starting ISR_16_PWMs_Array on ESP32_DEVESP32_PWM v1.3.3CPU 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.00Starting ITimer OK, micros() =2058746Channel :0 Period :1000000OnTime :50000Start_Time :2058951Channel :1 Period :500000OnTime :50000Start_Time :2069589Channel :2 Period :333333OnTime :66666Start_Time :2070006Channel :3 Period :250000OnTime :75000Start_Time :2080650Channel :4 Period :200000OnTime :80000Start_Time :2081082Channel :5 Period :166666OnTime :74999Start_Time :2091764Channel :6 Period :142857OnTime :71428Start_Time :2092203Channel :7 Period :125000OnTime :68750Start_Time :2102906Channel :8 Period :111111OnTime :66666Start_Time :2113570Channel :9 Period :100000OnTime :65000Start_Time :2114007Channel :10 Period :66666OnTime :46666Start_Time :2124648Channel :11 Period :50000OnTime :37500Start_Time :2125104Channel :12 Period :40000OnTime :32000Start_Time :2135783Channel :13 Period :33333OnTime :28333Start_Time :2136239Channel :14 Period :25000OnTime :22500Start_Time :2146919Channel :15 Period :20000OnTime :19000Start_Time :2147367
The following is the sample terminal output when running exampleISR_16_PWMs_Array_Simple onESP32_DEV to demonstrate how to use multiple Hardware PWM channels.
Starting ISR_16_PWMs_Array_Simple on ESP32_DEVESP32_PWM v1.3.3CPU 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.00Starting ITimer OK, micros() =2058739Channel :0 Period :1000000OnTime :50000Start_Time :2058949Channel :1 Period :500000OnTime :50000Start_Time :2069626Channel :2 Period :333333OnTime :66666Start_Time :2070013Channel :3 Period :250000OnTime :75000Start_Time :2080640Channel :4 Period :200000OnTime :80000Start_Time :2081020Channel :5 Period :166666OnTime :74999Start_Time :2091664Channel :6 Period :142857OnTime :71428Start_Time :2102298Channel :7 Period :125000OnTime :68750Start_Time :2102684Channel :8 Period :111111OnTime :66666Start_Time :2113302Channel :9 Period :100000OnTime :65000Start_Time :2113700Channel :10 Period :66666OnTime :46666Start_Time :2124315Channel :11 Period :50000OnTime :37500Start_Time :2124701Channel :12 Period :40000OnTime :32000Start_Time :2135333Channel :13 Period :33333OnTime :28333Start_Time :2135733Channel :14 Period :25000OnTime :22500Start_Time :2146377Channel :15 Period :20000OnTime :19000Start_Time :2156993
The following is the sample terminal output when running exampleISR_Modify_PWM onESP32_DEV to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on ESP32_DEVESP32_PWM v1.3.3CPU 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.00Starting ITimer OK, micros() =2059642Using PWM Freq =200.00, PWM DutyCycle =1.00Channel :0 Period :5000OnTime :50Start_Time :2060337Channel :0 Period :10000OnTime :555Start_Time :12071208Channel :0 Period :5000OnTime :50Start_Time :22066228Channel :0 Period :10000OnTime :555Start_Time :32071347Channel :0 Period :5000OnTime :50Start_Time :42066348Channel :0 Period :10000OnTime :555Start_Time :52072208Channel :0 Period :5000OnTime :50Start_Time :62077247Channel :0 Period :10000OnTime :555Start_Time :72078208Channel :0 Period :5000OnTime :50Start_Time :82078228Channel :0 Period :10000OnTime :555Start_Time :92078347Channel :0 Period :5000OnTime :50Start_Time :102073347Channel :0 Period :10000OnTime :555Start_Time :112079208Channel :0 Period :5000OnTime :50Start_Time :122074208Channel :0 Period :10000OnTime :555Start_Time :132079347Channel :0 Period :5000OnTime :50Start_Time :142084368Channel :0 Period :10000OnTime :555Start_Time :152085208Channel :0 Period :5000OnTime :50Start_Time :162080208
The following is the sample terminal output when running exampleISR_Changing_PWM onESP32_DEV to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on ESP32_DEVESP32_PWM v1.3.3CPU 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.00Starting ITimer OK, micros() =2058761Using PWM Freq =1.00, PWM DutyCycle =50.00Channel :0 Period :1000000OnTime :500000Start_Time :2059443Using PWM Freq =2.00, PWM DutyCycle =90.00Channel :0 Period :500000OnTime :450000Start_Time :12070335Using PWM Freq =1.00, PWM DutyCycle =50.00Channel :0 Period :1000000OnTime :500000Start_Time :22070374Using PWM Freq =2.00, PWM DutyCycle =90.00Channel :0 Period :500000OnTime :450000Start_Time :32070329Using PWM Freq =1.00, PWM DutyCycle =50.00Channel :0 Period :1000000OnTime :500000Start_Time :42070352Using PWM Freq =2.00, PWM DutyCycle =90.00Channel :0 Period :500000OnTime :450000Start_Time :52070349Using PWM Freq =1.00, PWM DutyCycle =50.00Channel :0 Period :1000000OnTime :500000Start_Time :62070352Using PWM Freq =2.00, PWM DutyCycle =90.00Channel :0 Period :500000OnTime :450000Start_Time :72070329
The following is the sample terminal output when running exampleISR_Modify_PWM onESP32S2_DEV to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on ESP32S2_DEVESP32_PWM v1.3.3CPU Frequency =240 MHz[PWM] ESP32_S2_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.00Starting ITimer OK, micros() =2059642Using PWM Freq =200.00, PWM DutyCycle =1.00Channel :0 Period :5000OnTime :50Start_Time :2060337Channel :0 Period :10000OnTime :555Start_Time :12071208Channel :0 Period :5000OnTime :50Start_Time :22066228Channel :0 Period :10000OnTime :555Start_Time :32071347Channel :0 Period :5000OnTime :50Start_Time :42066348Channel :0 Period :10000OnTime :555Start_Time :52072208Channel :0 Period :5000OnTime :50Start_Time :62077247Channel :0 Period :10000OnTime :555Start_Time :72078208Channel :0 Period :5000OnTime :50Start_Time :82078228Channel :0 Period :10000OnTime :555Start_Time :92078347Channel :0 Period :5000OnTime :50Start_Time :102073347Channel :0 Period :10000OnTime :555Start_Time :112079208Channel :0 Period :5000OnTime :50Start_Time :122074208Channel :0 Period :10000OnTime :555Start_Time :132079347Channel :0 Period :5000OnTime :50Start_Time :142084368Channel :0 Period :10000OnTime :555Start_Time :152085208Channel :0 Period :5000OnTime :50Start_Time :162080208
The following is the sample terminal output when running exampleISR_Changing_PWM onESP32S2_DEV to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on ESP32S2_DEVESP32_PWM v1.3.3CPU Frequency = 240 MHz[PWM] ESP32_S2_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.00Starting ITimer OK, micros() = 2563689Using PWM Freq = 1.00, PWM DutyCycle = 50.00Channel : 0 Period : 1000000OnTime : 500000Start_Time : 2568686Using PWM Freq = 2.00, PWM DutyCycle = 90.00Channel : 0 Period : 500000OnTime : 450000Start_Time : 12578679Using PWM Freq = 1.00, PWM DutyCycle = 50.00Channel : 0 Period : 1000000OnTime : 500000Start_Time : 22583648Using PWM Freq = 2.00, PWM DutyCycle = 90.00Channel : 0 Period : 500000OnTime : 450000Start_Time : 32583648The following is the sample terminal output when running exampleISR_Modify_PWM onESP32C3_DEV to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on ESP32C3_DEVESP32_PWM v1.3.3CPU Frequency = 160 MHz[PWM] ESP32_TimerInterrupt: _timerNo = 1 , _fre = 1000000[PWM] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[PWM] _timerIndex = 0 , _timerGroup = 1[PWM] _count = 0 - 20[PWM] timer_set_alarm_value = 20.00Starting ITimer OK, micros() = 2059642Using PWM Freq = 200.00, PWM DutyCycle = 1.00Channel : 0 Period : 5000OnTime : 50Start_Time : 2060337Channel : 0 Period : 10000OnTime : 555Start_Time : 12071208Channel : 0 Period : 5000OnTime : 50Start_Time : 22066228Channel : 0 Period : 10000OnTime : 555Start_Time : 32071347Channel : 0 Period : 5000OnTime : 50Start_Time : 42066348Channel : 0 Period : 10000OnTime : 555Start_Time : 52072208Channel : 0 Period : 5000OnTime : 50Start_Time : 62077247Channel : 0 Period : 10000OnTime : 555Start_Time : 72078208Channel : 0 Period : 5000OnTime : 50Start_Time : 82078228Channel : 0 Period : 10000OnTime : 555Start_Time : 92078347Channel : 0 Period : 5000OnTime : 50Start_Time : 102073347Channel : 0 Period : 10000OnTime : 555Start_Time : 112079208Channel : 0 Period : 5000OnTime : 50Start_Time : 122074208Channel : 0 Period : 10000OnTime : 555Start_Time : 132079347Channel : 0 Period : 5000OnTime : 50Start_Time : 142084368Channel : 0 Period : 10000OnTime : 555Start_Time : 152085208Channel : 0 Period : 5000OnTime : 50Start_Time : 162080208The following is the sample terminal output when running exampleISR_Changing_PWM onESP32C3_DEV to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on ESP32C3_DEVESP32_PWM v1.3.3CPU Frequency = 160 MHz[PWM] ESP32_TimerInterrupt: _timerNo = 1 , _fre = 1000000[PWM] TIMER_BASE_CLK = 80000000 , TIMER_DIVIDER = 80[PWM] _timerIndex = 0 , _timerGroup = 1[PWM] _count = 0 - 20[PWM] timer_set_alarm_value = 20.00Starting ITimer OK, micros() = 2100339Using PWM Freq = 1.00, PWM DutyCycle = 50.00Channel : 0 Period : 1000000OnTime : 500000Start_Time : 2105212Using PWM Freq = 2.00, PWM DutyCycle = 90.00Channel : 0 Period : 500000OnTime : 450000Start_Time : 12117109Using PWM Freq = 1.00, PWM DutyCycle = 50.00Channel : 0 Period : 1000000OnTime : 500000Start_Time : 22122103Using PWM Freq = 2.00, PWM DutyCycle = 90.00Channel : 0 Period : 500000OnTime : 450000Start_Time : 32122107Using PWM Freq = 1.00, PWM DutyCycle = 50.00Channel : 0 Period : 1000000OnTime : 500000Start_Time : 42127102The following is the sample terminal output when running exampleISR_16_PWMs_Array_Complex onESP32S3_DEV to demonstrate the accuracy of ISR Hardware PWM-channels,especially when system is very busy. The ISR PWM-channels isrunning exactly according to corresponding programmed periods and duty-cycles
Starting ISR_16_PWMs_Array_Complex on ESP32S3_DEVESP32_PWM v1.3.3CPU Frequency = 240 MHz[PWM] ESP32_S3_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.00Starting ITimer OK, micros() = 2118162Channel : 0 Period : 1000000OnTime : 50000Start_Time : 2118373Channel : 1 Period : 500000OnTime : 50000Start_Time : 2128833Channel : 2 Period : 333333OnTime : 66666Start_Time : 2129071Channel : 3 Period : 250000OnTime : 75000Start_Time : 2139568Channel : 4 Period : 200000OnTime : 80000Start_Time : 2150027Channel : 5 Period : 166667OnTime : 75000Start_Time : 2150272Channel : 6 Period : 142857OnTime : 71428Start_Time : 2160711Channel : 7 Period : 125000OnTime : 68750Start_Time : 2160971Channel : 8 Period : 111111OnTime : 66666Start_Time : 2171464Channel : 9 Period : 100000OnTime : 65000Start_Time : 2171714Channel : 10 Period : 66667OnTime : 46666Start_Time : 2182194Channel : 11 Period : 50000OnTime : 37500Start_Time : 2192697Channel : 12 Period : 40000OnTime : 32000Start_Time : 2192958Channel : 13 Period : 33333OnTime : 28333Start_Time : 2203438Channel : 14 Period : 25000OnTime : 22500Start_Time : 2203696Channel : 15 Period : 20000OnTime : 19000Start_Time : 2214155SimpleTimer (ms): 2000, us : 12214398, Dus : 10096157PWM Channel : 01000000, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00PWM Channel : 1500000, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00PWM Channel : 2333333, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00PWM Channel : 3250000, programmed Period (us): 250000, actual : 249999, programmed DutyCycle : 30.00, actual : 30.00PWM Channel : 4200000, programmed Period (us): 200000, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00PWM Channel : 5166667, programmed Period (us): 166667, actual : 166680, programmed DutyCycle : 45.00, actual : 45.00PWM Channel : 6142857, programmed Period (us): 142857, actual : 142860, programmed DutyCycle : 50.00, actual : 49.99PWM Channel : 7125000, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99PWM Channel : 8111111, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99PWM Channel : 9100000, programmed Period (us): 100000, actual : 100001, programmed DutyCycle : 65.00, actual : 65.00PWM Channel : 1066667, programmed Period (us): 66667, actual : 66680, programmed DutyCycle : 70.00, actual : 69.98PWM Channel : 1150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00PWM Channel : 1240000, programmed Period (us): 40000, actual : 40000, programmed DutyCycle : 80.00, actual : 80.00PWM Channel : 1333333, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85.00, actual : 84.94PWM Channel : 1425000, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90.00, actual : 90.00PWM Channel : 1520000, programmed Period (us): 20000, actual : 19999, programmed DutyCycle : 95.00, actual : 95.00SimpleTimer (ms): 2000, us : 22375317, Dus : 10160919PWM Channel : 01000000, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00PWM Channel : 1500000, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00PWM Channel : 2333333, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00PWM Channel : 3250000, programmed Period (us): 250000, actual : 250001, programmed DutyCycle : 30.00, actual : 30.00PWM Channel : 4200000, programmed Period (us): 200000, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00PWM Channel : 5166667, programmed Period (us): 166667, actual : 166680, programmed DutyCycle : 45.00, actual : 45.00PWM Channel : 6142857, programmed Period (us): 142857, actual : 142860, programmed DutyCycle : 50.00, actual : 49.99PWM Channel : 7125000, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99PWM Channel : 8111111, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99PWM Channel : 9100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 65.00, actual : 65.00PWM Channel : 1066667, programmed Period (us): 66667, actual : 66679, programmed DutyCycle : 70.00, actual : 69.98PWM Channel : 1150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00PWM Channel : 1240000, programmed Period (us): 40000, actual : 40000, programmed DutyCycle : 80.00, actual : 80.00PWM Channel : 1333333, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85.00, actual : 84.94PWM Channel : 1425000, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90.00, actual : 90.00PWM Channel : 1520000, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95.00, actual : 95.00SimpleTimer (ms): 2000, us : 32536323, Dus : 10161006PWM Channel : 01000000, programmed Period (us): 1000000, actual : 1000000, programmed DutyCycle : 5.00, actual : 5.00PWM Channel : 1500000, programmed Period (us): 500000, actual : 500000, programmed DutyCycle : 10.00, actual : 10.00PWM Channel : 2333333, programmed Period (us): 333333, actual : 333340, programmed DutyCycle : 20.00, actual : 20.00PWM Channel : 3250000, programmed Period (us): 250000, actual : 250001, programmed DutyCycle : 30.00, actual : 30.00PWM Channel : 4200000, programmed Period (us): 200000, actual : 200000, programmed DutyCycle : 40.00, actual : 40.00PWM Channel : 5166667, programmed Period (us): 166667, actual : 166680, programmed DutyCycle : 45.00, actual : 45.00PWM Channel : 6142857, programmed Period (us): 142857, actual : 142861, programmed DutyCycle : 50.00, actual : 49.99PWM Channel : 7125000, programmed Period (us): 125000, actual : 125000, programmed DutyCycle : 55.00, actual : 54.99PWM Channel : 8111111, programmed Period (us): 111111, actual : 111120, programmed DutyCycle : 60.00, actual : 59.99PWM Channel : 9100000, programmed Period (us): 100000, actual : 100000, programmed DutyCycle : 65.00, actual : 64.98PWM Channel : 1066667, programmed Period (us): 66667, actual : 66680, programmed DutyCycle : 70.00, actual : 69.98PWM Channel : 1150000, programmed Period (us): 50000, actual : 50000, programmed DutyCycle : 75.00, actual : 75.00PWM Channel : 1240000, programmed Period (us): 40000, actual : 40001, programmed DutyCycle : 80.00, actual : 80.00PWM Channel : 1333333, programmed Period (us): 33333, actual : 33340, programmed DutyCycle : 85.00, actual : 84.94PWM Channel : 1425000, programmed Period (us): 25000, actual : 25000, programmed DutyCycle : 90.00, actual : 90.01PWM Channel : 1520000, programmed Period (us): 20000, actual : 20000, programmed DutyCycle : 95.00, actual : 95.00Debug is enabled by default on Serial.
You can also change the debugging level_PWM_LOGLEVEL_ from 0 to 4
// These define's must be placed at the beginning before #include "ESP32_PWM.h"// _PWM_LOGLEVEL_ from 0 to 4// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.#define_PWM_LOGLEVEL_4
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to:ESP32_PWM issues
- Search for bug and improvement.
- Similar features for remaining Arduino boards such as SAMD21, SAMD51, SAM-DUE, nRF52, ESP8266, STM32, Portenta_H7, RP2040, etc.
- Basic hardware PWM-channels for ESP32, ESP32_C2 and ESP32_C3 forESP32 core v2.0.0+
- Longer time interval
- Add complex examples.
- Add functions to modify PWM settings on-the-fly
- Fix examples to use with ESP32 core v2.0.1+
- Fix
multiple-definitionslinker error. Dropsrc_cppandsrc_hdirectories - Add examplemultiFileProject to demo for multiple-file project
- Improve accuracy by using
float, instead ofuint32_tfordutycycle - DutyCycle to be optionally updated at the end current PWM period instead of immediately.
- Add support to
ESP32-S3 - Display informational warning only when
_PWM_LOGLEVEL_> 3 - Remove crashing
PIN_D24from examples
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed underMIT
Copyright 2021- Khoi Hoang
About
This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2 or ESP32_C3-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 chann…
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
