#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const int POT_PIN = A0; // Pin A0 for potentiometer
const float VOLTAGE_DIVIDER_RATIO = 0.5; // Resistance ratio for voltage divider circuit
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 the LCD display
lcd.init();
lcd.backlight();
}
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) * VOLTAGE_DIVIDER_RATIO;
// 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("Digital: ");
lcd.print(analog_value);
Serial.print("Voltage: ");
Serial.print(analog_string);
Serial.print(" V, Digital: ");
Serial.println(analog_value);
delay(100); // Delay for 100 ms before taking the next reading
}