So a quick summary, I am building a home energy meter using a CT and my arduino. I have it connected to a breadboard. The thing is my CT has a built in resistor so it doesen't input a current to the analog but instead inputs a voltage input which I convert to volts and then later use that to find the current. I am not the greatest at coding but the problem I'm facing now is that I am getting a 0 input on my led screen of all these values which I think is a problem with the input being received for the voltage but I'm not sure. The CT works 100% and with a previous code the arduino sensed this CT so it does work. The CT is using AC while the arduino is running on DC which is why im trying to get the maxVoltage so that i can calculate the RMSCurrent. But so far I am getting a 0 input for my values which i think could just be a logical error for my code.
Here is the code
#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Assign LCD screen pins, as per LCD shield requirements
unsigned long startMillis;
unsigned long endMillis;
double peakVoltage;
int peakPower = 0;
double kilos = 0;
void setup(){
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear();
lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)
lcd.print("ELEE 2790U");
lcd.setCursor(0,1); // set cursor to column 0, row 1 (the second row)
lcd.print("Energy Meter");
startMillis = millis();
}
void loop()
{
int voltage = 0;
int minVoltage = 5;
int maxVoltage = 0;
// read the input on analog pin 1:
///Monitors and logs the voltage input for 200 cycles to determine max and min current
for (int i=0 ; i<=200 ; i++)
{
//Reads current input and records maximum and minimum current
double sensorValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
double voltage = sensorValue * (5.0 / 1023.0);
if(voltage >= maxVoltage)
maxVoltage = voltage;
else if(voltage <= minVoltage)
minVoltage = voltage;
}
double RMSVoltage=maxVoltage/sqrt(2.0);
//Calculates RMS current based on maximum value
double RMSCurrent = RMSVoltage*50;
//Calculates RMS Power Assuming Voltage 230VAC
int RMSPower = 230*RMSCurrent;
if (RMSPower > peakPower)
{
peakPower = RMSPower;
}
endMillis = millis();
unsigned long time = endMillis - startMillis;
kilos = kilos + ((double)RMSPower * ((double)time/60/60/1000000));
startMillis = millis();
delay(2000);
lcd.clear();
// Displays all current data
lcd.setCursor(0,0);
lcd.print(RMSCurrent);
lcd.print("A");
lcd.setCursor(10,0);
// Displays Power data
lcd.print(RMSPower);
lcd.print("W");
lcd.setCursor(0,1);
lcd.print(kilos);
lcd.print("kWh");
lcd.setCursor(10,1);
lcd.print(RMSVoltage);
lcd.print("V");
}