Energy Meter Code reading 0 for the values

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");
}

Please post a circuit diagram (hand drawn, not Fritzing).

Here you go!

Thanks. You are adding a DC bias of Vcc/2 (as required), so the following lines are all wrong:

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;

Other than the poor choice of "int" for RMSPower, I don't see why you should be getting 0 as a result.

jremington:
Thanks. You are adding a DC bias of Vcc/2 (as required), so the following lines are all wrong:

Would this code work if the circuit was made like this

What would I have to change if i kept the circuit the same as the first schematic to make the code work. Since the reason we are multiplying the RMSVoltage by 50 is because its the ratio of the current going to through the CT to voltage that we calculated.

Thanks!

The bottom circuit won't work -- it could even destroy the ADC by applying negative voltages.

Surely you can do a tiny bit of math.

If the input is a pure sine wave and you have sampled frequently enough, then it is approximately correct that (rms voltage) = (max - min peak voltage)/(2.0*sqrt(2))

There are better ways to go about this, of course.

Ya i have seen this example before but our CT is different from the one they are using as ours already has a resitor built into it so its delivering a voltage instead of a current. I don't really understand too much how the emonlib would work in my scenario as I dont have a emon arduino shield nor do I have the CT sensor that inputs current. Is there a way that I use emon to obtain voltage input and then use a fixed current equation afterwards, all the examples ive seen need both a current and voltage input.

I recently built the circuit from the energy monitor website. Mine is nearly identical to the one you show in Reply 2. It works perfectly with the SCT 013 transformer.

TKall:
I recently built the circuit from the energy monitor website. Mine is nearly identical to the one you show in Reply 2. It works perfectly with the SCT 013 transformer.

Hey did you use an emon shield or did you directly connect it to a breadboard? Do you see any problems on my code other than previously stated? Thanks!

I connected directly to an UNO and used the emon library. Are you measuring something that is drawing sufficient current to move the needle? And only clamping one side with the transformer?

I just bought my first oscilloscope and connected it to the output side of the circuit. I thought the voltage would be flatter, but it appears to be a 60 hz sine wave. I don't think the voltages go negative however.