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

Commit 0766f2b

Browse files
authored
v1.0.1 to add PWM_StepperControl example
### Releases v1.0.1 1. Add example [PWM_StepperControl](https://github.com/khoih-prog/ESP32_FastPWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
1 parent 805e5d2 commit 0766f2b

File tree

11 files changed

+46272
-29
lines changed

11 files changed

+46272
-29
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ However, before reporting a bug please check through the following:
1010

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

13+
---
14+
1315
### How to submit a bug report
1416

1517
Please ensure to specify the following:
1618

1719
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18-
* `ESP32` Core Version (e.g. ESP32 core v2.0.5)
20+
* `ESP32` Core Version (e.g. ESP32 core v2.0.6)
1921
* `ESP32` Board type (e.g. ESP32_DEV Module, etc.)
2022
* `ESP32-S2` Board type (e.g. ESP32S2_DEV Module, ESP32_S2_Saola, etc.)
2123
* `ESP32_S3` Board type (e.g. ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)
@@ -27,14 +29,18 @@ Please ensure to specify the following:
2729
* Network configuration
2830

2931

32+
Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted.
33+
34+
---
35+
3036
### Example
3137

3238
```
3339
Arduino IDE version: 1.8.19
34-
ESP32 core v2.0.5
40+
ESP32 core v2.0.6
3541
ESP32S3_DEV Module
3642
OS: Ubuntu 20.04 LTS
37-
Linux xy-Inspiron-3593 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
43+
Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
3844
3945
Context:
4046
I encountered a crash while using TimerInterrupt.

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
1111
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>
1212

13+
1314
---
1415
---
1516

@@ -43,6 +44,7 @@
4344
* [ 5. PWM_Multi](examples/PWM_Multi)
4445
* [ 6. PWM_MultiChannel](examples/PWM_MultiChannel)
4546
* [ 7. PWM_Waveform](examples/PWM_Waveform)
47+
* [ 8. PWM_StepperControl](examples/PWM_StepperControl) **New**
4648
* [Example PWM_Multi](#example-PWM_Multi)
4749
* [Debug Terminal Output Samples](#debug-terminal-output-samples)
4850
* [1. PWM_DynamicDutyCycle on ESP32_DEV](#1-PWM_DynamicDutyCycle-on-ESP32_DEV)
@@ -142,7 +144,7 @@ Functions using normal software-based PWMs, relying on loop() and calling millis
142144
## Prerequisites
143145

144146
1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest)
145-
2. [`ESP32 Core 2.0.5+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/).
147+
2. [`ESP32 Core 2.0.6+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/).
146148

147149

148150

@@ -185,33 +187,33 @@ Please have a look at [**ESP_WiFiManager Issue 39: Not able to read analog port
185187

186188
#### 2. ESP32 ADCs functions
187189

188-
- ADC1 controls ADC function for pins **GPIO32-GPIO39**
189-
- ADC2 controls ADC function for pins **GPIO0, 2, 4, 12-15, 25-27**
190+
- `ADC1` controls ADC function for pins **GPIO32-GPIO39**
191+
- `ADC2` controls ADC function for pins **GPIO0, 2, 4, 12-15, 25-27**
190192

191193
#### 3.. ESP32 WiFi uses ADC2 for WiFi functions
192194

193195
Look in file [**adc_common.c**](https://github.com/espressif/esp-idf/blob/master/components/driver/adc_common.c#L61)
194196

195-
> In ADC2, there're two locks used for different cases:
197+
> In `ADC2`, there're two locks used for different cases:
196198
> 1. lock shared with app and Wi-Fi:
197199
> ESP32:
198-
> When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.
200+
> When Wi-Fi using the `ADC2`, we assume it will never stop, so app checks the lock and returns immediately if failed.
199201
> ESP32S2:
200202
> The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
201203
>
202204
> 2. lock shared between tasks:
203-
> when several tasks sharing the ADC2, we want to guarantee
205+
> when several tasks sharing the `ADC2`, we want to guarantee
204206
> all the requests will be handled.
205207
> Since conversions are short (about 31us), app returns the lock very soon,
206208
> we use a spinlock to stand there waiting to do conversions one by one.
207209
>
208210
> adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
209211
210212

211-
- In order to use ADC2 for other functions, we have to **acquire complicated firmware locks and very difficult to do**
212-
- So, it's not advisable to use ADC2 with WiFi/BlueTooth (BT/BLE).
213-
- Use ADC1, and pins GPIO32-GPIO39
214-
- 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 the **fix mentioned at the end** of [**ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to work with ESP32 WiFi/BlueTooth (BT/BLE).
213+
- In order to use `ADC2` for other functions, we have to **acquire complicated firmware locks and very difficult to do**
214+
- So, it's not advisable to use `ADC2` with WiFi/BlueTooth (BT/BLE).
215+
- Use `ADC1`, and pins **GPIO32-GPIO39**
216+
- 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 the **fix mentioned at the end** of [**ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to work with ESP32 WiFi/BlueTooth (BT/BLE)
215217

216218
---
217219
---
@@ -349,6 +351,7 @@ PWM_Instance->setPWM_manual(PWM_Pins, new_level);
349351
5. [PWM_Multi](examples/PWM_Multi)
350352
6. [PWM_MultiChannel](examples/PWM_MultiChannel)
351353
7. [PWM_Waveform](examples/PWM_Waveform)
354+
8. [PWM_StepperControl](examples/PWM_StepperControl) **New**
352355

353356

354357
---
@@ -371,7 +374,7 @@ The following is the sample terminal output when running example [PWM_DynamicDut
371374

372375
```cpp
373376
Starting PWM_DynamicDutyCycle on ESP32_DEV
374-
ESP32_FastPWM v1.0.0
377+
ESP32_FastPWM v1.0.1
375378
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 8 , LEDC_CHANNELS = 16 , LEDC_MAX_BIT_WIDTH = 20
376379
[PWM] ESP32_FastPWM: _dutycycle = 32768
377380
[PWM] setPWM_Int: _dutycycle = 128 , DC % = 50.00
@@ -404,7 +407,7 @@ The following is the sample terminal output when running example [**PWM_Multi**]
404407
405408
```cpp
406409
Starting PWM_Multi on ESP32_DEV
407-
ESP32_FastPWM v1.0.0
410+
ESP32_FastPWM v1.0.1
408411
[PWM] ESP32_FastPWM: new _channel = 0
409412
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 8 , LEDC_CHANNELS = 16 , LEDC_MAX_BIT_WIDTH = 20
410413
[PWM] ESP32_FastPWM: _dutycycle = 6553
@@ -450,7 +453,7 @@ The following is the sample terminal output when running example [**PWM_DynamicF
450453

451454
```cpp
452455
Starting PWM_DynamicFreq on ESP32S3_DEV
453-
ESP32_FastPWM v1.0.0
456+
ESP32_FastPWM v1.0.1
454457
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 8 , LEDC_CHANNELS = 8 , LEDC_MAX_BIT_WIDTH = 14
455458
[PWM] ESP32_FastPWM: _dutycycle = 32768
456459
[PWM] setPWM_Int: _dutycycle = 128 , DC % = 50.00
@@ -495,7 +498,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
495498
496499
```cpp
497500
Starting PWM_Waveform on ESP32S2_DEV
498-
ESP32_FastPWM v1.0.0
501+
ESP32_FastPWM v1.0.1
499502
[PWM] ESP32_FastPWM: new _channel = 0
500503
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 8 , LEDC_CHANNELS = 8 , LEDC_MAX_BIT_WIDTH = 14
501504
[PWM] ESP32_FastPWM: _dutycycle = 0
@@ -556,7 +559,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
556559

557560
```cpp
558561
Starting PWM_Waveform on ESP32C3_DEV
559-
ESP32_FastPWM v1.0.0
562+
ESP32_FastPWM v1.0.1
560563
[PWM] ESP32_FastPWM: new _channel = 0
561564
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 6 , LEDC_CHANNELS = 6 , LEDC_MAX_BIT_WIDTH = 14
562565
[PWM] ESP32_FastPWM: _dutycycle = 0
@@ -617,7 +620,7 @@ The following is the sample terminal output when running example [**PWM_Waveform
617620
618621
```cpp
619622
Starting PWM_Waveform on ESP32S3_DEV
620-
ESP32_FastPWM v1.0.0
623+
ESP32_FastPWM v1.0.1
621624
[PWM] ESP32_FastPWM: new _channel = 0
622625
[PWM] ESP32_FastPWM: SOC_LEDC_CHANNEL_NUM = 8 , LEDC_CHANNELS = 8 , LEDC_MAX_BIT_WIDTH = 14
623626
[PWM] ESP32_FastPWM: _dutycycle = 0
@@ -706,14 +709,13 @@ Submit issues to: [ESP32_FastPWM issues](https://github.com/khoih-prog/ESP32_Fas
706709
## TO DO
707710

708711
1. Search for bug and improvement.
709-
2. Similar features for remaining Arduino boards such as SAMD21, SAMD51, SAM-DUE, nRF52, ESP8266, STM32, Portenta_H7, RP2040, etc.
710-
711712

713+
---
712714

713715
## DONE
714716

715717
1. Basic hardware PWM-channels for `ESP32, ESP32_S2, ESP32_S3 and ESP32_C3` using [ESP32 core](https://github.com/espressif/arduino-esp32)
716-
718+
2. Add example [PWM_StepperControl](https://github.com/khoih-prog/ESP32_FastPWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM
717719

718720
---
719721
---
@@ -722,12 +724,23 @@ Submit issues to: [ESP32_FastPWM issues](https://github.com/khoih-prog/ESP32_Fas
722724

723725
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
724726

727+
1. Thanks to [Paul van Dinther](https://github.com/dinther) for proposing new way to use PWM to drive Stepper-Motor in [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16), leading to v1.0.1
728+
729+
730+
<table>
731+
<tr>
732+
<td align="center"><a href="https://github.com/dinther"><img src="https://github.com/dinther.png" width="100px;" alt="dinther"/><br /><sub><b>Paul van Dinther</b></sub></a><br /></td>
733+
</tr>
734+
</table>
735+
736+
725737

726738
---
727739

728740
## Contributing
729741

730742
If you want to contribute to this project:
743+
731744
- Report bugs and errors
732745
- Ask for enhancements
733746
- Create issues and pull requests

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@
1717
## Table of Contents
1818

1919
* [Changelog](#changelog)
20+
* [Releases v1.0.1](#releases-v101)
2021
* [Releases v1.0.0](#releases-v100)
2122

2223
---
2324
---
2425

2526
## Changelog
2627

28+
### Releases v1.0.1
29+
30+
1. Add example [PWM_StepperControl](https://github.com/khoih-prog/ESP32_FastPWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM. Check [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16)
31+
2732

2833
### Releases v1.0.0
2934

3035
1. Initial coding for ESP32, ESP32_S2, ESP32_S3 and ESP32_C3 boards using [ESP32 core](https://github.com/espressif/arduino-esp32)
36+
37+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/****************************************************************************************************************************
2+
PWM_StepperControl.ino
3+
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_FastPWM
7+
Licensed under MIT license
8+
9+
This is pure hardware-based PWM
10+
11+
Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16
12+
*****************************************************************************************************************************/
13+
/******************************************************************************************************************************
14+
// All GPIO pins (but GPIO34-39) can be used to generate PWM
15+
// For ESP32, number of channels is 16, max 20-bit resolution
16+
// For ESP32_S2, ESP32_S3, number of channels is 8
17+
// For ESP32_C3, number of channels is 6
18+
******************************************************************************************************************************/
19+
20+
// Use with Stepper-Motor driver, such as TMC2209
21+
22+
#define _PWM_LOGLEVEL_ 4
23+
24+
#include "ESP32_FastPWM.h"
25+
26+
#if ARDUINO_ESP32C3_DEV
27+
#define STEP_PIN 9
28+
#else
29+
#define STEP_PIN 16
30+
#endif
31+
32+
#define DIR_PIN 4
33+
34+
// Max resolution is 20-bit
35+
// Resolution 65536 (16-bit) for lower frequencies, OK @ 1K
36+
// Resolution 4096 (12-bit) for lower frequencies, OK @ 10K
37+
// Resolution 1024 (10-bit) for higher frequencies, OK @ 50K
38+
// Resolution 256 ( 8-bit)for higher frequencies, OK @ 100K, 200K
39+
// Resolution 128 ( 7-bit) for higher frequencies, OK @ 500K
40+
int PWM_resolution = 10;
41+
42+
//creates pwm instance
43+
ESP32_FAST_PWM* stepper;
44+
45+
void setSpeed(int speed)
46+
{
47+
if (speed == 0)
48+
{
49+
// Use DC = 0 to stop stepper
50+
stepper->setPWM(STEP_PIN, 500, 0);
51+
}
52+
else
53+
{
54+
// Set the frequency of the PWM output and a duty cycle of 50%
55+
digitalWrite(DIR_PIN, (speed < 0));
56+
stepper->setPWM(STEP_PIN, abs(speed), 50);
57+
}
58+
}
59+
60+
void setup()
61+
{
62+
pinMode(DIR_PIN, OUTPUT);
63+
64+
Serial.begin(115200);
65+
66+
while (!Serial && millis() < 5000);
67+
68+
delay(100);
69+
70+
Serial.print(F("\nStarting PWM_StepperControl on "));
71+
Serial.println(ARDUINO_BOARD);
72+
Serial.println(ESP32_FAST_PWM_VERSION);
73+
74+
// Create PWM object and passed just a random frequency of 500
75+
// The duty cycle is how you turn the motor on and off
76+
stepper = new ESP32_FAST_PWM(STEP_PIN, 500, 0);
77+
}
78+
79+
void loop()
80+
{
81+
setSpeed(1000);
82+
delay(3000);
83+
84+
// Stop before reversing
85+
setSpeed(0);
86+
delay(3000);
87+
88+
// Reversing
89+
setSpeed(-500);
90+
delay(3000);
91+
92+
// Stop before reversing
93+
setSpeed(0);
94+
delay(3000);
95+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-License-Identifier: GPL-2.0-or-later
2+
#
3+
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
4+
#
5+
# For example, OpenOCD can be started for ESP32 debugging on
6+
#
7+
# openocd -f board/esp32-wrover-kit-3.3v.cfg
8+
#
9+
10+
# Source the JTAG interface configuration file
11+
source [find interface/ftdi/esp32_devkitj_v1.cfg]
12+
set ESP32_FLASH_VOLTAGE 3.3
13+
# Source the ESP32 configuration file
14+
source [find target/esp32.cfg]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name":"Arduino on ESP32",
3+
"toolchainPrefix":"xtensa-esp32-elf",
4+
"svdFile":"debug.svd",
5+
"request":"attach",
6+
"postAttachCommands":[
7+
"set remote hardware-watchpoint-limit 2",
8+
"monitor reset halt",
9+
"monitor gdb_sync",
10+
"thb setup",
11+
"c"
12+
],
13+
"overrideRestartCommands":[
14+
"monitor reset halt",
15+
"monitor gdb_sync",
16+
"thb setup",
17+
"c"
18+
]
19+
}

0 commit comments

Comments
 (0)