Please help fix my code. My code is for a CT sensor connected to an arduino. We
ID: 3802602 • Letter: P
Question
Please help fix my code. My code is for a CT sensor connected to an arduino. We made the breadboard connections and we have our CT receiving input we checked. Our CT delivers voltage instead of current in the analog input so we convert the voltage to an RMS current using the max voltage obtained. The CT is working on AC while our Arduino works on DC which means that we are having fluctuating voltages so we have to find the max voltage which we think we have it right in the code. The problem is that the CT is receiving a 0 value for the data and its displaying 0 for all the data being displayed on the LED. I think its a logical error in the code but if someone can fix that'd be great.
#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("RIP HARAMBE");
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
int 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;
}
int 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");
}
Explanation / Answer
If you ICT is reading 0 value as current, then voltage and other values will be calculates as 0 as this is prety obvious.
If other strings that you are passing to your lcd.print api are also printing as 0 then probably some issue with your print api.
Moreover, i would suggest to use double datatype if sensorValue could be in decimals as 0.35 will be truncated to 0 in your case.
To check you print api, initialize kilos with 10 and try to print the value of kilos as it is independent of data read from the ICT.
Please ask if more information is needed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.