NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

teckel:
Not enough information to help. But, I would first guess that your sensors are not compatible with the one pin method or you didn't install the capacitor (you don't specify the sensor, circuit, nor code, so who knows).

Also, you're over-complicating things if you're having a problem. SIMPLIFY when you have a problem. If I were you, I would have just one sensor and use two pins (trigger and echo pins). See if that works, if it does, see what it takes to make it fail, then you've found your problem.

In any case, it seems you just need some basic deductive reasoning to isolate what the problem is.

Tim

Thank you very much for the answer. I didn't used any capacitor on them.
As an idea, i wanted to do something like this: Obstacle Avoiding Robot. Object Following Robot. Arduino Project 2018. Part 2.3. - YouTube . He uses hc-sr04 too and it seems to work fine even tough they are 6 sensors, not 4.I have attached the electric scheme, hope you understand something.
All the sonic sensors are connected on the top of l293d shield on +5v and gnd.

Hope you could help me, i need it to get it working fast... thank you for your time.

I would like to connect a capacitor, maybe it helps... what capacitor i need to use?

for coding i used this

NewPing sonar[4] = { 
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE), 
  NewPing(5, 5, MAX_DISTANCE), 
  NewPing(4, 4, MAX_DISTANCE) 
};

SETUP
delay(5000)
serial begin 9600 

LOOP
delay(60)
Serial.println(sonar[0].ping_cm());
delay(300); to see what i receive from the sensor. I used println for every sensor.
[code]

Aylee:
Thank you very much for the answer. I didn't used any capacitor on them.
As an idea, i wanted to do something like this: Obstacle Avoiding Robot. Object Following Robot. Arduino Project 2018. Part 2.3. - YouTube . He uses hc-sr04 too and it seems to work fine even tough they are 6 sensors, not 4.I have attached the electric scheme, hope you understand something.
All the sonic sensors are connected on the top of l293d shield on +5v and gnd.

Hope you could help me, i need it to get it working fast... thank you for your time.

I would like to connect a capacitor, maybe it helps... what capacitor i need to use?

for coding i used this

NewPing sonar[4] = { 

NewPing(3, 3, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE),
  NewPing(5, 5, MAX_DISTANCE),
  NewPing(4, 4, MAX_DISTANCE)
};

on setup
serial begin 9600

and

Serial.println(sonar[0].ping_cm());
delay(300); to see what i receive from the sensor. I used println for every sensor.

[code]

Use the KISS rule. First, use one sensor. Secondly, connect a trigger and echo sensor to that one sensor. Run a test to see if that works. If it works, add stuff back in till it no longer works and that's your problem. If it still doesn't work, make it even more simple (like use the example sketch only with nothing else). Still doesn't work, remove the shield, etc.

There's no way anyone can diagnose your problem, as it's not a library problem. But, it sounds like you're trying to run before you walk. Walk first, then run. Trying to do this quickly is basically the problem.

Tim

teckel:
Use the KISS rule. First, use one sensor. Secondly, connect a trigger and echo sensor to that one sensor. Run a test to see if that works. If it works, add stuff back in till it no longer works and that's your problem. If it still doesn't work, make it even more simple (like use the example sketch only with nothing else). Still doesn't work, remove the shield, etc.

There's no way anyone can diagnose your problem, as it's not a library problem. But, it sounds like you're trying to run before you walk. Walk first, then run. Trying to do this quickly is basically the problem.

Tim

I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!

Aylee:
I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!

Aylee:
I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!

Analog or digital pins is not the issue. The shield has nothing to do with the sonar sensors, so your confusing things by even including it. And you're still trying to get 4 sensors working before you successfully have one working. Maybe you have 4 bad sensors? No way to tell with the method you're using to build/write this. Strip everything down to just the Arduino, sensor, and 4 wires to it. Make sure it works with the sample sketch. Walk first or you'll never figure out the problem.

teckel:
Analog or digital pins is not the issue. The shield has nothing to do with the sonar sensors, so your confusing things by even including it. And you're still trying to get 4 sensors working before you successfully have one working. Maybe you have 4 bad sensors? No way to tell with the method you're using to build/write this. Strip everything down to just the Arduino, sensor, and 4 wires to it. Make sure it works with the sample sketch. Walk first or you'll never figure out the problem.

I'm home now and I've sorted it out, thanks God. The problem was the L293D shield, after removing it the ultrasonic sensors had good reading. I've moved the pins from digital to analog 0-3. Now it's much better:

sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 8
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7

Many thanks for helping me out.. i tough it was a big problem but it always the little things that complicate us.

I am looking for some help building a 2 or 3 sensor unit that produces different tones (audio) as the distance changes. My brother has had a stroke and is partially blind in his left eye and this creates major issues when walking. I am new to arduino but have some electronic background. Any help with sketches would be greatly appreciated... BTW, great thread and project. I have a single sensor working now using your NewPing... Thank you.

Hello, this is my first post and I have great expectations. https://forum.arduino.cc/Smileys/arduino/smiley.gif

This may not quite be the correct forum so any redirection would be appreciated. Thanks

I have a JSN-SR04 sensor used to measure water depth in a tank. It is connected to a Arduino NANO.
At the present time it is still in the test phase. The system is powered thru the computer USB port. I have measured the power with a voltmeter and everything is correct. Now for the problem if I use the pin triggering and the pulseIn process I receive data. The problem is that every fifth reading is about 20% lower than the rest. I tried switching to NewPing in hope for better results. Now with NewPing v1.9 I get reading of only zero. Looking at the hardware the green trigger LED on the SR04 board isn't flashing. I would really like to use NewPing whats wrong?

MountainBoy:
Hello, this is my first post and I have great expectations. https://forum.arduino.cc/Smileys/arduino/smiley.gif

This may not quite be the correct forum so any redirection would be appreciated. Thanks

I have a JSN-SR04 sensor used to measure water depth in a tank. It is connected to a Arduino NANO.
At the present time it is still in the test phase. The system is powered thru the computer USB port. I have measured the power with a voltmeter and everything is correct. Now for the problem if I use the pin triggering and the pulseIn process I receive data. The problem is that every fifth reading is about 20% lower than the rest. I tried switching to NewPing in hope for better results. Now with NewPing v1.9 I get reading of only zero. Looking at the hardware the green trigger LED on the SR04 board isn't flashing. I would really like to use NewPing whats wrong?

The sensor may need more current than you're giving it. How are you powering the sensor? Maybe use an external power supply instead of USB.

Hi,

thanx for the library. It makes the usage of ultrasonic sensors much easier.

How can i calculate tempature compensated ping times with this library?
If not, can you implement a function with temp parameter like ping_cm(25.3)?

Thanx Jens

jensgutsche:
Hi,

thanx for the library. It makes the usage of ultrasonic sensors much easier.

How can i calculate tempature compensated ping times with this library?
If not, can you implement a function with temp parameter like ping_cm(25.3)?

Thanx Jens

Some sensors include a temp sensor and already compensate. The default for the library is set for the speed of sound at typical indoor Temps (something like 75 degrees). It typically isn't required to make temp adjustments because the difference is slight and the sensors are not very accurate to begin with. They're better for detecting something "getting closer" or "getting further away" than actually using it for measurement.

But if you'd like to do whatever with the results, just get the ping time and then do whatever math you'd like based on temp, altitude, humidity, etc. But don't expect it to be anymore accurate for measurement reasons. That's the reason it's not included in the library, in practice it doesn't yield better results, just different ones.

Tim

Hi

I am creating a robot for a school project which uses 3 ultrasonic sensors and 2 infrared sensors but I only have an arduino uno board, so I was wondering if I could use a sketch that pings those 3 sensors using only one pin for each.

Thanks for everything and sorry if somenthing is not too clear, English is not my native language and this is my first program.

Eric_San:
project which uses 3 ultrasonic sensors and 2 infrared sensors but I only have an arduino uno board,

By my count that's only 8 pins: 3x2 for the ultrasonic and 2x1 for the infrared. Uno has loads of room for that....

Eric_San:
Hi

I am creating a robot for a school project which uses 3 ultrasonic sensors and 2 infrared sensors but I only have an arduino uno board, so I was wondering if I could use a sketch that pings those 3 sensors using only one pin for each.

Thanks for everything and sorry if somenthing is not too clear, English is not my native language and this is my first program.

Yes, as long as your sensors work with the one pin method. Try one sensor and see. You can also install a cap as sometimes that works (see the official page for details).

Tim

Thanks for the library, I'm moving over to using Ping_Median to try and settle out some values when i'm reading water levels. Testing this on a hard surface with the single sonar.ping_cm()); in a loop I get lots of zero readings now and then.

If I try a more basic sketch, running as fast I can, I have no such problems. Same board & sensor. Sensor is waterproof ultrasonic from ebay . Any Ideas ? I have noted that when the zero reading is returned the sensor does not click, so it is not being triggered for some reason.

Working Code

const int trigPin = 6;
const int echoPin = 7;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Which returns

Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188
Distance: 188

NewPing Code

int trigPin = 6;       // Trigger
int echoPin = 7;       // Echo
int iterations = 5;    // THe amount of samples we will take
int ledPin =13;
int cm;



#include <NewPing.h>
NewPing sonar(trigPin, echoPin, 300); //Constructor for Tri pin 6, echo pin 7, Distance 300


void setup()
{
 
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT_PULLUP);
}

 
void loop() 
{
 
 delay (700);                   //settle/loop time
 Serial.println(sonar.ping_cm());
 

}

Which returns

197
197
197
0
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
197
0
197
197
197
197
197
0
0
197
197
197
197
197
197
197
197
197
0
197
197
197
197

Hi, this library has a resolution of 1cm, any chance to have more resolution? Thanks

THis seems the best library for this module, I don't see resons to reduce the resolution so much to 1cm, if so I also I don't see reasons to use an unsigned long as a result. How we can reach a distance of 4 bilions of centimeters?

GiovanniG11:
Hi, this library has a resolution of 1cm, any chance to have more resolution? Thanks

THis seems the best library for this module, I don't see resons to reduce the resolution so much to 1cm, if so I also I don't see reasons to use an unsigned long as a result. How we can reach a distance of 4 bilions of centimeters?

Incorrect. The library measures down to the uS. It's the sensor that's only accurate to 1cm. Also, your probably referring to the ping_cm() method, which is designed to return an integer. But, that doesn't mean the library can't do more granular measurements.

In any case, you should probably read the 50 other posts that talk about this above. Or, visit the website and read the documentation.

Honestly, people find it easier to post a message than just RTFM? How is that easier or quicker? Finally, good luck trying to get lower than 1cm readings from a device that's only accurate +/- 0.5cm and temp makes it even further off.

Finally, it's a long int because internal calculation can go over the unsigned int limit, and it leaves the value at such to reduce compile size. If you're looking around in the code, you'd already know this.

Tim

Hi Tim,

Total newbie here, but I have read most of the 79 pages of this Topic and understand that you limit the distance to centimeters because the HC-SR04 sensor doesn't have sufficient precision.

However, I am trying to use your Library with a MB7569 SCXL-MaxSonar-WRM sensor that is supposed to have millimeter precision.

How can I get millimeter readings with your method?
How can I get a sonar.ping_mm() method?

I did try to edit your Library but it was waaay more than I am able to understand.

Thanks

Jcbc:
Hi Tim,

Total newbie here, but I have read most of the 79 pages of this Topic and understand that you limit the distance to centimeters because the HC-SR04 sensor doesn't have sufficient precision.

However, I am trying to use your Library with a MB7569 SCXL-MaxSonar-WRM sensor that is supposed to have millimeter precision.

How can I get millimeter readings with your method?
How can I get a sonar.ping_mm() method?

I did try to edit your Library but it was waaay more than I am able to understand.

Thanks

What I'll never understand is why some believe that the library or even more so I limit things to cm measurements.

The ping_cm() and ping_in() are simply designed to return just that, integers of either cm or in. Integer math is used because it's faster and uses about 1k less if you don't use floating point other places in your sketch. This allows the library to be used on microcontrollers that don't have much memory or with more complex sketches.

In the 79 pages, I've probably stated 100 times how you're free to do anything you wish with NewPing, and if you want to do measurements to a fraction of a mm and consider temperature, elevation, humidity, etc. you're free to do this.

Simply use the standard ping() method! This is the default method. Then, do whatever calculation you want to measure the distance.

So please, look at the sample sketches that use ping(). I should pull all references to ping_cm() so people don't insist the library doesn't support mm measurement.

Hello Tim,

Thanks for your clarifications. I followed your instructions and modified your excellent code so readings are in millimeters.

To avoid using Float numbers I simply use millimeters in the US_ROUNDTRIP_MM value.

Since some readings are Zero when they shouldn't be (in my case they can never be zero), I exclude those readings.

I also average 5 consecutive readings so the calculated distance is more stable.

I am attaching your modified code in case someone else is interested.

Thanks again for your excellent code and your help!

// ---------------------------------------------------------------------------
// Example NewPing library sketch that calculates distance in MILLIMETERS
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  2  // ESP32 pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // ESP32 pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 2000 // Maximum distance we want to ping for (in millimeters). Maximum sensor distance is rated at 4000-5000mm.
#define US_ROUNDTRIP_MM 5650 // Roundtrip sound speed mm/microseconds (In my case 5650 gave me most accurate distance)

int d;
int d1;
int i = 0;
int daverage = 0;  // average of 5 sonar.ping() readings


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

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

void loop() {
  delay(50);           // Wait 50ms between pings. 29ms should be the shortest delay between pings.
  Serial.println("Ping: ");
i = 1;
  while (i <= 5) {
    
  d = sonar.ping();

  if (d == 0)  d = sonar.ping();
  
  daverage = daverage + d;
  i++;
  }

  d=daverage/5;
  daverage = 0;

  Serial.print (" Average ping time in microseconds = ");
  Serial.println (d);
  
  d1 = ((1000*d / US_ROUNDTRIP_MM));   //  Calculate Distance in mm

  Serial.print("Distance = ");
  Serial.print(d1);  // Print distance in m
  Serial.println("mm  ");
  Serial.println(" ");
  delay(1000); //  Wait 1 second until next reading (can be zero or any other number) 
}

Hi, thanks for the great job.
I'm trying to use the median filter.
How can I make 10 (1 reading every 2 seconds one from the other) and perform the median of these in order to have a more stable reading? I have seen that in the library there is the possibility of varying the distance between the pings (of default is 29 milliseconds) but I can't find an example to take this to 10 seconds.
Thanks for your help. Greetings Fabio