Skip to content

Instantly share code, notes, and snippets.

@bkrajendra
Last active May 20, 2016 11:20
Show Gist options
  • Save bkrajendra/eb5ad3eebe38ae9015dd to your computer and use it in GitHub Desktop.
Save bkrajendra/eb5ad3eebe38ae9015dd to your computer and use it in GitHub Desktop.
ACS712 Measuring
float current_amplitude;
float effective_reading;
float effective_voltage = 230; // Set voltage to 230V (Europe) or 110V (US)
float effective_power;
float zero_current_reading;
//in setup or initially
//Calibrating ACS by reading no current value
zero_current_reading = measureACSvalue(A0);
//in Loop
// Measure Effective Power
float current_reading = measureACSvalue(A0);
// Convert to current
current_amplitude = (float)(current_reading-zero_current_reading)/1024*5/185*1000000;
effective_reading = current_amplitude/1.414;
effective_power = abs(effective_reading*effective_voltage/1000);
ePpower = (int)effective_power;
// Measure the reading from the ACS712
float measureACSvalue(int pin)
{
int sensorReading;
float avgSensor = 0;
int nb_measurements = 500;
for (int i = 0; i < nb_measurements; i++) {
sensorReading = analogRead(pin);
avgSensor = avgSensor + float(sensorReading);
}
avgSensor = avgSensor/float(nb_measurements);
return avgSensor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment