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

Commit

Permalink
v1.0.0 for hardware-PWM on ESP32 boards
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
khoih-prog committed Nov 1, 2022
1 parent 5f23c78 commit 09ea7da
Show file tree
Hide file tree
Showing 17 changed files with 1,684 additions and 0 deletions.
81 changes: 81 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
## Contributing to ESP32_FastPWM

### Reporting Bugs

Please report bugs in [ESP32_FastPWM Issues](https://github.com/khoih-prog/ESP32_FastPWM/issues) if you find them.

However, before reporting a bug please check through the following:

* [Existing Open Issues](https://github.com/khoih-prog/ESP32_FastPWM/issues) - someone might have already encountered this.

If you don't find anything, please [open a new issue](https://github.com/khoih-prog/ESP32_FastPWM/issues/new).

### How to submit a bug report

Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `ESP32` Core Version (e.g. ESP32 core v2.0.5)
* `ESP32` Board type (e.g. ESP32_DEV Module, etc.)
* `ESP32-S2` Board type (e.g. ESP32S2_DEV Module, ESP32_S2_Saola, etc.)
* `ESP32_S3` Board type (e.g. ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)
* `ESP32-C3` Board type (e.g. ESP32C3_DEV Module, etc.)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
* Network configuration


### Example

```
Arduino IDE version: 1.8.19
ESP32 core v2.0.5
ESP32S3_DEV Module
OS: Ubuntu 20.04 LTS
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
Context:
I encountered a crash while using TimerInterrupt.
Steps to reproduce:
1. ...
2. ...
3. ...
4. ...
```

### Additional context

Add any other context about the problem here.

---

### Sending Feature Requests

Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.

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.

---

### Sending Pull Requests

Pull Requests with changes and fixes are also welcome!

Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)

1. Change directory to the library GitHub

```
xy@xy-Inspiron-3593:~$ cd Arduino/xy/ESP32_FastPWM_GitHub/
xy@xy-Inspiron-3593:~/Arduino/xy/ESP32_FastPWM_GitHub$
```

2. Issue astyle command

```
xy@xy-Inspiron-3593:~/Arduino/xy/ESP32_FastPWM_GitHub$ bash utils/restyle.sh
```

25 changes: 25 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ESP32_FastPWM Library

[![arduino-library-badge](https://www.ardu-badge.com/badge/ESP32_FastPWM.svg?)](https://www.ardu-badge.com/ESP32_FastPWM)
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP32_FastPWM.svg)](https://github.com/khoih-prog/ESP32_FastPWM/releases)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ESP32_FastPWM/blob/master/LICENSE)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP32_FastPWM.svg)](http://github.com/khoih-prog/ESP32_FastPWM/issues)

---
---

## Table of Contents

* [Changelog](#changelog)
* [Releases v1.0.0](#releases-v100)

---
---

## Changelog


### 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)
74 changes: 74 additions & 0 deletions examples/PWM_Basic/PWM_Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/****************************************************************************************************************************
PWM_Basic.ino
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// All GPIO pins (but GPIO34-39) can be used to generate PWM
// For ESP32, number of channels is 16, max 20-bit resolution
// For ESP32_S2, ESP32_S3, number of channels is 8
// For ESP32_C3, number of channels is 6
******************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

#include "ESP32_FastPWM.h"

#if ARDUINO_ESP32C3_DEV
#define pinToUse 9
#else
#define pinToUse 16
#endif

// Max resolution is 20-bit
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
int PWM_resolution = 12;

//creates pwm instance
ESP32_FAST_PWM* PWM_Instance;

float frequency = 1000.0f;
float dutyCycle = 0.0f;

uint8_t channel = 0;

void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(500);

Serial.print(F("\nStarting PWM_Basic on "));
Serial.println(ARDUINO_BOARD);
Serial.println(ESP32_FAST_PWM_VERSION);

//assigns PWM frequency of 1.0 KHz and a duty cycle of 0%, channel 0, 12-bit resolution
PWM_Instance = new ESP32_FAST_PWM(pinToUse, frequency, dutyCycle, channel, PWM_resolution);
}

void loop()
{
frequency = 2000.0f;
dutyCycle = 20.0f;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

delay(5000);
dutyCycle = 90.0f;

PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

delay(5000);
}
109 changes: 109 additions & 0 deletions examples/PWM_DynamicDutyCycle/PWM_DynamicDutyCycle.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/****************************************************************************************************************************
PWM_DynamicDutyCycle.ino
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
Licensed under MIT license
This is pure hardware-based PWM
*****************************************************************************************************************************/
/******************************************************************************************************************************
// All GPIO pins (but GPIO34-39) can be used to generate PWM
// For ESP32, number of channels is 16, max 20-bit resolution
// For ESP32_S2, ESP32_S3, number of channels is 8
// For ESP32_C3, number of channels is 6
******************************************************************************************************************************/

#define _PWM_LOGLEVEL_ 4

#include "ESP32_FastPWM.h"

#if ARDUINO_ESP32C3_DEV
#define pinToUse 9
#else
#define pinToUse 16
#endif

// Max resolution is 20-bit
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
int PWM_resolution = 12;

//creates pwm instance
ESP32_FAST_PWM* PWM_Instance;

float frequency;
float dutyCycle;

char dashLine[] = "=====================================================================================";

void printPWMInfo(ESP32_FAST_PWM* PWM_Instance)
{
Serial.println(dashLine);
Serial.print("Actual data: pin = ");
Serial.print(PWM_Instance->getPin());
Serial.print(", PWM DC = ");
Serial.print(PWM_Instance->getActualDutyCycle());
Serial.print(", PWMPeriod = ");
Serial.print(PWM_Instance->getPWMPeriod());
Serial.print(", PWM Freq (Hz) = ");
Serial.println(PWM_Instance->getActualFreq(), 4);
Serial.println(dashLine);
}

void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

delay(500);

Serial.print(F("\nStarting PWM_DynamicDutyCycle on "));
Serial.println(ARDUINO_BOARD);
Serial.println(ESP32_FAST_PWM_VERSION);

frequency = 5000.0f;

PWM_Instance = new ESP32_FAST_PWM(pinToUse, frequency, 50.0f);

if (PWM_Instance)
{
if (!PWM_Instance->setPWM())
{
Serial.println(F("Stop here"));

// stop here
while (true)
delay(1000);
}
}

Serial.println(dashLine);
}

void loop()
{
dutyCycle = 90.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);

printPWMInfo(PWM_Instance);

delay(5000);
dutyCycle = 20.0f;

Serial.print(F("Change PWM DutyCycle to "));
Serial.println(dutyCycle);
PWM_Instance->setPWM(pinToUse, frequency, dutyCycle);
printPWMInfo(PWM_Instance);

delay(5000);
}
Loading

0 comments on commit 09ea7da

Please sign in to comment.