#include <Wire.h>
#include <LiquidCrystal_I2C.h> // LCD I2C library
// init. LCD (I2C address 0x27, 20 columns, 4 rows)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ESP32 pin connected to the potentiometer (current sensor simulation)
const int currentSensorPin = 36; // VP pin in Wokwi, GPIO 36
void setup() {
// init. LCD
lcd.init();
lcd.backlight();
// init. serial communication for debugging
Serial.begin(115200);
}
void loop() {
// read sensor value (analogRead goes from 0 to 4095)
int sensorValue = analogRead(currentSensorPin);
// convert to amps
float current = map(sensorValue, 0, 4095, 0, 5000) / 1000.0;
// convert amps to battery percentage
float battery = (current / 5) * 100;
// print both values in serial display
Serial.print("Current: ");
Serial.print(current);
Serial.println(" Amps");
Serial.print("Battery: ");
Serial.print(battery);
Serial.println(" %");
// display the current values on the LCD
lcd.clear(); // clear prev. LCD screen
lcd.setCursor(0, 0); // set cursor to first line
lcd.print("Power: ");
lcd.print(battery, 1); // Display battery with one decimal
lcd.print(" %");
// check low battery
if (battery < 20.0) {
lcd.setCursor(0, 1);
lcd.print("Battery Low! "); // warning
} else {
lcd.setCursor(0, 1);
lcd.print(" "); // clear second line
}
// wait 1 s before updating display
delay(1000);
}