#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const int POT_PIN = A0; // Pin A0 for potentiometer
const float REFERENCE_VOLTAGE = 5.0; // ADC reference voltage
// Initialize the I2C LCD interface (address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Initialize LED pins
pinMode(3, OUTPUT); //Green LED
pinMode(4, OUTPUT); //Yellow LED
pinMode(5, OUTPUT); //GRed LED
// Initialize the LCD display
lcd.init();
lcd.backlight();
//Set all LED's low
digitalWrite(3,LOW); //Green LED
digitalWrite(4,LOW); //Yellow LED
digitalWrite(5,LOW); //Red LED
}
void loop() {
// Read the analog voltage from the potentiometer
int analog_value = analogRead(POT_PIN);
// Convert the analog value to a voltage in volts
float voltage = analog_value * (REFERENCE_VOLTAGE / 1023.0);
// Convert the analog value to a string for printing to the LCD display
char analog_string[7];
dtostrf(voltage, 5, 2, analog_string);
// Print the voltage in volts and the digital value in decimal format
// to the LCD display and the serial monitor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(analog_string);
lcd.print(" V");
lcd.setCursor(0, 1);
lcd.print("Level: ");
if(voltage >= (0.0) && voltage <= (2.0)){
digitalWrite(3,HIGH); //Green LED
digitalWrite(4,LOW); //Yellow LED
digitalWrite(5,LOW); //Red LED
lcd.print("LOW ");
}
else if(voltage >= (2.01) && voltage <= (3.5)){
digitalWrite(3,LOW); //Green LED
digitalWrite(4,HIGH); //Yellow LED
digitalWrite(5,LOW); //Red LED
lcd.print("MEDIUM ");
}
else {
digitalWrite(3,LOW); //Green LED
digitalWrite(4,LOW); //Yellow LED
digitalWrite(5,HIGH); //Red LED
lcd.print("HIGH ");
}
Serial.print("Voltage: ");
Serial.print(analog_string);
Serial.print(" V, Digital: ");
Serial.print(analog_value);
Serial.print(", Level: ");
if(voltage >= (0.0) && voltage <= (2.0))
Serial.println("LOW");
else if(voltage >= (2.01) && voltage <= (3.5))
Serial.println("MEDIUM");
else
Serial.println("HIGH");
delay(100); // Delay for 100 ms before taking the next reading
}