Doubts current sensor ACS712 30A + Nodemcu ESP8266

Does anyone have an example with the current sensor ACS712 30A and nodemcu ESP8266-12 ?

Hug

Hi,

Is your question about Blynk? Or about how to work with this component in general?

Hello , Pavel !

It is about working with him in blynk using esp8266 .

hug

So I would recommend getting it Working without Blynk, and when it works, start adding Blynk. :wink:

1 Like

Hi! did you do your project with acs712?

i am trying to measure AC with ACS712 30A + Nodemcu ESP8266 + Blynk
I ve managed to get current readings in BLYNK with a simple code that is used for DC Current,
everything works fine with esp8266 and blynk but the readings are not correct .
Another code is needed to transform the AC current readings int something meaninful but this code uses a getVPP function in the loop section that causes esp8266 to get disconnected ornot connect at all
The code im using is the following

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "........";

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


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



void setup(){ 
 Serial.begin(9600);
 Blynk.begin(auth, "........", "..........");
}

void loop(){

  Blynk.run();
 
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");

}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  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
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

[26531] Connected to WiFi
[26531] IP:ā€¦
[26531] Blynk v0.3.8 on NodeMCU
[26531] Connecting to blynk-cloud.com:8442
0.21 Amps RMS
0.24 Amps RMS
0.24 Amps RMS
[29705] Login timeout
0.24 Amps RMS
0.24 Amps RMS
[31706] Connecting to blynk-cloud.com:8442
0.24 Amps RMS
0.26 Amps RMS
0.21 Amps RMS
[34868] Login timeout
0.21 Amps RMS
0.24 Amps RMS

how can i solve this connectivity failure problem?

@Polikarpos is the DC current reading required for every ms of every day?

Take everything out of loop and use the SimpleTimer library that is provided by Blynk to call functions.

Even the 1 second sampling should probably be done with SimpleTimer.

SimpleTimer allows other functions to run at the same time as the timer which is essential for ioT devices.

1 Like

thanx for the replyā€¦i ll try thatā€¦ĪµĪ»Ī»Ī·Ī½Ī±Ļ‚ ĪµĪ¹ĻƒĪ±Ī¹?

No, but I live on a ā€œGreekā€ island.

anyway thnx i wil study more and see what happens

I have the same CostasĀ“s ideia, if you remove this part of the code should be work, but you need to send less than 10 values per second to Blynk Cloud, otherwise you hardware will be disconeted. Use more than 1s to read/send the measure.

I tried to read my ACS712 without Blynk for a while, but I couldnĀ“t read the correct value using an arduino. I used tree or four diferents codes, but nothingā€¦ As soon as possible I will try using your code @Polikarpos. thanks

IĀ“m trying to measure a current of a ā€œiron clothesā€ (I donĀ“t know the correct name of this equipment =D), I should read something like 10A, but the ACS712 30A only read:

0.08 Amps RMS
0.05 Amps RMS
0.08 Amps RMS
0.05 Amps RMS
0.08 Amps RMS
0.08 Amps RMS
0.08 Amps RMS
0.05 Amps RMS
0.05 Amps RMS
0.08 Amps RMS

Should I do something else? like a calibration, or something like that? IĀ“m using the same @Polikarpos code.

Thanks guys

I found the problem. was my severals nodeMCU connected on my computer at the same time!!! OMG! so many ports, and I was uploading the code to the wrong nodemcuā€¦

I did the code. the loop ask the read function using SimpleTimer, 1 time per second and send the value to the blynk on V13 I used a GAUGE widget.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "...";
char ssid[] = "....";
char password[] = "...";


const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module

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

SimpleTimer timer0;

void readACS712() {
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");
 Blynk.virtualWrite(V13, AmpsRMS);
 pinMode(sensorIn, INPUT);
}

void setup(){ 
 Serial.begin(9600);
 Blynk.begin(auth, ssid, password);
 timer0.setInterval(1000L, readACS712);
 
}

void loop(){
 Blynk.run();
 timer0.run();
 
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  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
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }
4 Likes

your code seems more legit than mineā€¦i got it working by modifying float getVPP function and messing around with the timesā€¦it seems ok but i ll try your code too
here it is:

    #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>

    char auth[] = ".........";

    const int sensorIn = A0;
    int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


    double result;
    double VRMS = 0;
    double AmpsRMS = 0;

    SimpleTimer timer;
    void getVPP()
    {
       int readValue;             //value read from the sensor
      int maxValue = 0;          // store max value here
      int minValue = 1024;          // store min value here
      
       uint32_t start_time = millis();
       while((millis()-start_time) < 200) //sample for 1 Sec
       {
           readValue = analogRead(sensorIn);
           // see if you have a new maxValue
           if (readValue > maxValue) 
           {
               /*record the maximum sensor value*/
               maxValue = readValue;
           }
           if (readValue < minValue) 
           {
               /*record the maximum sensor value*/
               minValue = readValue;
           }
       }
       
       // Subtract min from max
       result = ((maxValue - minValue) * 5.0)/1024.0;
          
      VRMS = (result/2.0) *0.707; 
     AmpsRMS = (VRMS * 1000)/mVperAmp;
     Serial.print(AmpsRMS);
     Serial.println(" Amps RMS");
     Blynk.virtualWrite(7,AmpsRMS);
     if (AmpsRMS < 0.2)
    {
      Blynk.virtualWrite(8,"LAMP OFF");
    }
    else
    {
      Blynk.virtualWrite(8,"LAMP ON");
    }
      
     }



    void setup(){ 
     Serial.begin(9600);
     Blynk.begin(auth, ".......", "........");
         timer.setInterval(3000, getVPP);

void loop(){
  Blynk.run();
  
  timer.run();
}
      }

if i set timer.setInterval(1000, getVPP); instead of 3000 the nodemcu gets disconnectedā€¦

@Polikarpos I notice you have the sampling time down at 200ms. With @glauberbiroā€™s sketch it has a sampling time of 1s and a 1s loop. This means it is sampling constantly 24 hours a day which gives no time to do anything else. Normally this is a recipe for disaster.

Not sure why you need a 3s loop as 1s should be ok at first glance of your sketch.

so youre saying that sampling time down to 200ms is better?
i got both sketches working perfectly with no blynk disconnections but using at least 1.1s loop time. and sampling time less than 1sā€¦0.5s works fineā€¦cant explain why but suits me fine
3s or 2s loop is no problemā€¦just want to control a device and get feedback of its status with blynkā€¦

I always ask myself the question, how much time do I need and how accurate do I need to be? For example, I have a light on a timer which checks the light level every 30s (I think) to see if it needs to turn on. This is more than enough. I couldnā€™t care less if my lights go on 30s later or earlier, so I donā€™t need time critical code.

When operation a robot for example, it could well be more time critical. Always ask your self if there is room for performance improvement within these kind of applications. Measuring current is really not time critical. I try to make the biggest timer I can find which is still usable and use all Push frequencyā€™s so I can update the App from the hardware side. Why? Because the hardware is, for now, the slowest component in the whole bunch of IoT devices. So I donā€™t want to push it (pun intended) to the limit. :slight_smile:

1 Like