// the number of the LED pin
const int ledPin = 16 ; // 16 corresponds to GPIO 16
// setting PWM properties
const int freq = 5000;
const int resolution = 4;
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
const int potPin = 34;
// variable for storing the potentiometer value
int potValue = 0;
void setup(){
Serial.begin(115200);
// configure LED PWM
ledcAttach(ledPin, freq, resolution);
// if you want to attach a specific channel, use the following instead
//ledcAttachChannel(ledPin, freq, resolution, 0);
}
void loop(){
// Reading potentiometer value
potValue = analogRead(potPin);
Serial.println(potValue);
// 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);
}
}