// Demo RGB dan LED Merah, Hijau, Biru dengan ESP32 dan tampilan nilai warna di Serial Monitor
const int pinR = 23; // Kaki RGB LED untuk warna Merah
const int pinG = 22; // Kaki RGB LED untuk warna Hijau
const int pinB = 21; // Kaki RGB LED untuk warna Biru
const int ledR = 19; // LED Merah
const int ledG = 18; // LED Hijau
const int ledB = 5; // LED Biru
const int potR = 34; // Potensiometer untuk warna Merah
const int potG = 35; // Potensiometer untuk warna Hijau
const int potB = 32; // Potensiometer untuk warna Biru
void setup() {
pinMode(pinR, OUTPUT); // Atur kaki Merah RGB LED sebagai keluaran
pinMode(pinG, OUTPUT); // Atur kaki Hijau RGB LED sebagai keluaran
pinMode(pinB, OUTPUT); // Atur kaki Biru RGB LED sebagai keluaran
pinMode(ledR, OUTPUT); // Atur kaki LED Merah sebagai keluaran
pinMode(ledG, OUTPUT); // Atur kaki LED Hijau sebagai keluaran
pinMode(ledB, OUTPUT); // Atur kaki LED Biru sebagai keluaran
pinMode(potR, INPUT); // Atur potensiometer Merah sebagai masukan
pinMode(potG, INPUT); // Atur potensiometer Hijau sebagai masukan
pinMode(potB, INPUT); // Atur potensiometer Biru sebagai masukan
// Memulai komunikasi serial
Serial.begin(115200);
}
int readPot(int pin) {
return map(analogRead(pin), 0, 4095, 0, 255); // Rentang ADC ESP32 adalah 0-4095, dipetakan ke 0-255
}
void loop() {
// Membaca nilai potensiometer
int redValue = readPot(potR);
int greenValue = readPot(potG);
int blueValue = readPot(potB);
// Mengatur warna pada RGB LED
analogWrite(pinR, redValue);
analogWrite(pinG, greenValue);
analogWrite(pinB, blueValue);
// Mengatur LED tambahan
analogWrite(ledR, redValue);
analogWrite(ledG, greenValue);
analogWrite(ledB, blueValue);
// Menampilkan nilai RGB di Serial Monitor
Serial.print("Merah: ");
Serial.print(redValue);
Serial.print(", Hijau: ");
Serial.print(greenValue);
Serial.print(", Biru: ");
Serial.println(blueValue);
delay(100); // Jeda untuk stabilitas
}