Go Down

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

Timbergetter

I am using a single JSN-SR04T-2.0 with the NewPing 1.9.0 library on a stand alone Atmega 328P.
For testing I am cycling through 10 pings with a 200 ms delay between each call.  If I use the simple ping() function I get good time results (all result values within 1% or so).  If I use the ping_cm() function the distance results can vary wildly.  (eg 125, 126, 64, 125, 63, 63 etc).  I get the same experience after swapping to another of these sensors and to another of these sensor interface cards.  Do you have any idea why I could be getting such different consistency results when comparing ping() and ping_cm()?

teckel

I am using a single JSN-SR04T-2.0 with the NewPing 1.9.0 library on a stand alone Atmega 328P.
For testing I am cycling through 10 pings with a 200 ms delay between each call.  If I use the simple ping() function I get good time results (all result values within 1% or so).  If I use the ping_cm() function the distance results can vary wildly.  (eg 125, 126, 64, 125, 63, 63 etc).  I get the same experience after swapping to another of these sensors and to another of these sensor interface cards.  Do you have any idea why I could be getting such different consistency results when comparing ping() and ping_cm()?
My guess is that you setup a variable which is rolling over. You can just do:
Code: [Select]

sonar.ping() / US_ROUNDTRIP_CM

which is the same as ping_cm().

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

Edwooong

Good day ! i'm currently making a project (distance measuring device) that can measure distance max range of 500cm by using HC-SR04.
Now what ive noticed measuring distance from 1 to 200 cm is accurate but when it reaches 300,400,500cm it will now have an error of 5-10cm. Im new to arduino programming and i think one problem is my code . thanks in advance.

This is the code im using:
 #include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 9 ;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

void setup() {
       Serial.begin(9600);     
       lcd.begin(16,2);
       pinMode(trigPin,OUTPUT);
       pinMode(echoPin,INPUT);     
}   
void loop() {
       digitalWrite(trigPin,LOW);
       delayMicroseconds(2);
       digitalWrite(trigPin,HIGH);
       delayMicroseconds(10);
       digitalWrite(trigPin,LOW); 
       duration = pulseIn(echoPin,HIGH);
       distanceCm = duration*0.034/2;
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Measuring.....");       
       lcd.setCursor(0,1);
       lcd.print("Distance:");   
       lcd.print(distanceCm);
       lcd.print(" Cm   ");
}
 

teckel

Good day ! i'm currently making a project (distance measuring device) that can measure distance max range of 500cm by using HC-SR04.
Now what ive noticed measuring distance from 1 to 200 cm is accurate but when it reaches 300,400,500cm it will now have an error of 5-10cm. Im new to arduino programming and i think one problem is my code . thanks in advance.

This is the code im using:
 #include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 9 ;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

void setup() {
       Serial.begin(9600);     
       lcd.begin(16,2);
       pinMode(trigPin,OUTPUT);
       pinMode(echoPin,INPUT);     
}   
void loop() {
       digitalWrite(trigPin,LOW);
       delayMicroseconds(2);
       digitalWrite(trigPin,HIGH);
       delayMicroseconds(10);
       digitalWrite(trigPin,LOW); 
       duration = pulseIn(echoPin,HIGH);
       distanceCm = duration*0.034/2;
       lcd.clear();
       lcd.setCursor(0,0);
       lcd.print("Measuring.....");       
       lcd.setCursor(0,1);
       lcd.print("Distance:");   
       lcd.print(distanceCm);
       lcd.print(" Cm   ");
}
 
Quite simple, the distance is wrong because you're not using the correct speed of sound for your temperature.   Also, you're not even using the NewPing library so your entire question is out of scope.  Use the NewPing library in your sketch and use the built-in speed of sound calculations and then if it's wrong I can give guidance.

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

Edwooong

Quite simple, the distance is wrong because you're not using the correct speed of sound for your temperature.   Also, you're not even using the NewPing library so your entire question is out of scope.  Use the NewPing library in your sketch and use the built-in speed of sound calculations and then if it's wrong I can give guidance.

Tim
Thank you Very much Sir Tim.

Here is my code.


Code: [Select]

#include <NewPing.h>
#include <LiquidCrystal.h>

unsigned long previousMillis = 0;       
const long interval = 500;           

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledLCD = 6;

#define TRIGGER_PIN  9
#define ECHO_PIN     10 

NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup() {
  lcd.begin(16,2);
  pinMode(ledLCD,OUTPUT);
   
}

void loop() {
   unsigned long currentMillis = millis();
  digitalWrite(ledLCD,HIGH);
 
  delay(35);                     
  unsigned int uS = sonar.ping();
 if (uS != NO_ECHO) {
 
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Ping: ");
    lcd.print(uS / US_ROUNDTRIP_CM);
    lcd.print(" cm ");
   }
 }
}

teckel

Thank you Very much Sir Tim.

Here is my code.


Code: [Select]

#include <NewPing.h>
#include <LiquidCrystal.h>

unsigned long previousMillis = 0;       
const long interval = 500;           

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledLCD = 6;

#define TRIGGER_PIN  9
#define ECHO_PIN     10 

NewPing sonar(TRIGGER_PIN, ECHO_PIN);

void setup() {
  lcd.begin(16,2);
  pinMode(ledLCD,OUTPUT);
   
}

void loop() {
   unsigned long currentMillis = millis();
  digitalWrite(ledLCD,HIGH);
 
  delay(35);                     
  unsigned int uS = sonar.ping();
 if (uS != NO_ECHO) {
 
    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Ping: ");
    lcd.print(uS / US_ROUNDTRIP_CM);
    lcd.print(" cm ");
   }
 }
}

Yup, that works!

US_ROUNDTRIP_CM is the default ESTIMATED speed of sound.  It works well virtually all the time.  But, the accuracy is off the further the distance or the more the temperature you're at is off from the default speed of sound built into the library.

Typically, "good enough" is all the more a project needs.  Or, it doesn't really matter the distance, just that it's more or less than before or whatever being measured.  If, however, you need more accurate measurements, you'll need to do some work on your own.  To do that, you'll need to know the speed of sound at your altitude, humidity and temperature.  So, you'll take those measurements (maybe with different sensors, maybe hard-coded) do the math, and replace US_ROUNDTRIP_CM for whatever it is in your environment.

US_ROUNDTRIP_CM is exactly what it says, it's the number of uS it takes to sound to travel 1 cm round trip (in other words, 2 cm [1 cm there, 1 cm back]).  NewPing defaults to an integer for the US_ROUNDTRIP_CM because "good enough" is all the more 99.999% of projects need.  Using an integer is also great because it uses less memory and executes faster.

But by all means, use whatever number you want for the speed of sound that makes you feel good about the distance measurements.

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

Edwooong

Yup, that works!

US_ROUNDTRIP_CM is the default ESTIMATED speed of sound.  It works well virtually all the time.  But, the accuracy is off the further the distance or the more the temperature you're at is off from the default speed of sound built into the library.

Typically, "good enough" is all the more a project needs.  Or, it doesn't really matter the distance, just that it's more or less than before or whatever being measured.  If, however, you need more accurate measurements, you'll need to do some work on your own.  To do that, you'll need to know the speed of sound at your altitude, humidity and temperature.  So, you'll take those measurements (maybe with different sensors, maybe hard-coded) do the math, and replace US_ROUNDTRIP_CM for whatever it is in your environment.

US_ROUNDTRIP_CM is exactly what it says, it's the number of uS it takes to sound to travel 1 cm round trip (in other words, 2 cm [1 cm there, 1 cm back]).  NewPing defaults to an integer for the US_ROUNDTRIP_CM because "good enough" is all the more 99.999% of projects need.  Using an integer is also great because it uses less memory and executes faster.

But by all means, use whatever number you want for the speed of sound that makes you feel good about the distance measurements.

Tim
This will help me a Lot.
Thank You So much Sir Tim :)

C_J_K

Hey Tim, Great work!

I am currently working on my bachelor thesis and I do have a question about the HC-SR04 Sensor. My thesis is about autonomous driving Robots. They have to navigate in a room and avoid each other. I am planning to use the HC-SR04 Sensor for optical avoidance. The problem that I am facing is, that if there are many Robots in one room, the ultrasonic sounds from different Robots are interfering with each other, so that on Sensor of let's say Robot_1 detects the Ultrasonic sound from the Sensor of Robot_2. My question is now if it is possible to physically limit the Range of the Ultrasonic sensor? I mean not to wait for a shorter amount of time for the signal to come back. I would like to limit the distance that ultrasonic waves travel through the room. Maybe by changing the 40 kHz Burst-Signal to a higher frequency or changing the voltage of the sensor or simply by putting tape over the sensor. Have you ever tried something like that?
Thanks a lot in advance.

English is not my first language so please excuse any mistakes.

teckel

Hey Tim, Great work!

I am currently working on my bachelor thesis and I do have a question about the HC-SR04 Sensor. My thesis is about autonomous driving Robots. They have to navigate in a room and avoid each other. I am planning to use the HC-SR04 Sensor for optical avoidance. The problem that I am facing is, that if there are many Robots in one room, the ultrasonic sounds from different Robots are interfering with each other, so that on Sensor of let's say Robot_1 detects the Ultrasonic sound from the Sensor of Robot_2. My question is now if it is possible to physically limit the Range of the Ultrasonic sensor? I mean not to wait for a shorter amount of time for the signal to come back. I would like to limit the distance that ultrasonic waves travel through the room. Maybe by changing the 40 kHz Burst-Signal to a higher frequency or changing the voltage of the sensor or simply by putting tape over the sensor. Have you ever tried something like that?
Thanks a lot in advance.

English is not my first language so please excuse any mistakes.
The sensors can't really be changed to a different frequency.  It would require different hardware on the sensors and different software as well.  You'll need to consider other options if you're going to have multiple individual systems all trying to work in the same environment.

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

C_J_K

Thanks a lot for your advice. Do you know if there are any "near field" ultrasonic sensors for Arduino on the marked ore I guess I just have to go with a other kind of sensor like an Infrared sensor.
Thank you

egarvie

Hello! I am currently trying to use the NewPing.h file, but I keep receiving the error "cannot open source file WProgram.h" but I am unable to actually find this file anywhere in your links. Is this necessary to be included or could I just take it out? Thank you!

teckel

Hello! I am currently trying to use the NewPing.h file, but I keep receiving the error "cannot open source file WProgram.h" but I am unable to actually find this file anywhere in your links. Is this necessary to be included or could I just take it out? Thank you!
That's a core Arduino file, nothing to do with NewPing.  The fact that this can't be found sounds like you don't have Arduino installed correctly or are using non-standard hardware in an improper way.

Basically, you should ask for general help, not help with NewPing.

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

onire

Hi Tim,
Is it possible to have -1 when it's out of range?
Thank you

KSWang

Thank you very much.
Your library is awesome.
However, I want to use with five ultrasonic sensors and one hall sensor.
It is embarrassed if there are any delay time in loop.
Is it mean it will be few delay time/ or no delay if I use the similar way like <Two sensor example> with calling NewPing five times?

Code: [Select]

#include <NewPing.h>

NewPing sonar1(11, 12, 200); // Sensor 1: trigger pin, echo pin, maximum distance in cm
NewPing sonar2(9, 10, 200); // Sensor 2: same stuff

#define pingSpeed 100 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
unsigned long pingTimer1, pingTimer2;

void setup() {
 // Do other stuff here
 pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 100ms (pingSpeed)
 pingTimer2 = pingTimer1 + (pingSpeed / 2); // Sensor 2 fires 50ms later
}

void loop() {
 if (millis() >= pingTimer1) {
   pingTimer1 += pingSpeed; // Make sensor 1 fire again 100ms later (pingSpeed)
   int in1 = sonar1.ping_in();
 }
 if (millis() >= pingTimer2) {
   pingTimer2 = pingTimer1 + (pingSpeed / 2); // Make sensor 2 fire again 50ms after sensor 1 fires
   int in2 = sonar2.ping_in();
   // Both sensors pinged, process results here
 }
 // Do other stuff here, notice how there's no delays in this sketch, so you have processing cycles to do other things :)
}



teckel

Hi Tim,
Is it possible to have -1 when it's out of range?
Thank you
That's exactly what it does by default.

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