Skip to content

Commit 5c3ce86

Browse files
committed
Update
* Fixes wr_duty() function * Add new servo example * Update readme
1 parent 7e3b09c commit 5c3ce86

File tree

5 files changed

+75
-16
lines changed

5 files changed

+75
-16
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ Also included are non blocking Tone and Note functions that include *duration* a
1616

1717
**Simulation Examples:**
1818

19+
- [16 PWM Fade](https://wokwi.com/projects/349232255258853970)
20+
- [14 PWM Fade 2 Servo](https://wokwi.com/projects/349978851105833554)
21+
- [Servo Sweep](https://wokwi.com/projects/350037178957431378)
1922
- [Servo_Sweep_Inverted](https://wokwi.com/projects/351967394028061269)
20-
- [ESP32 Note Explorer ♩ ♪ ♫ ♬](https://wokwi.com/projects/351231798778266200)
21-
- [playingNotes](https://wokwi.com/projects/351175246893548120)
22-
- [playingTones](https://wokwi.com/projects/352178590336932865)
23-
- [ESP32_ServoSweep_NonBlockingTone_Fade](https://wokwi.com/projects/350973592395055698)
24-
- [16 pwm fade](https://wokwi.com/projects/349232255258853970)
25-
- [14 pwm fade 2 servo](https://wokwi.com/projects/349978851105833554)
23+
- [Dual Servo Sweep with Independent Speed Control](https://wokwi.com/projects/351978833396630095)
2624
- [Servo Knob](https://wokwi.com/projects/350033311963284051)
27-
- [Servo Sweep](https://wokwi.com/projects/350037178957431378)
28-
- [3 phase 40kHz](https://wokwi.com/projects/349336125753524820)
29-
- [2 sync 300kHz](https://wokwi.com/projects/349322326995632722)
30-
- [8 sync 20kHz](https://wokwi.com/projects/349319723103552084)
31-
- [ESP32_3phase_10kHz.ino](https://wokwi.com/projects/334722465700774482)
32-
- [ESP32_S2_3phase_10kHz.ino](https://wokwi.com/projects/334765722024542804)
33-
- [ESP32_C3_3phase_10kHz.ino](https://wokwi.com/projects/334856585002091092)
25+
- [Note Explorer ♩ ♪ ♫ ♬](https://wokwi.com/projects/351231798778266200)
26+
- [Playing Notes](https://wokwi.com/projects/351175246893548120)
27+
- [Playing Tones](https://wokwi.com/projects/352178590336932865)
28+
- [Servo Sweep with Non Blocking Tone and Fade](https://wokwi.com/projects/350973592395055698)
29+
- [2 Sync 300kHz](https://wokwi.com/projects/349322326995632722)
30+
- [8 Sync 20kHz](https://wokwi.com/projects/349319723103552084)
31+
- [ESP32_3-Phase 40kHz](https://wokwi.com/projects/349336125753524820)
32+
- [ESP32_3-Phase_10kHz](https://wokwi.com/projects/334722465700774482)
33+
- [ESP32_S2_3-Phase_10kHz](https://wokwi.com/projects/334765722024542804)
34+
- [ESP32_C3_3-Phase_10kHz](https://wokwi.com/projects/334856585002091092)
3435

3536
| Board | PWM Pins | PWM, Duty and Phase Channels | Frequency and Resolution Channels |
3637
| -------- | --------------------------------- | ---------------------------- | --------------------------------- |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Controls 2 servo positions and speed from 0-180 deg and back
3+
https://wokwi.com/projects/351978833396630095
4+
by dlloydev, December 2022.
5+
*/
6+
7+
#include <pwmWrite.h>
8+
9+
Pwm pwm = Pwm();
10+
11+
const int speedPin1 = 32, speedPin2 = 12;
12+
const int servoPin1 = 14, servoPin2 = 13;
13+
float speed1, speed2, pos1, pos2;
14+
bool dir1, dir2;
15+
16+
void setup() {
17+
}
18+
19+
void loop() {
20+
speed1 = (analogRead(speedPin1)) / 4095.0; // 0-100% speed
21+
speed2 = (analogRead(speedPin2)) / 2047.0; // 0-200% speed
22+
23+
pwm.writeServo(servoPin1, pos1);
24+
delay(5); // for simulator
25+
pwm.writeServo(servoPin2, pos2);
26+
delay(5); // for simulator
27+
28+
if (pos1 < 0) pos1 = 0;
29+
if (pos2 < 0) pos2 = 0;
30+
31+
if (dir1) {
32+
if (pos1 <= 180.0) {
33+
pos1 += speed1;
34+
} else {
35+
dir1 = false;
36+
}
37+
} else {
38+
if (pos1 >= 0.5) {
39+
pos1 -= speed1;
40+
} else {
41+
dir1 = true;
42+
}
43+
}
44+
if (dir2) {
45+
if (pos2 <= 180.0) {
46+
pos2 += speed2;
47+
} else {
48+
dir2 = false;
49+
}
50+
} else {
51+
if (pos2 >= 0.5) {
52+
pos2 -= speed2;
53+
} else {
54+
dir2 = true;
55+
}
56+
}
57+
} // loop speed determines the maximum servo speed limit

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"keywords": "pwm, servo, tone, esp32, analogWrite, esp32-s2, esp32-s3, esp32-c3, ledc",
44
"description": "ESP32 PWM, SERVO, TONE and NOTE. Smart GPIO pin management and advanced control features.",
55
"license": "MIT",
6-
"version": "4.2.3",
6+
"version": "4.2.4",
77
"frameworks": "arduino",
88
"platforms": "espressif32",
99
"repository": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32 ESP32S2 AnalogWrite
2-
version=4.2.3
2+
version=4.2.4
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=ESP32 PWM, SERVO, TONE and NOTE.

src/pwmWrite.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************
2-
ESP32 PWM, SERVO and TONE Library, Version 4.2.3
2+
ESP32 PWM, SERVO and TONE Library, Version 4.2.4
33
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
44
This Library is licensed under the MIT License
55
*******************************************************************/
@@ -351,6 +351,7 @@ void Pwm::wr_ch_pair(uint8_t ch, uint32_t frequency, uint8_t bits) {
351351

352352
void Pwm::wr_duty(uint8_t ch, uint32_t duty) {
353353
if (config[ch].duty != duty) {
354+
ledcSetup(ch, config[ch].frequency, config[ch].resolution);
354355
ledcWrite(ch, duty);
355356
config[ch].duty = duty;
356357
}

0 commit comments

Comments
 (0)