PWMExample.ino 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <ESP32Servo.h>
  2. int APin = 13;
  3. ESP32PWM pwm;
  4. int freq = 1000;
  5. void setup() {
  6. // Allow allocation of all timers
  7. ESP32PWM::allocateTimer(0);
  8. ESP32PWM::allocateTimer(1);
  9. ESP32PWM::allocateTimer(2);
  10. ESP32PWM::allocateTimer(3);
  11. Serial.begin(115200);
  12. pwm.attachPin(APin, freq, 10); // 1KHz 8 bit
  13. }
  14. void loop() {
  15. // fade the LED on thisPin from off to brightest:
  16. for (float brightness = 0; brightness <= 0.5; brightness += 0.001) {
  17. // Write a unit vector value from 0.0 to 1.0
  18. pwm.writeScaled(brightness);
  19. delay(2);
  20. }
  21. //delay(1000);
  22. // fade the LED on thisPin from brithstest to off:
  23. for (float brightness = 0.5; brightness >= 0; brightness -= 0.001) {
  24. freq += 10;
  25. // Adjust the frequency on the fly with a specific brightness
  26. // Frequency is in herts and duty cycle is a unit vector 0.0 to 1.0
  27. pwm.adjustFrequency(freq, brightness); // update the time base of the PWM
  28. delay(2);
  29. }
  30. // pause between LEDs:
  31. delay(1000);
  32. freq = 1000;
  33. pwm.adjustFrequency(freq, 0.0); // reset the time base
  34. }