summaryrefslogtreecommitdiff
path: root/libraries/ESP32Servo/examples/PWMExample
diff options
context:
space:
mode:
authorschererleander <leander@schererleander.de>2026-01-20 08:34:54 +0100
committerschererleander <leander@schererleander.de>2026-01-20 08:34:54 +0100
commit85ea4e995a75abe061f6fc375ea0481084dddd43 (patch)
tree7eb5d57653ecd8f041aeac4e68d7d554c1168681 /libraries/ESP32Servo/examples/PWMExample
initial commitHEADmain
Diffstat (limited to 'libraries/ESP32Servo/examples/PWMExample')
-rw-r--r--libraries/ESP32Servo/examples/PWMExample/PWMExample.ino36
1 files changed, 36 insertions, 0 deletions
diff --git a/libraries/ESP32Servo/examples/PWMExample/PWMExample.ino b/libraries/ESP32Servo/examples/PWMExample/PWMExample.ino
new file mode 100644
index 0000000..536fc4d
--- /dev/null
+++ b/libraries/ESP32Servo/examples/PWMExample/PWMExample.ino
@@ -0,0 +1,36 @@
+#include <ESP32Servo.h>
+int APin = 13;
+ESP32PWM pwm;
+int freq = 1000;
+void setup() {
+ // Allow allocation of all timers
+ ESP32PWM::allocateTimer(0);
+ ESP32PWM::allocateTimer(1);
+ ESP32PWM::allocateTimer(2);
+ ESP32PWM::allocateTimer(3);
+ Serial.begin(115200);
+ pwm.attachPin(APin, freq, 10); // 1KHz 10 bits
+
+}
+void loop() {
+
+ // fade the LED on thisPin from off to brightest:
+ for (float brightness = 0; brightness <= 0.5; brightness += 0.001) {
+ // Write a unit vector value from 0.0 to 1.0
+ pwm.writeScaled(brightness);
+ delay(2);
+ }
+ //delay(1000);
+ // fade the LED on thisPin from brithstest to off:
+ for (float brightness = 0.5; brightness >= 0; brightness -= 0.001) {
+ freq += 10;
+ // Adjust the frequency on the fly with a specific brightness
+ // Frequency is in herts and duty cycle is a unit vector 0.0 to 1.0
+ pwm.adjustFrequency(freq, brightness); // update the time base of the PWM
+ delay(2);
+ }
+ // pause between LEDs:
+ delay(1000);
+ freq = 1000;
+ pwm.adjustFrequency(freq, 0.0); // reset the time base
+}