Help me finish this code for an Arduino energy meter using a CT and breadboard.
ID: 3801838 • Letter: H
Question
Help me finish this code for an Arduino energy meter using a CT and breadboard. I am having a problem displaying the appropriate voltage and current because the thing I am measure is AC and the Arduino itself is DC. I am having trouble with the logic of the voltage being received with the Arduino. Feel free to edit I just need to display the voltage of the thing being measured aswell as the current.
#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;
double newVoltage;
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()
{
newVoltage = 0;
// read the input on analog pin 1:
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 > peakVoltage)
{
peakVoltage = voltage;
}
endMillis = millis();
int newVoltage=peakVoltage/sqrt(2.0);
unsigned long time = endMillis - startMillis;
double Irms = 50 * newVoltage;
int RMSPower = Irms*230.0 ;
startMillis = millis();
lcd.clear();
lcd.setCursor(0,0); // Displays all current data
lcd.print(Irms);
lcd.print("A");
lcd.setCursor(10,0);
lcd.print(RMSPower);
lcd.print("W");
lcd.setCursor(0,1);
lcd.print(voltage);
lcd.print("V");
lcd.setCursor(10,1);
lcd.print(newVoltage);
lcd.print("V");
delay (500);
}
Explanation / Answer
Please find the code below. This code uses the millis() function which calculates
the exact cycle time for each cycle in order to improve the accuracy.
#include <LiquidCrystal.h>
unsigned long startMillis;
unsigned long endMillis;
//Assign CT input to pin 1
int currentPin = 1;
double kilos = 0;
int peakPower = 0;
//Assign LCD screen pins, as per LCD shield requirements
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
// columns, rows. use 16,2 for a 16x2 LCD, etc
lcd.begin(16,2);
lcd.clear();
// set cursor to column 0, row 0 (the first row)
lcd.setCursor(0,0);
lcd.print("Arduino");
// set cursor to column 0, row 1 (the second row)
lcd.setCursor(0,1);
lcd.print("Energy Meter");
startMillis = millis();
}
void loop()
{
int current = 0;
int minCurrent = 1000;
int maxCurrent = 0;
//Monitors and logs the current 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
current = analogRead(currentPin);
if(current >= maxCurrent)
maxCurrent = current;
else if(current <= minCurrent)
minCurrent = current;
}
if (maxCurrent <= 517)
{
maxCurrent = 516;
}
//Calculates RMS current based on maximum value
double RMSCurrent = ((maxCurrent - 516)*0.707)/11.8337;
//Calculates RMS Power Assuming Voltage 220VAC, change to 110VAC accordingly
int RMSPower = 220*RMSCurrent;
if (RMSPower > peakPower)
{
peakPower = RMSPower;
}
endMillis = millis();
unsigned long time = endMillis - startMillis;
//Calculate kilowatt hours used
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(peakPower);
lcd.print("W");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.