const int potPin = 34; // Analog input pin for potentiometer on ESP32 const int ledPin = 5; // PWM enabled pin for LED on ESP32 void configurePWM() { const int freq = 5000; // PWM frequency const int ledChannel = 0; // PWM channel const int resolution = 8; // PWM resolution (8 bits) // Configure LED PWM functionalities ledcSetup(ledChannel, freq, resolution); // Attach the channel to the GPIO to be controlled ledcAttachPin(ledPin, ledChannel); } void setup() { pinMode(potPin, INPUT); // Set the potentiometer pin as input configurePWM(); // Configure PWM for the LED } void loop() { int sensorValue = analogRead(potPin); // Read potentiometer value (0-4095 on ESP32) int brightness = map(sensorValue, 0, 4095, 0, 255); // Map potentiometer value to LED brightness (0-255) ledcWrite(0, brightness); // Set LED brightness using PWM on channel 0 }
const int potPin = 34; // Analog input pin for potentiometer on ESP32
const int ledPin = 5; // PWM enabled pin for LED on ESP32
void configurePWM() {
const int freq = 5000; // PWM frequency
const int ledChannel = 0; // PWM channel
const int resolution = 8; // PWM resolution (8 bits)
// Configure LED PWM functionalities
ledcSetup(ledChannel, freq, resolution);
// Attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
}
void setup() {
pinMode(potPin, INPUT); // Set the potentiometer pin as input
configurePWM(); // Configure PWM for the LED
}
void loop() {
int sensorValue = analogRead(potPin); // Read potentiometer value (0-4095 on ESP32)
int brightness = map(sensorValue, 0, 4095, 0, 255); // Map potentiometer value to LED brightness (0-255)
ledcWrite(0, brightness); // Set LED brightness using PWM on channel 0
}