ESP32_PWM Library

khoih-prog
Posts: 53
Joined: Sat Feb 22, 2020 8:16 pm

ESP32_PWM Library

Postby khoih-prog » Tue Sep 21, 2021 6:37 am

ESP32_PWM Library
How To Install Using Arduino Library Manager


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.


Initial Release v1.0.0

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


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 supported yet


Examples:

1. ISR_16_PWMs_Array
2. ISR_16_PWMs_Array_Complex
3. ISR_16_PWMs_Array_Simple

Example ISR_16_PWMs_Array_Complex on ESP32_DEV

The following is the sample terminal output when running example ISR_16_PWMs_Array_Complex on ESP32_DEV 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

Code: Select all

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

Eerhie
Posts: 2
Joined: Sat Sep 25, 2021 3:14 pm

Re: ESP32_PWM Library

Postby Eerhie » Sat Sep 25, 2021 3:36 pm

Hello!
It sounds interesting for me.

My task is, to control four brushed motors with PWM.

Is your library useful for me?

Thank you for discussion
Eerhie

khoih-prog
Posts: 53
Joined: Sat Feb 22, 2020 8:16 pm

Re: ESP32_PWM Library

Postby khoih-prog » Wed Sep 29, 2021 12:15 am

My task is, to control four brushed motors with PWM.

I'm sorry I don't know and can't help, and I'm afraid nobody can know and help with that kind of info.

You have to read / understand the motor specs, and know if PWM can be used for them, then which is required PWM frequency / duty-cycle, and how to connect from ESP32 to those motors (drivers, power, etc.).

I suggest you post the question in the correct forum, certainly with more info about those motors (specs., type, model, etc).

This library is just providing the PWM according to requested PWM frequency / duty-cycle, certainly if they are within the supporting range.

Good Luck,

Who is online

Users browsing this forum: No registered users and 18 guests