Need help with Ultrasonic Sensor

Hi,

I recently purchased (and received - Order No. 100263779) the Waterproof JSN-SR04T Ultrasonic Distance Sensor (SKU SEN0208). I also have a HC-SR04 Ultrasonic Ranging Sensor which I know works.

I set up a small test using my Arduino Uno both sensors (separately). The target was set at 40cm (minimum range on the JSN-SR04T is 25 cm). The HC-SR04 produced the correct result while the JSN-SR04T was wildly fluctuating as in the following screen shot. I suspect a hardware fault but am open to other suggestions.

I’m using an example sketch in the Arduino as follows:

/*Arduino example sketch to control a JSN-SR04T ultrasonic distance sensor with NewPing libary and Arduino. More info: https://www.makerguides.com */

// Include the library:
#include <NewPing.h>

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500 cm:
#define MAX_DISTANCE 400

// NewPing setup of pins and maximum distance.
NewPing sonar = NewPing(trigPin, echoPin, MAX_DISTANCE);

void setup() {
  // Open the Serial Monitor at 9600 baudrate to see ping results:
  Serial.begin(9600);
}

void loop() {
  // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings:
  delay(1000); // I changed it to a 1 second delay
  
  // Measure distance and print to the Serial Monitor:
  Serial.print("Distance = ");
  // Send ping, get distance in cm and print result (0 = outside set distance range):
  Serial.print(sonar.ping_cm()); 
  Serial.println(" cm");
}

Finally, here’s a picture or two of my simple setup:

I would like to use this sensor in an outdoor project I am building so any help in resolving this issue will be greatly appreciated.

Cheers,
David

Hi David,

Hmm, as a first port of call, lets see if we can isolate it to a software or hardware issue. The NewPing library is moderately complex, so I’d suggest testing it out using DFRobot’s very basic sample code:

#define ECHOPIN 3// Pin to receive echo pulse
#define TRIGPIN 2// Pin to send trigger pulse
void setup(){
  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  digitalWrite(ECHOPIN, HIGH);
}
void loop(){
  digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW); // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse, max wait 26ms
  distance= distance/58; //Spd of Snd = 343 m/s = 0.0343cm/us /2 for round trip = 0.01715cm/s => 1/0.01715 ~= 58us/cm
  Serial.print(distance);
  Serial.println("   cm");
  delay(50);// Wait 50mS before next ranging
}

Thanks Oliver. I’ll try that and let you know. Thanks for getting back to me.

I should note that I also have another example code which I have tried with the same result. Here it is for you to have a look at:

/* Arduino example sketch to control a JSN-SR04T ultrasonic distance sensor with Arduino. No library needed. More info: https://www.makerguides.com */

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  
  delayMicroseconds(5);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance:
  distance = duration*0.034/2;
  
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(100);
}

What’s your target surface? Something nice flat and hard is always good, eg. A smartphone or tablet screen.

Also good to watch out for other reflections like off the table.

Okay, I’ve tested with the code you supplied and here are the results. (Target set at a random distance.)

First with my known HC-SR04 sensor

Next with the JSN-SR04T in question

My target surface is a table place mat - very flat and hard. I have the sensors (both) near the table surface so also tried raising them to avoid reflections off the table with this result (distances shifted a little).

It appears to be measuring more accurately however I am still getting those zero readings interspersed throughout. Are these just reflections off something? I don’t get it with the other sensor.

From the arduino reference for pulseIn():

gives up and returns 0 if no complete pulse was received within the timeout.

Seems like it’s not getting a solid return echo and so pulseIn() is timing out (looking at the dfrobot wiki, this sensors normal range is 150us yo 25ms, but 38ms for no echo detected. The sample code above sets the pulseIn() timeout to 26ms.

How’s it fi if you set up your sensor on the edge of the table and hold the placemat at a nice 90° - give it the best chance of success?

It’s worth having a look at the data in the serial plotter too - it’s probably still producing quite viable data with a little bit of filtering.

It’d be interesting to look at the raw signal with a scope. I wonder how much attenuation happens with that 2.5m of cable.

I must say that I prefer the simplicity of the code you supplied. I think I’ll use that in the project instead of the other - no need to make things more complex than they need to be.

1 Like

Okay, I tried your “edge of table” suggestion with similar results. Now that it is returning a correct answer (excluding the zeros) I can just write some code to ignore the zeros in my calculations. My project involves measuring water height to determine whether to switch on a pump so the time span will be in hours rather than seconds (or less). Ignoring the zeros and taking an average value of x number of pulses will be fine.

Here’s the output from the serial plotter you wanted to see:

Anyway, thanks for your help. Unless you have any further suggestions I should be okay now.

2 Likes

That is quite a lot of zeroes! But yep horses for courses

Depending on how accurate you need your pump switching to be and whether you’re powering it from mains or battery, it can be more effective to disregard data based on rate of change - though the code gets a little bit more complex.

I for one am interested to see your final set up :slight_smile:

I am designing a water distillation unit. We live on a property outside of Perth and have both a (very large) rainwater tank plus a 60m bore (connected to a HUGE underground water system). The water from the bore is quite potable and just has small flecks of iron ore in it. (We use it for the gardens and animals.)

My idea (still a ways to go yet) is to design a system which will take advantage of the plenty of sunshine we get here to distill the bore water. So the system will monitor the water level in the pan and, once it gets low enough, turn on the pump to refill it to a certain height. I have yet to determine effective water heights both for evaporation rates and refilling levels. Also, I’m still designing the “pan system” to use. I chose the waterproof sensor because of the obvious environmental issues. Do you know of a better waterproof sensor option? I’d rather have the best if it works better. I just happened to see this one on YouTube.

Now that I can get a reading. My next test is to see how it goes with water, both still and moving.

When I have further results, I’ll update you. Thanks again for your help and interest.

2 Likes

I quickly had a look at a couple of others you stock.

A01NYUB Waterproof Ultrasonic Sensor #### SKU: SEN0313

and the

Water-proof Ultrasonic Sensor (ULS) #### SKU: SEN0300

Any thoughts on these?

I did some proper controlled testing over the weekend and found that the JSN-SR04T is consistently short by 2 to 3 cm. The distances I measured were from 25 to 60 cm in increments of 5 cm. On the other hand, using the same code, the HC-SR04 was consistently accurate (+/- 2 mm). I’m thinking of trying one of the other sensors but will wait to discuss with you before ordering.

Hi David,

I think I answered most of this in our web chat this morning, but just posting here so others can benefit :slight_smile:

Actually, if you can have a plastic pan for what it is you probably don’t need to know the exact level, you just need a float switch. But the distance sensing might be handy for keeping an eye on your evaporation rates. Mechanical aeration would also greatly assist.

Regarding the constant offset of the sensor, it may because the sensor itself is in the base but you’re measuring distance from the front face of the sensor. If it’s a constant 2cm, this should be pretty simple to correct for.

As for the differences between the other two sensors, the A01NYUB only has a single output mode (same as the other sensors) the ME007 (ULS) has multiple output modes. Checkout DFRobot’s (DFRobot supply both of these) wiki pages:
https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313
https://wiki.dfrobot.com/Water-proof%20Ultrasonic%20Sensor%20(ULS)%20%20SKU:%20SEN0300

Thanks Oliver. Some good points. I have also considered building the unit out of fibreglass or similar but have yet to look into it. Plastic might be okay but there is always UV degradation to consider over the long term. Do you guys have any sort of electromechanical float switch?

1 Like

Hey David,

Certainly, lots to take into consideration designing it.

In regards to the float switch, this Seeed sensor would be the closest, it uses a Hall sensor to determine when the float moves away from the body of the sensor.

I would also give this non-contact sensor a look into, it mounts on the outside of the tank and has some good documentation.

Let us know how you go with your project!

Liam.

1 Like

Thanks Liam. I’ll have a look at it.

1 Like

Hey David,

Another interesting idea to get a float switch set up that you can easily customize to best suit your application would be to 3D print it in a plastic resistant to UV (ASA should do the trick pretty well but probably best to read up online what others recommend for it too) then you can make it whatever dimensions, material, design, etc and easily modify it in some CAD program as needed. There should be a few designs available free online if you don’t want to go through the process of designing it manually. Best of luck with the project!