Hi there
First I will discuss the HW.
Im using an arduino uno with a 30A ACS712 sensor and a ZMPT101B voltage module to "catch" each value from a "X" amount of time.
With that, I´m using EXCEL graph funcions to represent the sine wave for voltage and current. And then I can find out the phase shift of both (sorry for my english).
I have this skecth that I did on the fly and retrofitted from a watt metter that I´m also coding using both sensors, so it´s ugly but in its primal way does the job
Here is the code
double volt_neg = 1024;
double volt_pos = 0;
double volt_rms;
double amp_pos = 0;
double amp_neg = 1024;
double amp_rms;
int var = 0;
void setup() {
Serial.begin(115200);
}
void loop()
{
/*Voltaje();
Corriente();*/
//unsigned int start = 0;
while (var < 20)
{
Voltaje();
Corriente();
var++;
delay(1);
}
/*var = 0;
Serial.println("Voltaje MAX");
Serial.println(volt_pos);
Serial.println("Voltaje MIN");
Serial.println(volt_neg);
Serial.println("Voltaje Eficaz");
Serial.print(volt_rms);
Serial.println("V");*/
}
void Voltaje()
{
int volt = analogRead(A0);
volt = map(volt, 285, 740, -335, 335);
Serial.print(volt/1.414);Serial.println("V");
/*if ( volt > volt_pos) {
volt_pos = volt;
} else if (volt < volt_neg) {
volt_neg = volt;
}
volt_rms = ((volt_pos + (-volt_neg)) / 2) / sqrt(2);*/
}
void Corriente()
{
int amp = analogRead(A5); //lectura del sensor
amp = map(amp, 106, 916, -30, 30);
Serial.print(amp/1.414);Serial.println("A");
/*if ( amp > amp_pos) {
amp_pos = amp;
} else if (amp < amp_neg) {
amp_neg = amp;
}
amp_rms = ((amp_pos + (-amp_neg)) / 2) / sqrt(2); //Ecuación para obtener la corriente*/
}
I have noticed that serial print have some sort of "delay" because I wanted to take 20 samples, each at 1ms apart (corresponding to 50Hz that is the Frequency of my country)
However, I have intervals of 1ms, 2ms, 4ms, 7 ms, and I think is the serial print that is doing that
I thought of store the values in an array, and then, after the sampling is done and there is no more time sensitive demand, print the 20 values of AMP, VOLT and MILLIS()
However, im not very good at array manipulation.
So, if anyone is interested, care to debug the code?
Also maybe someone can find this proyect interesting.
Here is a picture of my sinewave graph. Blue is voltage and orange is current (you can notice that the sketch skipped a reading).
Regards