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

Hi there,

Apologies if this has been answered already, im a beginner so just trying some small experiments out. When the sensor distance exceeds its maximum it defaults to 0cm. Is there anyway this can be changed to the max distance value?

Thanks

harrygover: Hi there,

Apologies if this has been answered already, im a beginner so just trying some small experiments out. When the sensor distance exceeds its maximum it defaults to 0cm. Is there anyway this can be changed to the max distance value?

Thanks

This is a common question/confusion but if you think about it, it makes total sense that it works the way it does.

Imagine you set the max distance to 100cm. Now the way the library works, any non-zero value means there's something in front of the sensor. Let's say the object was 100cm away, it would return a value of 100.

If instead, it would return the max distance when there wasn't an object at all in the way, how would you know there wasn't an object at all? Or if there was an object 100cm away? So there would be no difference between an object detected at 100cm and no object at all. That's not good logic, and it makes it impossible to know what the condition is.

Keep in mind that a result of 0 doesn't mean the object is further away than the max distance. It means there isn't an object AT ALL, which is why a value of 0 makes total sense.

If, however, for your use, you want this ambiguous logic, that's totally possible. Simply read the sensor distance, then if 0 make it any value you want. For example:

``````if (dist == 0) {
dist = 100;
}
``````

Or whatever you want it to be. Writing the library in this way, you can both detect when an object doesn't exist or just set the max distance (if that makes sense to your project).

Basically, this is where your programming needs to take over and deal with the results as your program needs.

Tim

Zero is much better because you can use a better sensor with longer range so the max distance is not always the same.

Hello, thank you very much for your contribution! I have a problem in my project. I have a car, Arduino + L293D shield. I have connected 4x ultrasonic sensors on pins: 3,4,5,6. I have put a delay of 60ms and 5s in setup.

However, the front sensor measures are like 12,12,12,0,12,12,0,12,12... etc a sensor from left measures 0,0,0,0,0,18,0,0,0,18. How could I make a consistent measurment? Some of the sensors are measuring good, some have a lot of zeros.

I need the car to avoid obstacles in its way. Thank you.

Aylee: Hello, thank you very much for your contribution! I have a problem in my project. I have a car, Arduino + L293D shield. I have connected 4x ultrasonic sensors on pins: 3,4,5,6. I have put a delay of 60ms and 5s in setup.

However, the front sensor measures are like 12,12,12,0,12,12,0,12,12... etc a sensor from left measures 0,0,0,0,0,18,0,0,0,18. How could I make a consistent measurment? Some of the sensors are measuring good, some have a lot of zeros.

I need the car to avoid obstacles in its way. Thank you.

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

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: https://youtu.be/I9UEWkUHS2U . 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: https://youtu.be/I9UEWkUHS2U . 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?