Go Down

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

scytayl

Hi Tim,

Apologies if this has already been asked. I am trying to use NewPing with the dfrobot waterproof sensor (link below), but I can't get the demo examples to report anything other than zero. My board is an Arduino Mega 2560. The example listed on the wiki page below does work. I've tried both the IDE (1.8.5) and the web editor.

Should this be possible? Many of the sites selling this product link to NewPing so I assumed so.

https://www.dfrobot.com/wiki/index.php/Weather_-_proof_Ultrasonic_Sensor_with_Separate_Probe_SKU_:_SEN0208

Edit: It seems I may have the JSN-SR04T-2.0, which is not compatible with the previous version, or likely with NewPing.

Thanks,
Craig

Weissglut

Hello everybody.

First of all, I would like to say a few words about my project: For a long time, I have been using a level indicator for my water reservoir with an Arduino Mega and a DYP-ME007Y waterproof ultrasonic module.
For various reasons, I would like to switch to an ESP8266 (IoT, OTA, etc.) and then the problems begin. The old DYP-ME007Y module has a serial interface. But I need the one serial interface with the ESP8266 for the upload of the sketch. That's why I switched to an AJ-SR04M or JSN-SR04T module (trigger and echo as interface).
To increase the accuracy I would like to use the NewPing Library (median value and iterations etc).
Now I have 2 problems:
1.: If I do not use the NewPing-Lib for distance measurement (trigger pulse and evaluation by "pulseIn (echo, HIGH)"), then I get from a distance of about 3m completely wrong results. I also read somewhere that the ESP8266 has problems with "pulseIn" ...
2nd: That's why I try to use the NewPing-Lib, because here on "pulseIn" is omitted. But when I use the NewPing lib, I get much worse or no results at all. Only about every twentieth value is displayed correctly, since only very rarely a trigger pulse is sent out.

I've spent many weeks for troubleshooting, but can not find any mistake. Does anyone have any idea why it does not work or where to find the mistake? Did someone already have similar problems? I am grateful for any help or tip!

Best regards
Martin

teckel

Hi Tim,

Apologies if this has already been asked. I am trying to use NewPing with the dfrobot waterproof sensor (link below), but I can't get the demo examples to report anything other than zero. My board is an Arduino Mega 2560. The example listed on the wiki page below does work. I've tried both the IDE (1.8.5) and the web editor.

Should this be possible? Many of the sites selling this product link to NewPing so I assumed so.

https://www.dfrobot.com/wiki/index.php/Weather_-_proof_Ultrasonic_Sensor_with_Separate_Probe_SKU_:_SEN0208

Edit: It seems I may have the JSN-SR04T-2.0, which is not compatible with the previous version, or likely with NewPing.

Thanks,
Craig
If the JSN-SR04T-2.0 operates with a trigger and echo, it should work.  If the sketch on the link works, so should NewPing, as they're doing the same basic thing to initiate a ping and listen for a result.

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

teckel

Hello everybody.

First of all, I would like to say a few words about my project: For a long time, I have been using a level indicator for my water reservoir with an Arduino Mega and a DYP-ME007Y waterproof ultrasonic module.
For various reasons, I would like to switch to an ESP8266 (IoT, OTA, etc.) and then the problems begin. The old DYP-ME007Y module has a serial interface. But I need the one serial interface with the ESP8266 for the upload of the sketch. That's why I switched to an AJ-SR04M or JSN-SR04T module (trigger and echo as interface).
To increase the accuracy I would like to use the NewPing Library (median value and iterations etc).
Now I have 2 problems:
1.: If I do not use the NewPing-Lib for distance measurement (trigger pulse and evaluation by "pulseIn (echo, HIGH)"), then I get from a distance of about 3m completely wrong results. I also read somewhere that the ESP8266 has problems with "pulseIn" ...
2nd: That's why I try to use the NewPing-Lib, because here on "pulseIn" is omitted. But when I use the NewPing lib, I get much worse or no results at all. Only about every twentieth value is displayed correctly, since only very rarely a trigger pulse is sent out.

I've spent many weeks for troubleshooting, but can not find any mistake. Does anyone have any idea why it does not work or where to find the mistake? Did someone already have similar problems? I am grateful for any help or tip!

Best regards
Martin
Are you using version 1.9.0?  Also to keep it simple, are you using the example simple NewPing sketch?  You could also try setting ONE_PIN_ENABLED to false

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

Weissglut

Yes of course. I am useing the 1.9.0-Version. And yes I keep the sketch as simple as possible (like the NewPing Example). So nothing else could disturb the measurement.
What do you mean by "try setting ONE_PIN_ENABLED to false" ?

BR Martin  

Edit: Okay. I know what you are meaning with "try setting ONE_PIN_ENABLED to false". I set this parameter at the NewPing.h to false, but it did not solve the problem. Every time I reset the ESP8266 I am getting 2 "good" results. After the first 2 results, most of the time I get "0" at the output-monitor. And only a very few times, a trigger was sending...


teckel

Yes of course. I am useing the 1.9.0-Version. And yes I keep the sketch as simple as possible (like the NewPing Example). So nothing else could disturb the measurement.
What do you mean by "try setting ONE_PIN_ENABLED to false" ?

BR Martin 

Edit: Okay. I know what you are meaning with "try setting ONE_PIN_ENABLED to false". I set this parameter at the NewPing.h to false, but it did not solve the problem. Every time I reset the ESP8266 I am getting 2 "good" results. After the first 2 results, most of the time I get "0" at the output-monitor. And only a very few times, a trigger was sending...


First, try changing the delay between pings to 1000 ms.  It could be that that sensor can't ping as frequently.

Secondly, you could try changing the MAX_SENSOR_DELAY value in NewPing.h from 5800 to maybe 40000.  That value is how long to wait for a previous ping to finish.  5800 works for every sensor I have, but it's possible that some sensors need a longer timeout.

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

Weissglut

Hi Tim,

now I am back from work to test your tips.
I've changed the delay between the pings many times before. This had no effect.
I've changed the "MAX_SENSOR_DELAY" also, but no effect :-(
That's why I took a look at the trigger signal with the scope. With the NewPing-Lib and the simple sketch I see most of the time a constant high level at the trigger-pin!
If I use the method "trigger pulse and evaluation by "pulseIn (echo, HIGH)" without NewPing, I get the trigger and echo impulses that I expect. Any ideas what could be wrong?

Regards
Martin

teckel

Hi Tim,

now I am back from work to test your tips.
I've changed the delay between the pings many times before. This had no effect.
I've changed the "MAX_SENSOR_DELAY" also, but no effect :-(
That's why I took a look at the trigger signal with the scope. With the NewPing-Lib and the simple sketch I see most of the time a constant high level at the trigger-pin!
If I use the method "trigger pulse and evaluation by "pulseIn (echo, HIGH)" without NewPing, I get the trigger and echo impulses that I expect. Any ideas what could be wrong?

Regards
Martin
Are you using an ATmega microcontroller or something else?

The only time that NewPing sets the trigger pin to high is for 10 microseconds to initiate the sensor.  If the ONE_PIN_ENABLED value is set to true, it then sets the trigger pin to input mode, which is why I suggested setting this to false if you're using two pins anyway.

The last thing I'd try is adding the following two lines right before the "class NewPing {" line in NewPing.h:

Code: [Select]

#undef DO_BITWISE
#define DO_BITWISE false


This will force it to use Arduino commands instead of bitwise logic on the pins.  This would especially be required if not using an AVR ATmega microcontroller.

If this doesn't work, there's probably no way for me to help without having your exact hardware to duplicate it.

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

DTC57

Does NewPing work with an Espressif ESP32 (using Arduino IDE)?

Thanks for any hints

David

teckel

Does NewPing work with an Espressif ESP32 (using Arduino IDE)?

Thanks for any hints

David
Should.  Would take 0.1 seconds to at least see if it compiled.

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

DTC57

Thanks - I got it working. I wasn't using suitable IO pins the first time around.

Many thanks, I'm still a beginner...

Weissglut

Are you using an ATmega microcontroller or something else?

The only time that NewPing sets the trigger pin to high is for 10 microseconds to initiate the sensor.  If the ONE_PIN_ENABLED value is set to true, it then sets the trigger pin to input mode, which is why I suggested setting this to false if you're using two pins anyway.

The last thing I'd try is adding the following two lines right before the "class NewPing {" line in NewPing.h:

Code: [Select]

#undef DO_BITWISE
#define DO_BITWISE false


This will force it to use Arduino commands instead of bitwise logic on the pins.  This would especially be required if not using an AVR ATmega microcontroller.

If this doesn't work, there's probably no way for me to help without having your exact hardware to duplicate it.

Tim
Hi Tim.

Yesterday I received new hardware (JSN-SR04T-2.0) and now it works. I just had to set the parameter "ONE_PIN_ENABLED to false" ...
Nevertheless, many thanks for your help.

However, I have another question: I currently calculate the distance in the following way:
    duration = sonar.ping ();
    distance = ((duration / 2) * 0.03435);
Result e.g. 233,34cm

If I measure the distance in the following way:
    float distance = (sonar.ping_cm ());
then I get another result, e.g. 238cm at the same measuring distance.
Yes, in the first variant, the temperature and the humidity affect the result, but the result of the second variant is closer to the true measuring distance (237 cm). Why?

Can you explain me how the distance is calculated with "(sonar.ping_cm ())"?

Best regards
Martin

teckel

However, I have another question: I currently calculate the distance in the following way:
    duration = sonar.ping ();
    distance = ((duration / 2) * 0.03435);
Result e.g. 233,34cm

If I measure the distance in the following way:
    float distance = (sonar.ping_cm ());
then I get another result, e.g. 238cm at the same measuring distance.
Yes, in the first variant, the temperature and the humidity affect the result, but the result of the second variant is closer to the true measuring distance (237 cm). Why?

Can you explain me how the distance is calculated with "(sonar.ping_cm ())"?

Best regards
Martin
If you can believe it, there's actually a debate on what the speed of sound is.  Resources will give different values and factor things in like altitude when that doesn't change the speed of sound.

Also, doing floating point math on your calculation increases the size of your code by about 1kb and slows down your script.  It's also not typically very important that the distance is exact, it's more about measuring the change in distance.  Finally, these ultrasonic sensors are not very accurate to begin with.

Due to the above, I've greatly simplified things with the cm calculation.  It just divides by 57.  This is actually quite close to the correct number for common indoor temps, it's an integer so it keeps the compiled code size small, and it's much faster than using floating point math.

In the NewPing.h file, the
Code: [Select]
#define US_ROUNDTRIP_CM 57 sets the conversion to cm.  You could change this to a floating point number, or anything you wanted, but I'd suggest just leaving it as 57 and using the ping_cm() method as I've found it to be very accurate in normal conditions, with the added benefit of being faster and smaller.

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

DTC57

Hi

I have an SR-05 connected to an ESP32 DevKit C and am using the Arduino IDE to drive things.

It all worked fine for a while: I could reliably measure distances in cm with no problems using the following code snippet:

Code: [Select]
   unsigned int uS = sonar.ping_median(10);
   sensors.distance = sonar.convert_cm(uS);

Now all of a sudden I only seem to get random results. Mostly zero and then large numbers. I don't think I did anything I should not have. Could it be that I have frizzed the sensor in some way? What could I look for? The placement of the hardware did not change either (as far as I can tell).

Any hints welcome before I go and buy another sensor!

Thanks, David

teckel

Hi

I have an SR-05 connected to an ESP32 DevKit C and am using the Arduino IDE to drive things.

It all worked fine for a while: I could reliably measure distances in cm with no problems using the following code snippet:

Code: [Select]
   unsigned int uS = sonar.ping_median(10);
   sensors.distance = sonar.convert_cm(uS);

Now all of a sudden I only seem to get random results. Mostly zero and then large numbers. I don't think I did anything I should not have. Could it be that I have frizzed the sensor in some way? What could I look for? The placement of the hardware did not change either (as far as I can tell).

Any hints welcome before I go and buy another sensor!

Thanks, David

If you didn't change anything and it stopped working, it points to a hardware problem.

To verify, use the test sketch and just the sensor and if that doesn't work (and it did previously), it must be a dead sensor.

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