jsn sr04t v2.0 questions.

Hi all,

i hope someone can help me with a problem i'm facing with the jsn sr04t v2.0.

While building a distance meter, with two sensors (oposite 180 degree, so each facing to one side), i find the reading too unprecise.

I wrote the following code, but even with an average for trying to eliminate floating, it's still very unprecise.

#include <Wire.h>
#include <LCD.h> // For LCD
#include <LiquidCrystal_I2C.h>

// SDA - A4
// SCL - A5
// LCD for debug and setup - after everything is fine, no LCD
// Set the LCD I2C address, if it's not working try 0x27
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Define TRIG_ and ECHO_ pin:
#define TRIG_Pin_1 1
#define ECHO_Pin_1 2
#define TRIG_Pin_2 3
#define ECHO_Pin_2 4

#define AVG 20

int counter1;
int counter2;
int count = 0;

void setup() {
  pinMode(TRIG_Pin_1, OUTPUT);
  pinMode(TRIG_Pin_2, OUTPUT);
  pinMode(ECHO_Pin_1, INPUT_PULLUP);
  pinMode(ECHO_Pin_1, INPUT_PULLUP);
  // Open the Serial Monitor at 9600 baudrate to see ping results:
  lcd.begin(16, 2);  // iInit the LCD for 16 chars 2 lines
  lcd.backlight();   // Turn on the backligt (try lcd.noBaklight() to turn it off)
  lcd.clear();
//  Serial.begin(9600); // Serial monitoring 
}

void loop() {
    digitalWrite(TRIG_Pin_1, LOW); // Set the trigger pin to low for 2uS 
    digitalWrite(TRIG_Pin_2, LOW); // Set the trigger pin to low for 2uS
    delayMicroseconds(2); 
    
    digitalWrite(TRIG_Pin_1, HIGH); // Send a 10uS high to trigger ranging 
    digitalWrite(TRIG_Pin_2, HIGH); // Send a 10uS high to trigger ranging 
    delayMicroseconds(20); 
    
    digitalWrite(TRIG_Pin_1, LOW); // Send pin low again 
    int distance1 = pulseIn(ECHO_Pin_1, HIGH,26000); // Read in times pulse 
    distance1= distance1/58; //Convert the pulse duration to distance 
    counter1 = counter1 + distance1;
                       
    digitalWrite(TRIG_Pin_2, LOW); // Send pin low again 
    int distance2 = pulseIn(ECHO_Pin_2, HIGH,26000); // Read in times pulse 
    distance2= distance2/58; //Convert the pulse duration to distance
                           //You can add other math functions to calibrate it wel 
    counter2 = counter2 + distance2;

    count++;
    if (count == AVG)
    {
      count = 0;
      counter1 = counter1/AVG;
      lcd.setCursor(0, 0);
      lcd.print("Dis 1 = ");
      // Send ping, get distance in cm and print result (0 = outside set distance range):
      lcd.print(counter1); 
      lcd.print(" cm");      
      counter2 = counter2/AVG;
      lcd.setCursor(0, 1);
      lcd.print("Dis 2 = ");
      // Send ping, get distance in cm and print result (0 = outside set distance range):
      lcd.print(counter2);  
      lcd.print(" cm");     
    }                                                
 //   delay(50);

}

Anyone with experience with the jsn sr04t v2.0 able to help me out to tune this, so reading becomes more precise? It must acquire distance fast and at least 90% precise.

THank you in advance.

I have just experimented with the SR04T and the classic SR04 (indoor - non weather sealed)
What I found out:
SR04T

  • signal is very weak and has problems with softer reflection material, sometimes doesnt detect hands, cutton coatet things or cloth etc.
  • reflection angle dependend, small changes wont return any signal
  • distance must be >20cm else no signal!

So first check that your setting has good and stable reflection.

Another hint: divide, calc cm distance, after! calculating the average else you will loose digits with your int.
(but take care, use unsigned long integer for sum)

And: Handle sensors in serial, else you will ait for sensor 1 while sensor 2 ist getting its echo!
do
Trigger sensor 1, get echo of sensor 1
Trigger sensor 2, get echo of sensor 2

instead of

Trigger sensor 1,
Trigger sensor 2,
get echo of sensor 1 ==> will wait for sensor echo!
get echo of sensor 2 ^^^ may delay the return of

Turning the TRIG to low later does not change the starting time nor the reflection behaivoir!

unsgigned long sum1,sum2;
-----
digitalWrite(TRIG_Pin_1, LOW); // Set the trigger pin to low for 2uS 
delayMicroseconds(2);     
digitalWrite(TRIG_Pin_1, HIGH); // Send a 10uS high to trigger ranging 
delayMicroseconds(20);     
digitalWrite(TRIG_Pin_1, LOW); // Send pin low again 
int distance1 = pulseIn(ECHO_Pin_1, HIGH,26000); // Read in times pulse 
sum1 += distance1; // add
digitalWrite(TRIG_Pin_2, LOW); // Set the trigger pin to low for 2uS 
delayMicroseconds(2);     
digitalWrite(TRIG_Pin_2, HIGH); // Send a 10uS high to trigger ranging 
delayMicroseconds(20);     
digitalWrite(TRIG_Pin_2, LOW); // Send pin low again 
int distance1 = pulseIn(ECHO_Pin_2, HIGH,26000); // Read in times pulse 
sum2 += distance2; // add 

....
avg1 = sum1 / AVG
dist1 = avg1/59

etc etc,...