Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 09ea7da

Browse files
authored
v1.0.0 for hardware-PWM on ESP32 boards
### Releases v1.0.0 1. Initial coding for ESP32, ESP32_S2, ESP32_S3 and ESP32_C3 boards using [ESP32 core](https://github.com/espressif/arduino-esp32)
1 parent 5f23c78 commit 09ea7da

File tree

17 files changed

+1684
-0
lines changed

17 files changed

+1684
-0
lines changed

CONTRIBUTING.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Contributing to ESP32_FastPWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in [ESP32_FastPWM Issues](https://github.com/khoih-prog/ESP32_FastPWM/issues) if you find them.
6+
7+
However, before reporting a bug please check through the following:
8+
9+
* [Existing Open Issues](https://github.com/khoih-prog/ESP32_FastPWM/issues) - someone might have already encountered this.
10+
11+
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/ESP32_FastPWM/issues/new).
12+
13+
### How to submit a bug report
14+
15+
Please ensure to specify the following:
16+
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18+
* `ESP32` Core Version (e.g. ESP32 core v2.0.5)
19+
* `ESP32` Board type (e.g. ESP32_DEV Module, etc.)
20+
* `ESP32-S2` Board type (e.g. ESP32S2_DEV Module, ESP32_S2_Saola, etc.)
21+
* `ESP32_S3` Board type (e.g. ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)
22+
* `ESP32-C3` Board type (e.g. ESP32C3_DEV Module, etc.)
23+
* Contextual information (e.g. what you were trying to achieve)
24+
* Simplest possible steps to reproduce
25+
* Anything that might be relevant in your opinion, such as:
26+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
27+
* Network configuration
28+
29+
30+
### Example
31+
32+
```
33+
Arduino IDE version: 1.8.19
34+
ESP32 core v2.0.5
35+
ESP32S3_DEV Module
36+
OS: Ubuntu 20.04 LTS
37+
Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
38+
39+
Context:
40+
I encountered a crash while using TimerInterrupt.
41+
42+
Steps to reproduce:
43+
1. ...
44+
2. ...
45+
3. ...
46+
4. ...
47+
```
48+
49+
### Additional context
50+
51+
Add any other context about the problem here.
52+
53+
---
54+
55+
### Sending Feature Requests
56+
57+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
58+
59+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/ESP32_FastPWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
60+
61+
---
62+
63+
### Sending Pull Requests
64+
65+
Pull Requests with changes and fixes are also welcome!
66+
67+
Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)
68+
69+
1. Change directory to the library GitHub
70+
71+
```
72+
xy@xy-Inspiron-3593:~$ cd Arduino/xy/ESP32_FastPWM_GitHub/
73+
xy@xy-Inspiron-3593:~/Arduino/xy/ESP32_FastPWM_GitHub$
74+
```
75+
76+
2. Issue astyle command
77+
78+
```
79+
xy@xy-Inspiron-3593:~/Arduino/xy/ESP32_FastPWM_GitHub$ bash utils/restyle.sh
80+
```
81+

changelog.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ESP32_FastPWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/ESP32_FastPWM.svg?)](https://www.ardu-badge.com/ESP32_FastPWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP32_FastPWM.svg)](https://github.com/khoih-prog/ESP32_FastPWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ESP32_FastPWM/blob/master/LICENSE)
6+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
7+
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP32_FastPWM.svg)](http://github.com/khoih-prog/ESP32_FastPWM/issues)
8+
9+
---
10+
---
11+
12+
## Table of Contents
13+
14+
* [Changelog](#changelog)
15+
* [Releases v1.0.0](#releases-v100)
16+
17+
---
18+
---
19+
20+
## Changelog
21+
22+
23+
### Releases v1.0.0
24+
25+
1. Initial coding for ESP32, ESP32_S2, ESP32_S3 and ESP32_C3 boards using [ESP32 core](https://github.com/espressif/arduino-esp32)

examples/PWM_Basic/PWM_Basic.ino

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************************************************************
2+
PWM_Basic.ino
3+
4+
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
8+
Licensed under MIT license
9+
10+
This is pure hardware-based PWM
11+
*****************************************************************************************************************************/
12+
/******************************************************************************************************************************
13+
// All GPIO pins (but GPIO34-39) can be used to generate PWM
14+
// For ESP32, number of channels is 16, max 20-bit resolution
15+
// For ESP32_S2, ESP32_S3, number of channels is 8
16+
// For ESP32_C3, number of channels is 6
17+
******************************************************************************************************************************/
18+
19+
#define _PWM_LOGLEVEL_ 4
20+
21+
#include "ESP32_FastPWM.h"
22+
23+
#if ARDUINO_ESP32C3_DEV
24+
#define pinToUse 9
25+
#else
26+
#define pinToUse 16
27+
#endif
28+
29+
// Max resolution is 20-bit
30+
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
31+
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
32+
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
33+
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
34+
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
35+
int PWM_resolution = 12;
36+
37+
//creates pwm instance
38+
ESP32_FAST_PWM* PWM_Instance;
39+
40+
float frequency = 1000.0f;
41+
float dutyCycle = 0.0f;
42+
43+
uint8_t channel = 0;
44+
45+
void setup()
46+
{
47+
Serial.begin(115200);
48+
49+
while (!Serial && millis() < 5000);
50+
51+
delay(500);
52+
53+
Serial.print(F("\nStarting PWM_Basic on "));
54+
Serial.println(ARDUINO_BOARD);
55+
Serial.println(ESP32_FAST_PWM_VERSION);
56+
57+
//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%, channel 0, 12-bit resolution
58+
PWM_Instance = new ESP32_FAST_PWM(pinToUse, frequency, dutyCycle, channel, PWM_resolution);
59+
}
60+
61+
void loop()
62+
{
63+
frequency = 2000.0f;
64+
dutyCycle = 20.0f;
65+
66+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
67+
68+
delay(5000);
69+
dutyCycle = 90.0f;
70+
71+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
72+
73+
delay(5000);
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/****************************************************************************************************************************
2+
PWM_DynamicDutyCycle.ino
3+
4+
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
8+
Licensed under MIT license
9+
10+
This is pure hardware-based PWM
11+
*****************************************************************************************************************************/
12+
/******************************************************************************************************************************
13+
// All GPIO pins (but GPIO34-39) can be used to generate PWM
14+
// For ESP32, number of channels is 16, max 20-bit resolution
15+
// For ESP32_S2, ESP32_S3, number of channels is 8
16+
// For ESP32_C3, number of channels is 6
17+
******************************************************************************************************************************/
18+
19+
#define _PWM_LOGLEVEL_ 4
20+
21+
#include "ESP32_FastPWM.h"
22+
23+
#if ARDUINO_ESP32C3_DEV
24+
#define pinToUse 9
25+
#else
26+
#define pinToUse 16
27+
#endif
28+
29+
// Max resolution is 20-bit
30+
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
31+
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
32+
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
33+
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
34+
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
35+
int PWM_resolution = 12;
36+
37+
//creates pwm instance
38+
ESP32_FAST_PWM* PWM_Instance;
39+
40+
float frequency;
41+
float dutyCycle;
42+
43+
char dashLine[] = "=====================================================================================";
44+
45+
void printPWMInfo(ESP32_FAST_PWM* PWM_Instance)
46+
{
47+
Serial.println(dashLine);
48+
Serial.print("Actual data: pin = ");
49+
Serial.print(PWM_Instance->getPin());
50+
Serial.print(", PWM DC = ");
51+
Serial.print(PWM_Instance->getActualDutyCycle());
52+
Serial.print(", PWMPeriod = ");
53+
Serial.print(PWM_Instance->getPWMPeriod());
54+
Serial.print(", PWM Freq (Hz) = ");
55+
Serial.println(PWM_Instance->getActualFreq(), 4);
56+
Serial.println(dashLine);
57+
}
58+
59+
void setup()
60+
{
61+
Serial.begin(115200);
62+
63+
while (!Serial && millis() < 5000);
64+
65+
delay(500);
66+
67+
Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
68+
Serial.println(ARDUINO_BOARD);
69+
Serial.println(ESP32_FAST_PWM_VERSION);
70+
71+
frequency = 5000.0f;
72+
73+
PWM_Instance = new ESP32_FAST_PWM(pinToUse, frequency, 50.0f);
74+
75+
if (PWM_Instance)
76+
{
77+
if (!PWM_Instance->setPWM())
78+
{
79+
Serial.println(F("Stop here"));
80+
81+
// stop here
82+
while (true)
83+
delay(1000);
84+
}
85+
}
86+
87+
Serial.println(dashLine);
88+
}
89+
90+
void loop()
91+
{
92+
dutyCycle = 90.0f;
93+
94+
Serial.print(F("Change PWM DutyCycle to "));
95+
Serial.println(dutyCycle);
96+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
97+
98+
printPWMInfo(PWM_Instance);
99+
100+
delay(5000);
101+
dutyCycle = 20.0f;
102+
103+
Serial.print(F("Change PWM DutyCycle to "));
104+
Serial.println(dutyCycle);
105+
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
106+
printPWMInfo(PWM_Instance);
107+
108+
delay(5000);
109+
}

0 commit comments

Comments
 (0)