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

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.

• @fregger I want to do exactly the same thing, but I’m failing to understand your final wiring.

The ESP8266 is only able to read 0-1V at the A0 pin (analog), so at zero amps (no load) you should be getting 1024 already, since the ACS outputs 2.5V at 0 amps. Don’t understand how you are getting ~512

Could you please post the final schematics? I’m also new with electronics.

My multimeter reads the expected 2.5V at zero amps on the acs712.

• He uses a voltage divider (10k-Poti)
One end to the sensor, the other to ground. The middle one to A0.
At 0 Amps you trim the Poti till you get 0.5V (or 512 at the esp)

• And here’s the sketch, just fill out the wifi credentials and the mqtt server’s ip-nr. I did not define mqtt-server username and password. Works like a charm

`````` * Modified for wifi and mqtt
Measuring AC Current Using ACS712 20A Module
www.circuits4you.com
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define wifi_ssid "YOUR_SSID"

#define mqtt_server "IP_NR_MQTT_SERVER"

#define solaramps_topic "solar/amps"

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;

WiFiClient espClient;
PubSubClient client(espClient);

void setup(){
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
}

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
if (client.connect("ESP8266Client")) {
// if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void loop(){
if (!client.connected()) {
reconnect();
}
client.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");
client.publish(solaramps_topic, String(AmpsRMS).c_str(), true);
}

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;
}`````````
• Thanks. I’ll buy a couple and test that.

• But don´t buy “normal” Potis, you need the trimmer-type

Posts 25Views 29853

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