// LED is connected to GPIO 2
const int ledPin = 2; //pin data output analog dari LED
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
const int potentiometerPin = 34; //pin data input analog dari potensiometer
// variable for storing the potentiometer value
int potentiometerValue = 0; //variabel untuk memunculkan nilai analog dari potentiometer
float potentiometer_Vout = 3.3; //variabel untuk memunculkan Vout
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //berfungsi agar mikrokontoroler dapat berkomunikasi dengan komputer
//115200 adalah nilai yang dipilih untuk komunikasi
pinMode(ledPin, OUTPUT); //menempatkan ledPin atau pin2 sebagai OUTPUT
pinMode(potentiometerPin, INPUT); //menempatkan potentiometerPin atau pin34 sebagai INPUT
Serial.println("Hello, ESP32!"); //menampilkan Hello, ESP32!
}
void loop() {
potentiometerValue = analogRead(potentiometerPin); //Reading potentiometer value
// Description: Map data from one range to another.
// Function prototype: map(value, fromLow, fromHigh, toLow, toHigh)
int dutyCycle = map(potentiometerValue, 0, 4095, 0, 254);
Serial.print("Duty Cycle: "); //menampilkan "Duty Cycle :"
Serial.println(dutyCycle); //menampilkan nilai dutyCycle
// changing the LED brightness with PWM
analogWrite(ledPin, dutyCycle); /*mengatur kecerahan pada LED menggunakan knob
yang ada di potensiometer */
delay(3); //jedaa waktu yang keluar berdasarkan potensio
}