77 lines
1.9 KiB
C++
77 lines
1.9 KiB
C++
/*
|
|
Measuring Current Using ACS712
|
|
*/
|
|
#include "math.h"
|
|
const int analogIn = A0;
|
|
int mVperAmp = 185; // 185 pour 5A, use 100 for 20A Module and 66 for 30A Module
|
|
int RawValue= 0;
|
|
int ACSoffset = 2500;
|
|
double mp_offset = 0.040;
|
|
double Voltage = 0;
|
|
double Amps = 0;
|
|
|
|
void setup(){
|
|
Serial.begin(9600);
|
|
pinMode(A0, INPUT);
|
|
}
|
|
|
|
void loop(){
|
|
|
|
int i = 0;
|
|
RawValue = 0;
|
|
// Somme du courant alternatif pendant 20 ms ==> 50hz
|
|
// Détermination du max et max pour hauteur de crete
|
|
int vmin = 1024;
|
|
int vmax = 0;
|
|
for (i = 0; i < 20; i++) {
|
|
int value = analogRead(analogIn);
|
|
if (value >= 0) {
|
|
RawValue += value;
|
|
vmax = max(value,vmax);
|
|
vmin = min(value,vmin);
|
|
} else {
|
|
i--;
|
|
}
|
|
delay(1);
|
|
|
|
}
|
|
// Serial.print("Raw Value = " );
|
|
// Serial.print(RawValue);
|
|
Serial.print("min = " );
|
|
Serial.print(vmin);
|
|
Serial.print(" max = " );
|
|
Serial.print(vmax);
|
|
|
|
// Serial.print(" i =" );
|
|
// Serial.print(i);
|
|
// RawValue = RawValue / i;
|
|
|
|
|
|
// Tension continue
|
|
// Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
|
|
// Amps = ((Voltage - ACSoffset) / mVperAmp);
|
|
|
|
|
|
// La valeur maxi * racine carrée de 2 pour obtenir la tension "réelle"
|
|
// La tension efficace pour l'effet Hall étant réduite d'un facteur 0,707
|
|
Voltage = ((vmax - vmin) / 430.0) * 5000;
|
|
|
|
Amps = max(5.5 * (vmax - vmin) / 473.0 -0.0580, 0.0); // <= pour le bruit
|
|
|
|
|
|
// Serial.print(" Raw Value = " ); // shows pre-scaled value
|
|
// Serial.print(RawValue);
|
|
Serial.print("\t mV = "); // shows the voltage measured
|
|
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
|
|
Serial.print("\t Amps = "); // shows the voltage measured
|
|
Serial.print(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
|
|
Serial.print("\t Watt = "); // shows the voltage measured
|
|
Serial.print(Amps * 220,3);
|
|
Serial.print("\t WattH = "); // shows the voltage measured
|
|
Serial.println(Amps * 220 / 1200,3);
|
|
|
|
|
|
delay(2500);
|
|
|
|
}
|