const int potPin = 34; // ขา ADC สำหรับเชื่อมต่อ potentiometer
const int ledPin = 2; // ขาสำหรับเชื่อมต่อ LED
// กำหนดค่าพารามิเตอร์ PWM
const int freq = 5000; // ความถี่ PWM (Hz)
const int ledChannel = 0; // ช่อง PWM
const int resolution = 8; // ความละเอียด 8-bit (0-255)
void setup() {
Serial.begin(115200);
// ตั้งค่า PWM สำหรับ ESP32
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(ledPin, ledChannel);
}
void loop() {
int potValue = analogRead(potPin); // อ่านค่า potentiometer (0-4095)
int dutyCycle = map(potValue, 0, 4095, 0, 255); // แปลงค่าให้เป็นช่วง PWM (0-255)
ledcWrite(ledChannel, dutyCycle); // ปรับค่า PWM ที่ช่องที่เลือก
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" -> PWM Duty Cycle: ");
Serial.println(dutyCycle);
delay(10); // หน่วงเวลาเล็กน้อย
}