#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD 16x2
const int sensorPin = A0; // Pin sensor tegangan
const int redLedPin = 8; // Pin LED merah
const int yellowLedPin = 9; // Pin LED kuning
const int greenLedPin = 10; // Pin LED hijau
void setup() {
lcd.begin(16, 2); // Inisialisasi LCD
lcd.backlight(); // Aktifkan backlight LCD
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
}
void loop() {
// Baca tegangan dari sensor
int sensorValue = analogRead(sensorPin);
// Konversi nilai sensor menjadi tegangan (0-5V)
float voltage = sensorValue * (5.0 / 1023.0);
// Tampilkan tegangan di LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tegangan: ");
lcd.print(voltage);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Sensor: ");
lcd.print(sensorValue);
lcd.print(" ADC");
// Tentukan warna LED sesuai dengan rentang tegangan
if (voltage >= 0.0 && voltage <= 0.6) {
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
} else if (voltage > 0.6 && voltage <= 1.2) {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
} else if (voltage > 1.2 ) {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
}
delay(1000); // Tunda selama 1 detik sebelum membaca ulang tegangan
}