#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Analog input pin for voltage measurement
const int voltagePin = A0;
// Reference voltage (usually 5V for Arduino, but measure for accuracy)
const float referenceVoltage = 5.0;
// Voltage divider ratio if you're using one (set to 1 for direct measurement)
const float voltageDividerRatio = 1.0;
void setup() {
// Initialize the I2C LCD
lcd.init();
// Turn on the backlight
lcd.backlight();
// Print a startup message
lcd.print("Voltage Meter");
delay(1000);
lcd.clear();
// Configure the analog reference (DEFAULT is usually 5V)
analogReference(DEFAULT);
// Optional: set the resolution to 10 bits (default on most Arduino boards)
// analogReadResolution(10);
// Initialize I2C
Wire.begin();
}
void loop() {
// Read the analog value (0-1023 for 10-bit ADC)
int sensorValue = analogRead(voltagePin);
// Convert the analog reading to voltage
float voltage = (sensorValue * referenceVoltage) / 1023.0;
// Apply voltage divider ratio if needed
voltage *= voltageDividerRatio;
// Display the voltage on the LCD
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2); // Display with 2 decimal places
lcd.print(" V");
// Clear the second line and show raw value (optional)
lcd.setCursor(0, 1);
lcd.print("Raw: ");
lcd.print(sensorValue);
lcd.print(" "); // Clear any remaining characters
// Wait a bit before the next reading
delay(500);
}