JSN _SR40T Gives Zero value every other reading ESP32

I am using the ESP32 Board with the JSN-SR40T sensor. I have the sensor wired correctly and it is getting 4.84V to the sensor from the VIN pin on the Esp32. The issue I’m having is with the reading being zero every other time the function sendSensorReadings() is called via the blynk timer. I have added an exponential filter to try and smooth the data but it is still not good enough. I also tried using the BLYNK_CONNECTED() function incase it was a sync issue. None of that has helped and I cannot find anyone that has had a similar problem. I currently have it taking a reading every 5 minutes but I was originally using shorter intervals with the same issue. I am using the Blynk app on an Iphone 11 and Adruino IDE to program the ESP32. I am also using the blynk servers and not a local one. I have also tried multiple sensors, power sources, and esp32’s with the same result.


#include <WiFi.h>
#include <WiFiClient.h>
#include <Blynk.h>
#include <BlynkSimpleEsp32.h>
#include <MegunoLink.h>
#include <Filter.h>
//#include <ArduinoOTA.h> 

#define trigPin 25
#define echoPin 26
 
 char ssid[] = "TMax";                                           //local wifi network SSID
 char pass[] = "charger1";                                      // local network password
 char auth[] = "GaKxoyGpUVF7kmi7LBtExrDW2DS081_l";             //  Authority Token  
 float Distance_cm;
 float Distance_ft;
 float duration;
 float Duration_clean;
 long FilterWeight = 55;
 ExponentialFilter<long> ADCFilter(FilterWeight, 0);
 BlynkTimer timer;   //config timer

BLYNK_CONNECTED() {
  //get data stored in virtual pins V1 and V2 from server
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
}

void setup() { 
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 // ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });       
 // ArduinoOTA.begin();        
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(300000L, sendSensorReadings); 

}

void sendSensorReadings()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); 
  Distance_cm = duration *0.034/2;
  Distance_ft = 0.0328084 * Distance_cm;
  ADCFilter.Filter(Distance_ft);
  Blynk.virtualWrite(V1,ADCFilter.Current());
  Blynk.virtualWrite(V2, Distance_ft);
 
}

void loop() {
  
  Blynk.run();
  timer.run();
  //ArduinoOTA.handle();
}

Here is the image I’m getting on Blynk. The raw data is the top value and the data in the filter is below. You can see where the filtered value doesn’t go to zero but it still gives a sawtooth shape instead of remaining at a constant level

First of all, what version of the Blynk library are you using?
You’re asked to provide this info when you create the topic, and it is important data.

The only purpose of the Blynk.syncVirtual(Vx) commands you have in your BLYNK_CONNECTED() callback is to cause the corresponding BLYNK_WRITE(Vx) callback functions to be triggered. As you don’t have these functions in your code, and have no reason to ad them, this piece of code is redundant.

Have you tried adding serial print statements to your code and seeing what readings you are getting? This would narrow-down the problem to being a sensor related issue or a Blynk communications issue, depending on whether the serial monitor exactly mirrors Blynk, or shows data every time it is requested.

Pete.

I apologize, I am using version 1.0.0-beta3. I have tested using serial print statements and it worked, but that was before I added blynk and wifi capabilities, so I will try that again. Also, thanks for telling me about the BlynkConnected redundancy. I was not sure if that was actually doing anything.

You should un-install this and install 0.6.1 instead.
You should also add your serial print statements back in and monitor the results.

Pete.

The issue ended up being with the sensor and not blynk. I will post my code below. I believe there was a timing issue that caused the “0” readings that was fixed when i increased the delay between pings. It still occasionally reads 0 values and odd readings but infrequently enough for them to be filtered with an ‘if’ statement. Thanks Pete for the help.

void sendSensorReadings()
{ 
 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(20);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  Distance_cm = duration *0.034/2; 
  if (Distance_cm >= 30 && Distance_cm <= 450) 
  {
    Distance_ft = 0.0328084 * Distance_cm;
    Blynk.virtualWrite(V0, Distance_ft);
 };
 
}

I recommend to anyone dealing with this to try several different timing values until you find one that works with your sensor.