52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
//Measuring Current Using ACS712
|
|
|
|
const int analogIn = A0; //Connect current sensor with A0 of Arduino
|
|
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
|
|
int RawValue= 0;
|
|
int ACSoffset = 2500;
|
|
double Voltage = 0; //voltage measuring
|
|
double Amps = 0;// Current measuring
|
|
|
|
void setup() {
|
|
//baud rate
|
|
Serial.begin(9600);//baud rate at which arduino communicates with Laptop/PC
|
|
delay(2000);//delay for 2 sec
|
|
}
|
|
|
|
float valeurACS712( int pin )
|
|
{
|
|
int valeur;
|
|
float moyenne = 0;
|
|
int nbr_lectures = 50;
|
|
|
|
for( int i = 0; i < nbr_lectures; i++ )
|
|
{
|
|
valeur = analogRead( pin );
|
|
moyenne = moyenne + float(valeur);
|
|
delay(10);
|
|
}
|
|
moyenne = moyenne / float(nbr_lectures);
|
|
return moyenne -235;
|
|
}
|
|
|
|
void loop() //method to run the source code repeatedly
|
|
{
|
|
|
|
RawValue = valeurACS712(analogIn);//reading the value from the analog pin
|
|
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
|
|
Amps = ((Voltage - ACSoffset) / mVperAmp);
|
|
|
|
//Prints on the serial port
|
|
Serial.print("Raw Value = " ); // prints on the serial monitor
|
|
Serial.print(RawValue); //prints the results on the serial monitor
|
|
|
|
|
|
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.println(Amps,3);// the '3' after voltage allows you to display 3 digits after decimal point
|
|
|
|
delay(2500);
|
|
}
|