// Program: LED_PWM_Example_1.ino
//
// ESP32 PWM with Arduino IDE (Analog Output)
// https://randomnerdtutorials.com/esp32-pwm-arduino-ide
//
// LED Control (LEDC) — Arduino-ESP32 2.0.6 documentation
// https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/ledc.html
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16 (RX2)
const int ledPin2 = 17; // 17 corresponds to GPIO17 (TX2)
const int ledPin3 = 5; // 5 corresponds to GPIO5
const int ledPin4 = LED_BUILTIN; // LED_BUILTIN corresponds to GPIO2
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
// const int resolution = 8;
const int resolution = 5;
const int maxValue = pow(2, resolution) - 1;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
ledcAttachPin(ledPin2, ledChannel);
ledcAttachPin(ledPin3, ledChannel);
ledcAttachPin(ledPin4, ledChannel);
}
void loop(){
// increase the LED brightness
// for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
for(int dutyCycle = 0; dutyCycle <= maxValue; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
delay(500);
// decrease the LED brightness
// for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
for(int dutyCycle = maxValue; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
delay(500);
}