#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if needed
// Define the potentiometer pin
const int potPin = A0;
// Define the LED pin
const int ledPin = 2;
// Define the resistor value in ohms
const float resistorValue = 100.0;
void setup() {
Serial.begin(9600);
// Initialize the I2C LCD
lcd.init();
lcd.backlight();
// Initialize the LED pin
pinMode(ledPin, OUTPUT);
// Clear LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Power Calculation");
}
void loop() {
// Read the voltage from the potentiometer
int sensorValue = analogRead(potPin);
float voltage = sensorValue * (5.0 / 1023.0);
// Calculate current (Ohm's law: I = V / R)
float current = voltage / resistorValue;
// Calculate power (P = V * I)
float power = voltage * current;
// Display values on LCD
lcd.setCursor(0, 1);
lcd.print("V: " + String(voltage, 2) + "V ");
lcd.setCursor(8, 1);
lcd.print("I: " + String(current, 4) + "A ");
lcd.setCursor(0, 0);
lcd.print("P: " + String(power, 4) + "W ");
// Turn on/off LED based on voltage
if (voltage > 4.0) {
digitalWrite(ledPin, LOW); // Turn on the LED
} else {
digitalWrite(ledPin, HIGH); // Turn off the LED
}
// Print values to Serial Monitor
Serial.print("V: ");
Serial.print(voltage, 2);
Serial.print("V, I: ");
Serial.print(current, 4);
Serial.print("A, P: ");
Serial.print(power, 4);
Serial.println("W");
// Delay for stability
delay(1000);
}