/*
* ESP32 PWM menggunakan LEDC API - Metode Advanced
* Compatible dengan ESP32 Core 3.X
*
* Berdasarkan: Random Nerd Tutorials ESP32 PWM Guide
*/
// ===== KONFIGURASI PIN =====
const int ledPin = 16; // GPIO16 untuk LED
// ===== KONFIGURASI PWM =====
const int freq = 5000; // Frequency 5000 Hz
const int resolution = 8; // Resolution 8-bit (0-255)
// ===== SETUP =====
void setup() {
Serial.begin(115200);
Serial.println("ESP32 PWM dengan LEDC API - Started");
// Configure LED PWM dengan automatic channel selection
ledcAttach(ledPin, freq, resolution);
// Alternatif: jika ingin pilih channel manual
// ledcAttachChannel(ledPin, freq, resolution, 0);
}
// ===== LOOP UTAMA =====
void loop() {
// Increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
// Changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
// Decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
// Changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
}