// PLEASE NOTE: WORK IN PROGRESS, INCOMPLETE CODE!
/*
This is as simple as I can make a program for reading voltage and current,
and calculating power and energy from it.
A unit for electrical energy is a multiple of power and time in Watt-hours (Wh)
or kilo-Watt-hours (kWh), which is (kilo-) Watts over 3600 seconds.
By sampling voltage and current every 1 second (1000ms),
all I have to do is divide the power in Watts by 3600 to get Wh.
A margin of error should be negligent if no other delays are used in code.
*/
#include <LiquidCrystal.h> // include standard LCD library
LiquidCrystal lcd(5,18,19,21,22,23); // assign pins for LCD as RS, E, D4, D5, D6, D7
// Potentiometers are connected to GPIO 34 and 35
// The left (potPin1) simulates voltage, the right (potPin2) simulates current
const int potPin1 = 34;
const int potPin2 = 35;
// Declare integers for potentiometers raw ADC values
int pot1Value, pot2Value = 0;
// Declare floats for potentiometer voltages
float pot1Voltage = 0.00;
float pot2Current = 0.00;
float Power = 0.00;
float Energy = 0.00;
float IncrementalPower = 0.00;
void setup() {
Serial.begin(115200);
lcd.begin(16,2);
// delay(100);
// pinMode(potPin1,INPUT_PULLUP);
}
void loop() {
// Measures the value of the potentiometer
pot1Value = analogRead(potPin1);
pot1Voltage = 3.3 * analogRead(potPin1)/4095;
pot2Value = analogRead(potPin2);
pot2Current = 3.3 * pot2Value/4095;
Power = pot1Voltage * pot2Current;
IncrementalPower += Power;
Energy = IncrementalPower / 3600;
Serial.println((String)pot1Value + ", " + (String)pot2Value);
Serial.println((String)pot1Voltage + "V, " + (String)pot2Current + "A");
Serial.println(String(pot1Voltage,3) + "V, " + String(pot2Current,3) + "A");
Serial.println((String)Power + "W");
Serial.println(IncrementalPower, 3);
Serial.println((String)Energy + "Wh");
char buff[18]; // make enough space
snprintf(buff,(sizeof(buff)-1),"%6.3fV,%6.3fA",pot1Voltage,pot2Current);
Serial.println(buff);
lcd.clear();
lcd.setCursor(0,0);
//lcd.println((String)pot1Value + " " + (String)pot2Value);
//lcd.setCursor(0,1);
lcd.println((String)pot1Voltage + "V " + (String)pot2Current + "A");
lcd.setCursor(0,1);
lcd.println((String)Power + "W");
delay(1000);
}
/*
void setup() {
Serial.begin(115200);
analogSetAttenuation(ADC_0db);
Serial.println("Hello, ESP32!");
lcd.begin(16,2); // specify 16-column 2-row LCD
//lcd.backlight(); // no function for most LCDs
lcd.setCursor(1, 0);
lcd.print("Hello, Wokwi!");
delay(1000);
}
void loop()
{
lcd.clear();
ADCvalue = analogRead(ADCpin);
Serial.print("ADC Value: ");
Serial.println(ADCvalue);
ADCvoltage = (ADCvalue * 3.3) / 4095;
actual_voltage = ADCvoltage * 6;
lcd.setCursor(0,0); // set cursor to first column, first row
lcd.print("ADC Value:"); // print message
lcd.setCursor(11,0); // set cursor to 12th column, first row
lcd.print(ADCvalue);
lcd.setCursor(0,1);
lcd.print("Voltage: ");
lcd.print(actual_voltage);
lcd.print("V");
delay(500);
lcd.clear();
delay(10); // this speeds up the simulation
}
/*
int analogvalue = analogRead(A0);
temp = (analogvalue * 5.0) / 1024.0; // FORMULA USED TO CONVERT THE VOLTAGE
input_volt = temp / (r2/(r1+r2));
if (input_volt < 0.1)
{
input_volt=0.0;
}
Serial.print("v= "); // prints the voltage value in the serial monitor
Serial.println(input_volt);
lcd.setCursor(0, 1);
lcd.print("Voltage: "); // prints the voltage value in the LCD display
lcd.print(input_volt);
delay(300);
*/