BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Nodemcu + JSN SR04T 2.0 unstable readings

#1

Hello!

I am trying to make a simple water level meter and connect to Blynk.
The hardware is Nodemcu V3 and JSN SR04T 2.0
It connects and no errors during compilation of code, but the readings from the sensor is very unstable. I have read in other forums about the unstable readings from the JSN SR04T 2.0, but so far no solutons.
Does anyone have any ideas how to make it work properly ?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NewPing.h>
//#include <ArduinoOTA.h>                                                             //added for OTA
 
    #define SONAR_NUM 2                                         // Number of sensors. Change to suit your requirements.
    #define PI 3.1415926535897932384626433832795
    
    //** CHANGE TO SUIT TANK DIMENSIONS
    const int MAX_DISTANCE = 200;                       //max distance to measure
    const int Diameter1 = 160;		                            //internal Diameter of tank 1 in cm		    
    const int Diameter2 = 160;		                            //internal Diameter of tank 2 in cm		   
    const int Depth1 = 220;			                            //total depth of tank 1 in cm	, from sensor to base	inside 
    const int Depth2 = 200;			                            //total depth of tank 2 in cm, from sensor to base inside  

    const unsigned int Period = 1000;    //period between pings, in milliseconds. i.e  1 minute = 60,000. max 65535. Want longer? use unsigned long
    
    //** SENSOR PINS
  
    const int PingPin1 = 5;                          //     GPIO5,  D1 
    const int EchoPin1 = 4;                        //     GPIO4,  D2
    const int PingPin2 = 14;                         //     GPIO14,  D5
    const int EchoPin2 = 12;                         //     GPIO12,  D6
    
    const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));	        //area of base of tank 1
    const int Area2 = PI * ((Diameter2 / 2) * (Diameter2 / 2));	        //area of base of tank 2
    
    // Global variables
    int Litres1, Litres2, Distance1, Distance2, WaterDepth1, WaterDepth2;
    
    // Set password to "" for open networks.
    char ssid[] = "xxxxxxxxxxx";	                                                //local wifi network SSID
    char pass[] = "xxxxxxxxxx"; 			                                //local network password
  
   char auth[] = "xxxxxxxxxxxx";                                       

    BlynkTimer timer;                                                                   

    NewPing sonar[SONAR_NUM] = {		                                // Sensor object array.
	    NewPing(PingPin1, EchoPin1, MAX_DISTANCE),	        // Each sensor's trigger pin, echo pin, and max distance to ping.
	    NewPing(PingPin2, EchoPin2, MAX_DISTANCE)
    };

void sendSensorReadings()
{
//***********Readings Tank 1
    Distance1 = sonar[0].ping_cm();                                       //get distance to the top of the water tank 1
    if (Distance1 >= Depth1 || Distance1 == 0 )  Distance1 = Depth1;                   //check it does not go negative
	WaterDepth1 = Depth1 - Distance1;							        //calculate the depth of the water
	Litres1 = (Area1 * WaterDepth1) / 1000;	                         //calculate the volume of the water in litres
   
//************Readings Tank 2
	Distance2 = sonar[1].ping_cm();								     //get distance to the top of the water tank 2
   if (Distance2 >= Depth2 || Distance2 == 0)  Distance2 = Depth2;                   //check it does not go negative
	WaterDepth2 = Depth2 - Distance2;							     //calculate the depth of the water
	Litres2 = (Area2 * WaterDepth2) / 1000;		                //calculate the volume of the water

//***********SEND INFO TO BLYNK
    Blynk.virtualWrite(V1, WaterDepth1);                                //send depth to Blynk server
    Blynk.virtualWrite(V2, Litres1);                                            //send litres to Blynk server
    Blynk.virtualWrite(V3, Litres1 / 10);                                   //send litres to Blynk server. for vertical level widget & chart, 
                                                                                                //scaled to 1/10 as Blynk only goes up to 9999 and we need up to 16000
    delay(50);
    Blynk.virtualWrite(V4, WaterDepth2);                            //send depth to Blynk server
    Blynk.virtualWrite(V5, Litres2);                                      //send real litres to Blynk server
    Blynk.virtualWrite(V6, Litres2 / 10);                               //send litres/10 to Blynk server

    digitalWrite(13, HIGH);                                                //flash the LED on D7, just to let us know it's running
    delay(50);
    digitalWrite(13, LOW);
 
    

    Serial.println();
    Serial.println();
    Serial.println("Tank 1 water distance: " + String(Distance1));   //print depth
    Serial.println("Tank 1 water depth: " + String(WaterDepth1));  //print depth
    Serial.println("Tank 1 Litres: " + String(Litres1));                //print litres

    Serial.println();
    Serial.println("Tank 2 water distance: " + String(Distance2));   //print depth
    Serial.println("Tank 2 water depth: " + String(WaterDepth2));   //print depth
    Serial.println("Tank 2 Litres: " + String(Litres2));    //print litres 
    

}

void setup() {

    pinMode(13, OUTPUT);                                                //LED D7
    
	timer.setInterval(Period, sendSensorReadings);		// Setup a function to be called every ten seconds
	delay(10);
  

    Serial.begin(115200);										        // Open serial console. 
    Serial.println();
    Serial.println("Connecting to " + String(ssid));      // Connected to WiFi network
//**	
    Blynk.begin(auth, ssid, pass);
	delay(20);
    

	Serial.println(WiFi.localIP());								     //this is local IP for this board
	Serial.println("WiFi connected");

}

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

0 Likes

#2

Have you tried searching this forum:

https://community.blynk.cc/search?q=SR04

Pete.

0 Likes

#3

Hello Pete!

Yes, but i can’t find anything regarding this topic.

0 Likes

#4

Välkommen! :wink:

timer.setInterval(Period, sendSensorReadings);
// Setup a function to be called every ten seconds

const unsigned int Period = 1000; 
// period between pings, in milliseconds. i.e  1 minute = 60,000. max 65535. Want longer? use unsigned long

That looks like ONE second to me :stuck_out_tongue: When in doubt, easy up on the timers he he.

Also does this calculate correctly? Your PI is defined with a million decimals or so, but all other data types are int (= no decimals). I mean, since it’s a water tank, shouldn’t be float? :rofl:

const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));

0 Likes

#5

Then search for other Ultrasonic topics… the principle is the same. Your code does the measuring and Blynk handles the GUI control and display of the results.

For example, this is how I managed an Ultrasonic sensors picky timing with Blynk’s need for timing as well…

Also, Google for normal code and get it working so you can study it and understand the principles behind the code, before also tackling the integration of such code with Blynk.

0 Likes

#6

I managed to make it work.
There is a tunable inductor on the JSN SR04T 2.0.
I just turned it a little bit and all readings became perfect! Amazing!

0 Likes

#7

@David_Gronlund hey can you explain in detail so that it will be useful. Even I am having the same issue.

0 Likes

#8

As stated… try turning the “tunable inductor” :stuck_out_tongue_winking_eye:

image

0 Likes

#9

@Gunner Thanks for the reply. I can change the inductor, but the issue is I am not able to understand the reason. Can you tell me briefly how changing inductor works?

0 Likes

#10

It changes the inductance of the circuit… apparently part of that device’s fine tuning.

The hows and whys of that is not a topic for this forum, but if you Google it you might find more info about inductors.

1 Like