Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7 (Read 629329 times) previous topic - next topic

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

teckel

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
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

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.

meltDown

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....


teckel

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
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

colbee89

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

Code: [Select]

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

Quote
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

Code: [Select]

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
Quote
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


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?

teckel

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
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

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

teckel

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.
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

Jcbc

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!

Code: [Select]
// ---------------------------------------------------------------------------
// 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)
}


fafidio


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

teckel

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
It's not designed to work like that. The ping_median() method is designed to take several samples over a short period of time and return the median time. You would need to write your own code to do what you're looking to do as it's non-typical.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

fafidio

thanks for reply.

could you suggest an example to do it?

thanks Fabio

teckel

thanks for reply.

could you suggest an example to do it?

thanks Fabio
The example sketch does a ping every about 30ms, just change that to 1000ms for once a second.  Then, do whatever averaging you want to do on those pings.  There's a million ways of averaging numbers, and it has nothing to do with NewPing.

You could (for example) take the previous ping result, multiply it by 9, add the new ping result, and divide the sum by 10.  That would average out the pings over the last 10 seconds.

But, there's many ways to average results.  It all depends on what kind of average you're looking for.  Mean, median, mode, etc.  Running average, average over a certain amount of time, etc.

There's no way to write it for you, as it depends on what you want.  But, you can expand your search outside a sonar distance sensor as you're now just working with number results, and any C language information will work helping guild you to write the averaging code you're looking for.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

Go Up