JSN-SR04T + New Ping library - Problem when there is no obstacle

Hello guys!

I am using a JSN-SR04T waterproof ultrasonic distance sensor with the New Ping library. When the sensor detects some obstacle closer than 20cm, the library returns 0. So far, so good.

The problem is when I point the sensor into some direction where there is no obstacle (for example, point the sensor in the sky direction). When I do it, the sensor returns a set of inconsistent values (e.g., 100cm, 153cm, 120cm, 78cm, 0cm, 0cm, 0cm, 112cm...)

I set up the New Ping library to measure distances from 20cm to 160cm. So, I would expect that when I point the sensor in the sky direction, the New Ping library would return 0 for me and it is not happening.

I am using a 1A external power supply.

Do you guys have any idea why is it happening?

Thank you!

Can you please post your sketch and circuit diagram? I've used that sensor myself and haven't had issues like you describe (only when my power rail isn't stable).

Hi npkamen,

I am using arduino uno with an external power supply (12volts - 1A). The ultrassonic sensor pins are connected as follows:

Trigger pin - Arduino port 12
Echo pin - Arduino port 11
5v - Arduino 5v port
GND - Arduino GND port

The sketch is a New Ping library example:

#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(50);                     // 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 have two sensors. The problem happens with both of them.

What happens if you use the ping_median function across 10 or so pings?

Personally I use it with 30 iterations as I only need an update once per minute.

Hi Daniel,

Sorry for posting so late, but I am having a similar problem with my new jsn-sr04,

I didn´t have that problem before, using the same code. I am beggining to think theres something wrong with the new versions of the sensor.

I'm running into exactly same issue with 3 sensors on an Arduino UNO. I also strongly suspect it's a sensor issue.

Daniel, is your issue resolved later? I'm thinking about trying different sensors.

I bought 4 JSN-SR04T and I have a same issue. It has not got a stable output.

It is a JSN-SR04T 2.0 version.

Don't know if you're still trying to get it working, but I thought I'd throw out what worked for me. For starters I had to set the echo pin as a pullup input:

pinMode(ECHO_PIN,INPUT_PULLUP);

Originally I was getting 0cm back almost every ping; that got me a reading maybe 4 or 5 out of 6 pings, with intermittent 0cm readings. But the readings were all over the place. I watched the trigger and echo pins on a scope and it was fairly regularly not responding to the trigger. I thought maybe the timings were off slightly on either my arduino or the SR04T, so I edited the ping_trigger() function in newping.cpp, changing the delays from 4 and 10 to 6 and 12:

	*_triggerOutput &= ~_triggerBit;   // Set the trigger pin low, should already be low, but this will make sure it is.
	delayMicroseconds(6);              // Wait for pin to go low.
	*_triggerOutput |= _triggerBit;    // Set trigger pin high, this tells the sensor to send out a ping.
	delayMicroseconds(12);             // Wait long enough for the sensor to realize the trigger pin is high. Sensor specs say to wait 10uS.
	*_triggerOutput &= ~_triggerBit;   // Set trigger pin back to low.

Results have been great. Haven't had a missed ping since.

On a side note, I changed the output on ping_in (the only one I use) to float so I could get fractions of an inch. I saw at least one post where somebody requested decimals in ping_cm or ping_in and the author of NewPing responded that the sensors aren't capable of sub-centimeter accuracy, so the extra precision was not useful. But in my testing, my sensor results tend to stay within about 0.4-0.5" of each other, at least at sizeable distances, barring the occasional wildly inaccurate reading which is easily detectable. Mine gives a reading that's 15-50" different from the rest about every 15-20 pings, but because they're so far off, it's easy to pick out and discard them.

I left it running for an hour, taking a measurement every second, while pointed at my ceiling from my desk. After discarding any measurements more than 5" above or below the average, the low measurement was 64.87", high was 64.93", and the vast majority of the measurements were between 64.88 and 64.90. Lowering the sensor a bit at a time seems to indicate that the sensor can reliably detect changes greater than 0.1". I'd say a quarter of an inch precision, at least at the above distance, isn't unreasonable to assume.

1 Like

Thanks chantling