 • # [Solved] Measuring AC current with ESP8266 and ACS712, calculation

Hi guys,

I want to measure current at a point far away from the Pi so i took an ESP8266 and an ACS712 current sensor to report the data over mqtt. With some ‘borrowed’ code i managed to make a working arduino sketch. However, the values i’m reading don’t make any sense. That’s probably because the ACS712 wants 5 volt and the ESP8266 can only deliver 3 volt.

Could anybody give me a hand with adjusting this calculation?

From the sketch:

Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);

• The minimum Voltage is 4,5V. With 3,3 V it will not run stable.
Use it with 5 V and use a Voltage-Divider (2 Resistors) on the Output.

• @michael-rudek Tnx, i’ll give that a try!

• @michael-rudek What type resistors should i use? And should they be parallel or serial connected?

• Search for “Voltage divider” “Spannungsteiler”. You have to calculate the Resistors. Serial on the 5V Side. Between the two resistors is the 3,3V Side

• @fregger Here you can find all the information you need

http://esp8266-projects.org/2015/06/mailbag-arrival-acs712-current-sensor-html/

"It always takes longer than you expect, even when you take into account Hofstadter's Law.", Hofstadter's Law

• @michael-rudek I connected as you suggested with 2 x 4.7 Kohm resistors because that’s what i had lying around. It gives me 2,6 volt on the output so at least it won’t blow the ESP. The readings still don’t make much sense to me.

@mwittig Thanx, i’ll have to let sink in that info I’m not too experienced with electronics. The ultimate goal with this little project is to measure the output of my solar invertor, since the Mastervolt intelliweb server goes annoyingly down about every sunny day.

• And what is the Output?

• @michael-rudek measuring a wall plug with nothing plugged in, i get:

``````Raw Value = 51	 mV = 249.023	 Amps = -22.510
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 52	 mV = 253.906	 Amps = -22.461
Raw Value = 715	 mV = 3491.211	 Amps = 9.912
Raw Value = 299	 mV = 1459.961	 Amps = -10.400
Raw Value = 68	 mV = 332.031	 Amps = -21.680
Raw Value = 72	 mV = 351.562	 Amps = -21.484
Raw Value = 63	 mV = 307.617	 Amps = -21.924
Raw Value = 68	 mV = 332.031	 Amps = -21.680
Raw Value = 31	 mV = 151.367	 Amps = -23.486
Raw Value = 45	 mV = 219.727	 Amps = -22.803
Raw Value = 52	 mV = 253.906	 Amps = -22.461
Raw Value = 69	 mV = 336.914	 Amps = -21.631
Raw Value = 506	 mV = 2470.703	 Amps = -0.293
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 4	 mV = 19.531	 Amps = -24.805
Raw Value = 51	 mV = 249.023	 Amps = -22.510
Raw Value = 57	 mV = 278.320	 Amps = -22.217
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 57	 mV = 278.320	 Amps = -22.217
Raw Value = 70	 mV = 341.797	 Amps = -21.582
Raw Value = 91	 mV = 444.336	 Amps = -20.557
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
``````

With a halogen light using 500 watt plugged in, i get

``````Raw Value = 61	 mV = 297.852	 Amps = -22.021
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 529	 mV = 2583.008	 Amps = 0.830
Raw Value = 527	 mV = 2573.242	 Amps = 0.732
Raw Value = 605	 mV = 2954.102	 Amps = 4.541
Raw Value = 293	 mV = 1430.664	 Amps = -10.693
Raw Value = 264	 mV = 1289.063	 Amps = -12.109
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 59	 mV = 288.086	 Amps = -22.119
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 518	 mV = 2529.297	 Amps = 0.293
Raw Value = 518	 mV = 2529.297	 Amps = 0.293
Raw Value = 519	 mV = 2534.180	 Amps = 0.342
Raw Value = 623	 mV = 3041.992	 Amps = 5.420
Raw Value = 449	 mV = 2192.383	 Amps = -3.076
Raw Value = 448	 mV = 2187.500	 Amps = -3.125
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 403	 mV = 1967.773	 Amps = -5.322
Raw Value = 635	 mV = 3100.586	 Amps = 6.006
Raw Value = 497	 mV = 2426.758	 Amps = -0.732
Raw Value = 491	 mV = 2397.461	 Amps = -1.025
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 0	 mV = 0.000	 Amps = -25.000
Raw Value = 55	 mV = 268.555	 Amps = -22.314
Raw Value = 54	 mV = 263.672	 Amps = -22.363
Raw Value = 81	 mV = 395.508	 Amps = -21.045
Raw Value = 586	 mV = 2861.328	 Amps = 3.613
Raw Value = 104	 mV = 507.812	 Amps = -19.922
``````

At the beginning of the arduino sketch (the ACS712 is a 20A module), we have:

``````const int analogIn = A0;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module and 185 for 5A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
``````

Later called with this:

``````  RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
``````
• Ok, i never used it for AC-Current. Maybe you need a condensator to ground at the output.
What does a multimeter measure at the Output?
You have common ground between ESP and ACS?

• What does a multimeter measure at the Output?

You mean voltage? That would be around 1,32 (please bear with me, as i said i’m not too experienced with electronics)

You have common ground between ESP and ACS?

No, should i?

• If ISP and ACS don´t have the same ground(Minus), you will not measure anything. Connect the 3,3V Minus and the 5V Minus, otherwise you have only an Antenna • @michael-rudek Lol, well that does make a difference! Above mentioned sketch still gives me weird readings. However, i found another sketch and now i see readings that start to make some sense.

With nothing plugged in, I get:

sensor_max = 385 (rises to 386 sometimes)
The amplitude of the current is(in mA) -6201.2
The effective value of the current is(in mA) -4385.6

With power usage according to power consumption meter:

385 watt (1,687 A, voltage 228)

sensor_max = 422 (rises to 423 sometimes)
The amplitude of the current is(in mA) -4394.5
The effective value of the current is(in mA) -3107.9

Now i would have to find a way to calculate the difference between the sensor_maxes, telling me the power usage is 385 watt.

The sketch I’m currently using:

``````#define CURRENT_SENSOR A0  // Define Analog input pin that sensor is attached

float amplitude_current;      // Float amplitude current
float effective_value;       // Float effective current

void setup()
{
Serial.begin(9600);
pins_init();
}
void loop()
{
int sensor_max;
sensor_max = getMaxValue();
Serial.print("sensor_max = ");
Serial.println(sensor_max);

//the VCC on the Arduino interface of the sensor is 5v

amplitude_current=(float)(sensor_max-512)/1024*5/100*1000000; // for 5A mode(185),you need to modify this with 20 A (100) and 30A (66) mode;
effective_value=amplitude_current/1.414;

//for minimum current=1/1024*5/185*1000000/1.414=18.7(mA)
//Only sinusoidal alternating current

Serial.println("The amplitude of the current is(in mA)");
Serial.println(amplitude_current,1);

//Only one number after the decimal point

Serial.println("The effective value of the current is(in mA)");
Serial.println(effective_value,1);
}
void pins_init()
{
pinMode(CURRENT_SENSOR, INPUT);
}
/*Function: Sample for 1000ms and get the maximum value from the S pin*/

int getMaxValue()
{
int sensorValue;    //value read from the sensor
int sensorMax = 0;
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1000ms
{
if (sensorValue > sensorMax)
{
/*record the maximum sensor value*/

sensorMax = sensorValue;
}
}
return sensorMax;
}
``````
• Because of the “wrong” Voltage Divider you will have wrong readings.
At 0 Ampere (no Load) you should read 512, or measure 2,5V at the ACS and 1,65 V at the Analog-Pin of the ESP.
Instead of the 2 Resistors you can use a Spindle-Potentiometer, makes tuning easier…

• @michael-rudek Thanks for clearing that up! I’ll order a few Type 3296W 500kOhm 500000 Ohm 0,5W Trimmer Potentiometers (unless you think that’s the wrong type of course )

• The poti is OK, but why 500k? If you want to order more then one, take something that you can use for other things. 10k,47k,100k…
The higher the value, the more sensitive to your fingers, moisture and other interferences

• @michael-rudek 10k it is! • With the 10 trimmer potentiometer i’m now reading 512 most of the time, but it keeps jumping to 511 or 513. Is it at all possible to get it steady at 512? I guess that’s why it’s called alternate current So what do i do now to get a usable power consumption reading? When i calculate this to watts it’s still too eratic to be usefull…

• This is not a precision Instrument. And plus minus one digit it not bad.
You have to use a sketch for AC-Current. I use a 100Amp Type for DC, so i dont know how this works with AC. And you cant use my sketch (wich is for a particle photon, not an ESP), because its for DC. I´m measuring my Solar Battery with it. Positive and Negative Current. In and Out
But 512 is “Zero”, in both AC and DC. There are other Types where “Zero” is 0, but not yours.
Find a Sketch for AC-Current.

• Understood, you have been very helpfull, thank you I found an AC-sketch. I’ll put i here for educational purposes so this post might be helpfull as a how-to. It gives me an error margin of about 0,08 amps (about 20 watt) so it might need some finetuning.

``````/*
Measuring AC Current Using ACS712 20A Module
www.circuits4you.com
*/
const int sensorIn = A0;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module and 185 for 5A Module

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void setup(){
Serial.begin(9600);
}

void loop(){

Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;  //root 2 is 0.707
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
}

float getVPP()
{
float result;
int maxValue = 0;          // store max value here
int minValue = 1024;          // store min value here

uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
// see if you have a new maxValue
{
/*record the maximum sensor value*/
}
{
/*record the minimum sensor value*/
}
}

// Subtract min from max
result = ((maxValue - minValue) * 5.0)/1024.0;

return result;
}
``````

Edit: It’s actually pretty close: 385 watt (1,687 A, voltage 228) according to the power consumption meter gives a 1.69 Amps RMS reading. Good enough for me.

Posts 25Views 34356

Looks like your connection to pimatic forum was lost, please wait while we try to reconnect.