Virtual pins has a data always

I’m using virtual pins for my current sensor ACS712 my problem is eventhough i removed the current sensor from my arduino board there always data received by the app :sob:

Presumably you mean that you have the sensor connected to physical pins on your Arduino and that you’ve written some code which publishes this data to the Blynk server via a virtual pin?

Can you post your code (correctly formatted of course) and provide some info on the widgets you’re using in the app and which pins they’re using?

Pete.

BlynkTimer timer;

  IPAddress server_ip (192, 168, 1, 47);

  // Mac address should be different for each device in your LAN
  byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
  IPAddress arduino_ip ( 192,   168,   1,  55);
  IPAddress dns_ip     (192, 168, 1, 1);
  IPAddress gateway_ip (192, 168, 1, 1);
  IPAddress subnet_mask(255, 255, 255,   0);

  #define W5100_CS  10
  #define SDCARD_CS 4


  const int sensorIn = A0;
unsigned int mVperAmp = 66;   // 185 for 5A, 100 for 20A and 66 for 30A Module
float ACSVoltage = 0;
float VRMS = 0;
float AmpsRMS = 0;
float inputV = 238.0;           // reading from DMM
float mcuVoltage = 5.0;         // MCU ADC maximum - voltage divider means we should use 5.0V reference not 3.2V
float WH = 0;
unsigned int calibration = 66;  // V2 slider calibrates this
unsigned int pF = 95;           // Power Factor default 90
float energyCostpermonth = 0;   // 30 day cost as present energy usage incl approx PF 
unsigned int energyTariff = 1437; // http://www.energysavingtrust.org.uk/about-us/our-calculations

void getACS712() {  // for AC
  ACSVoltage = getVPP();
  VRMS = (ACSVoltage/2.0) *0.707; 
  VRMS = VRMS - (calibration / 10000.0);     // calibtrate to zero with slider
  AmpsRMS = (VRMS * 1000)/mVperAmp;
  if((AmpsRMS > -0.015) && (AmpsRMS < 0.008)){  // remove low end chatter
    AmpsRMS = 0.0;
  }
  Serial.print(" Volts RMS ");
  Serial.print(String( ACSVoltage , 3));
  Serial.print(String(AmpsRMS, 3));
  Serial.print(" Amps RMS");
  Serial.print("\tPower: ");  
  WH = (inputV * AmpsRMS) * (pF / 100.0);  
  Serial.print(String(WH, 3)); 
  Serial.print(" WH"); 
  Blynk.virtualWrite(V0, String(AmpsRMS, 3));
  Blynk.virtualWrite(V1, String(WH, 3));
  Blynk.virtualWrite(V3, int(WH + 0.5));  // gauge
  energyCostpermonth = 24.0 * 30.0 * (WH / 1000.0) * (energyTariff / 10000.0);
  Serial.print("  Approx cost per month: £"); 
  Serial.println(String(energyCostpermonth, 2));
  Blynk.virtualWrite(V6, String(energyCostpermonth, 2));
  Blynk.virtualWrite(V7, String(ACSVoltage, 2));
}

float getVPP()
{
  float result; 
  int readValue;                
  int maxValue = 0;             
  int minValue = 1024;          
  uint32_t start_time = millis();
  while((millis()-start_time) < 950) //read every 0.95 Sec
  {
     readValue = analogRead(sensorIn);    
     if (readValue > maxValue) 
     {         
         maxValue = readValue; 
     }
     if (readValue < minValue) 
     {          
         minValue = readValue;
     }
  } 
   result = ((maxValue - minValue) * mcuVoltage) / 1024.0;  
   result = result - 511;
   return result;
 }

// V0 AC IRMS display
// V1 WH display

//BLYNK_WRITE(V2) {  // calibration slider 50 to 70
  //  calibration = param.asInt();
//}

// V3 WH gauge

BLYNK_WRITE(V4) {  // set supply voltage slider 70 to 260
    inputV = param.asInt();
}

BLYNK_WRITE(V5) {  // set 5, 20 or 30A ACS712 with menu
    switch (param.asInt())
    {
      case 1: {       // 30A
        mVperAmp = 66;
        break;
      }
      case 2: {       // 20A
        mVperAmp = 100;
        break;
      }
      case 3: {       // 5A
        mVperAmp = 185;
        break;
      }
      default: {      // 30A
       mVperAmp = 66;
      }
    }
}

// V6 Cost gauge

BLYNK_WRITE(V7) {  // PF slider 60 to 100 i.e 0.60 to 1.00, default 95
    pF = param.asInt();
}

BLYNK_WRITE(V8) {  // Energy tariff slider 1000 to 2000, default 1437 (UK £ 0.1437 / KWH)
    energyTariff = param.asInt();
}


  void setup()
  {
    // Debug console
    Serial.begin(9600);

    pinMode(SDCARD_CS, OUTPUT);
    digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

     Blynk.begin(auth, server_ip, 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
     timer.setInterval(1000L, getACS712); // get data every 2s

    //Blynk.begin(auth);
    // You can also specify server:
    //Blynk.begin(auth, "blynk-cloud.com", 8442);
    //Blynk.begin(auth, IPAddress(192,168,1,46), 8443);
     
    
  }


  void loop()
  {
    Blynk.run();
    // You can inject your own code or combine it with other sketches.
    // Check other examples on how to communicate with Blynk. Remember
    // to avoid delay() function!
    timer.run();
  }

That is a sensor that uses an analog pin… and an analog pin will float random readings when there is nothing connected to it, and there is no pull up or down resistor… so basically you are reading the random readings of a floating Analog Input pin :stuck_out_tongue_winking_eye:

1 Like

is there anything way to calibrate it? by the way by the time I connect the sensor again into the board there’s a fluctuate reading on it (about 13.00) and back to random reading like .02… or .03…

Calibrate what?? The Analog pin? No, it gets precisely whatever data range you give it between GND and 3.3v (or 5v for Arduino).

Remove the signal completely and it floats (basically picking up whatever random stray voltages it senses from capacitance and/or neighboring pins… this is perfectly normal.

So if for some strange reason you want to keep “reading” the pin when the sensor is removed, then implement a way to “read” 0 in your code, or via physical, switchable, pulldown… don’t leave it there when sensor is hooked up or you will get erroneous data.

That sensor itself may pick up random “magnetic signals” (it is a HALL effect sensor) if it is not hooked up to some current source of its own. It also tends to vary back and forth just a little bit. You will not get a simple 0-1024 natively out of it, as it centers around a midway point (it is really expecting an AC signal) and requires specific coding and averaging to interpret the “current” range.

PS - it is also not very accurate at lower ranges… perhaps anything below 1-2A from a 20A capable sensor, from my experience.