Big troubles with the JSN-SR04T

Hello everyone,
i'm having some annoying difficulties with my new JSN-SR04T waterproof ultrasonic sensor. Previously i've been using the HC-SR04T without any problems at all, but now i can't seem to make the JSN-SR04T give me any readings but "-1" (indicading that the sensor is out of reach which it isn't).
i'm sure it has something to do with the coding but i can't seem to figure out what it is... If you have a code that works for you or know what's wrong with mine, then please help a man in need! Thank you so much!

Udgangspunktet.ino (1.38 KB)

Please post the code here and use code tags when you do to avoid any of it being interpreted as HTML commands

#define echoPin 8 // Echo Pin
#define trigPin 7 // Trigger Pin
#define pin 13 // Red light
#define pin 12 // Green light 


int maximumRange = 40; // 
int minimumRange = 20; // 
long duration, distance; // 
void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(13, OUTPUT); 
 pinMode(12, OUTPUT);
}

void loop() {
 
 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5); 
 
 digitalWrite(trigPin, LOW);
 
 duration = pulseIn(echoPin, HIGH);

 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 Serial.println("-1"); 
 digitalWrite(12, LOW), 
 digitalWrite(13, HIGH); 

}
 else {
 Serial.println("distance"); 
 digitalWrite(13, LOW);
 digitalWrite(12, HIGH);
 }
 
 delay(100);
}

Is there any reason why you are not using the NewPing library with this sensor ?

Have you searched the forum for other posts about this device ?

but now i can't seem to make the JSN-SR04T give me any readings but "-1" (indicading that the sensor is out of reach which it isn't).

Well, it's your own fault:

 if (distance >= maximumRange || distance <= minimumRange){
 Serial.println("-1");
 digitalWrite(12, LOW),
 digitalWrite(13, HIGH);

}

Print something meaningful, instead of -1, when the distance is too large.
Print something (else) meaningful, when the distance is too small.

Figure out what problem you are dealing with.

Thank you for the brief respons! I have searched the internet thin and i can't find an answer to why the HC-SR04T works fine but the JSN-SR04T keeps giving me fals readings.. I just tried with the sketch example "NewPingExample" but with the same results - the HC-SR04T working just fine but the JSN-SR04T only reads 19 and 20 cm (which is it's minimum distance). The setup is the exact same.

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(100);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

I made a big mistake buying the JSN SR04T. It does not give accurate readings and the latest abundant version 2.0 is not stable.

I googled the same and the latest version 2.0 is giving problems for many

Searching more and more, I bought this below sensor instead

It has 2 sensors (transmit and receive) instead of 1 sensor of JSN-SR04T and therefore has a lesser minimum distance of 3cm where as JSN SR04T has minimum distance of 20cm.

Plus it is completely sealed from all sides making it fully waterproof.