// Pin definitions
const int POT_PIN = 34; // Analog input pin (ADC1)
const int LED_PIN = 18; // PWM output pin
// PWM properties
const int PWM_FREQ = 5000; // 5 kHz frequency
const int PWM_RESOLUTION = 12; // 12-bit resolution (0 - 4095)
void setup() {
Serial.begin(115200);
// Set ADC resolution to 12 bits (0 - 4095)
analogReadResolution(12);
// Attach the LED pin to the LEDC PWM peripheral
ledcAttach(LED_PIN, PWM_FREQ, PWM_RESOLUTION);
}
void loop() {
// Read the potentiometer value (0 to 4095)
int potValue = analogRead(POT_PIN);
// Write the value directly to the LED as a PWM duty cycle
//ledcWrite(LED_PIN, potValue);
float ratio = potValue / 4095.0;
//int correctedBrightness = ratio * ratio * 4095;
int correctedBrightness =(pow(ratio, 2.2))*4095;
ledcWrite(LED_PIN, correctedBrightness);
// Print values to Serial Monitor for debugging
Serial.print("Potentiometer Value: ");
Serial.println(ratio);
delay(10); // Small delay for stability
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4