#define POT_PIN A0
#define LED_PIN 6
int adcValue = 0;
int pwmValue = 0;
float voltage = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
// อ่านค่า ADC 0-1023
adcValue = analogRead(POT_PIN);
// แปลงค่า ADC เป็นแรงดัน 0-5V
voltage = adcValue * (5.0 / 1023.0);
// แปลงค่า ADC เป็น PWM 0-255
pwmValue = map(adcValue, 0, 1023, 0, 255);
// ควบคุมความสว่าง LED
analogWrite(LED_PIN, pwmValue);
// แสดงข้อมูลผ่าน Serial Monitor
Serial.print("ADC = ");
Serial.print(adcValue);
Serial.print(" | Voltage = ");
Serial.print(voltage, 2);
Serial.print(" V | PWM = ");
Serial.println(pwmValue);
delay(200);
}