const int LED_OUTPUT_PIN = 21;
const int POTENTIOMETER_INPUT_PIN = A6; // GPIO 14
const int MAX_ANALOG_VAL = 10000;
const int PWM_CHANNEL = 0; // ESP32 has 16 channels which can generate 16 independent waveforms
const int PWM_FREQ = 500; // Recall that Arduino Uno is ~490 Hz. Official ESP32 example uses 5,000Hz
const int PWM_RESOLUTION = 16; // We'll use same resolution as Uno (8 bits, 0-255) but ESP32 can go up to 16 bits
// The max duty cycle value based on PWM resolution (will be 255 if resolution is 8 bits)
const int MAX_DUTY_CYCLE = (int)(pow(2, PWM_RESOLUTION) - 1);
void setup() {
Serial.begin(9600);
pinMode(LED_OUTPUT_PIN, OUTPUT);
pinMode(POTENTIOMETER_INPUT_PIN, INPUT);
// Setup the PWM waveform and attach our LED output pin
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(LED_OUTPUT_PIN, PWM_CHANNEL);
}
void loop() {
int potVal = analogRead(POTENTIOMETER_INPUT_PIN);
int dutyCycle = map(potVal, 0, MAX_ANALOG_VAL, 0, MAX_DUTY_CYCLE);
ledcWrite(PWM_CHANNEL, dutyCycle);
Serial.println((String)potVal + ", " + dutyCycle);
delay(10);
}