/* * Program by R. Jordan Kreindler * showing the use of the PWM capabilities * of an ESP32 Development board. */ #define ledChannel 0 // Channel 0 chosen, from 0-15 choices #define resolution 15 // Resolution is chosen as 15 bits #define frequency 5000 // 5,000 Hz frequency #define LED 15 // LED connected via 330 ohm // resistor to pin 15 // Other end of LED // to GND int delay1 = 20; // Delay in milliseconds void setup() { ledcSetup(ledChannel, frequency, resolution); // Set-up a channel ledcAttachPin(LED, ledChannel); // Attach a channel to a pin } void loop() { // In the for() loops, adding and subtracting // two hundred (200) is necessary owing to the greater // resolution provided by 15 bits, compared to the UNO for (int i = 0; i < 32768; i = i + 200) { //loop to fade up ledcWrite(ledChannel, i); delay(delay1); } for (int i = 32767; i > -1; i = i - 200) { //loop to fade down ledcWrite(ledChannel, i); delay(delay1); } }