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

bombicri:
I tried to use this library with Makeblock MegaPi board.
When I try to include NewPing library in Arduino IDE I received errors from the attached image.
How to solve this problem?
Thank you.

Have you tried looking on the NewPing home page? There’s info about vector errors. Also, I don’t know what a Makeblock MegaPi board is. NewPing was designed for Arduino boards.

Tim

AndrewsBrewing:
Hello Tim,

When using a JSN-SR04T water proof sensor, its required PING_MEDIAN_DELAY is longer than the 29000 which specified in the #define statement. This causes the sensor to ill-behaved resulting in improper operation.

If this #define constant was converted to a pre-settable value through a user command, then it would accommodate proper operation with a broader range of sensors making the library even more flexible.

Of course if no value was set by the command the default of 29000 would be used.

Might you consider this change in your next update?

Sincerely,
The Beer Drinkers at Andrews Brewing Company
Andrews, North Carolina

PING_MEDIAN_DELAY is a config value so you can change it if needed.

Tim

I cannot get Newping (v1.9)to provide data with the sample sketches. The "distance = sonar.ping_cm(200);" command returns nothing on the serial monitor. No errors on compile. I tried both a Leonardo and a 101, same result. The HC-sr404 works fine without the library commands. I have followed the instructions on the web to the letter. Any tips? Thanks Alex

aliocha12: I cannot get Newping (v1.9)to provide data with the sample sketches. The "distance = sonar.ping_cm(200);" command returns nothing on the serial monitor. No errors on compile. I tried both a Leonardo and a 101, same result. The HC-sr404 works fine without the library commands. I have followed the instructions on the web to the letter. Any tips? Thanks Alex

Are you sure you matched the baud rate of the serial monitor and the baud rate set in the script? Both must match to display values in the serial monitor.

Tim

Hi there,

I'm having a problem with the 3 pin solution. I'm using the same sketch that you have in your bitbucket, and also the same code but the sensor doesn't work properly, it just prints 0 cm always. I prove the sensor with 4 pin connection and works fine. I don't understand why it doesn't work.

I'm using Arduino UNO rev 3 and a HC-SR04 sensor.

Thanks

Daniel

DanielG8: Hi there,

I'm having a problem with the 3 pin solution. I'm using the same sketch that you have in your bitbucket, and also the same code but the sensor doesn't work properly, it just prints 0 cm always. I prove the sensor with 4 pin connection and works fine. I don't understand why it doesn't work.

I'm using Arduino UNO rev 3 and a HC-SR04 sensor.

Thanks

Daniel

I believe you mean the 1 pin method (the other two pins are just power/ground which can be shared with multiple devices.

Anyway, the 1 pin method is a bit of a hack, and as stated, not all sensors or microcontrollers will work. As the HC-SR04 is made by 500 different companies in China for $0.50 a piece, many are different and some cut corners to make a slight profit.

Because of this, I would suggest trying to install the cap as instructed if you have problems. If that doesn't work, you probably just have a sensor that's not compatible, which I know exists as I have one as well.

Best of luck!

teckel: I believe you mean the 1 pin method (the other two pins are just power/ground which can be shared with multiple devices.

Anyway, the 1 pin method is a bit of a hack, and as stated, not all sensors or microcontrollers will work. As the HC-SR04 is made by 500 different companies in China for $0.50 a piece, many are different and some cut corners to make a slight profit.

Because of this, I would suggest trying to install the cap as instructed if you have problems. If that doesn't work, you probably just have a sensor that's not compatible, which I know exists as I have one as well.

Best of luck!

Yes, I meant the 1 pin method (I said the 3 pin method because I have to connect just three pins counting gnd and vcc). It seems that I'm a bit unlucky and my sensors cannot be hacked.

I'm doing a project with lots of connections and I would have like to be able to optimize some of them.

Thanks for your fast answer.

DanielG8: Yes, I meant the 1 pin method (I said the 3 pin method because I have to connect just three pins counting gnd and vcc). It seems that I'm a bit unlucky and my sensors cannot be hacked.

I'm doing a project with lots of connections and I would have like to be able to optimize some of them.

Thanks for your fast answer.

I'd try a cap across trigger and echo. That works for every HC-SR04 I have no matter the microcontroller.

Tim

Tim, Thanks for the excellent improvements to the ultrasonic rangefinder capabilities!
I recently got a couple US-100s to replace my HC-SR04s for use with Particle Photon.

  1. Will you put the latest New_Ping up on Particle.build.io? The one there is 1.8.0
  2. I want to use the US-100s digital means for communicating, instead of the standard analog pulse length your library does so well. This puts the computation onto the microprocessor of the US-100, including temperature compensation (important for my outdoor installation in an oil tank). Do you have any plans for adding the US-100 to the list of compatible ultrasonic transceivers?
  3. If you have used the US-100, how do you feel about its claims of mm accuracy?

Thanks again!
Steve Potter

stevempotter: Tim, Thanks for the excellent improvements to the ultrasonic rangefinder capabilities! I recently got a couple US-100s to replace my HC-SR04s for use with Particle Photon. 1. Will you put the latest New_Ping up on Particle.build.io? The one there is 1.8.0 2. I want to use the US-100s digital means for communicating, instead of the standard analog pulse length your library does so well. This puts the computation onto the microprocessor of the US-100, including temperature compensation (important for my outdoor installation in an oil tank). Do you have any plans for adding the US-100 to the list of compatible ultrasonic transceivers? 3. If you have used the US-100, how do you feel about its claims of mm accuracy?

Thanks again! Steve Potter

The US-100 is compatible with NewPing when using the trigger and echo pins. Serial communications with sensors is outside of the scope for NewPing. Temperature compensation isn't typically that important as the speed of sound changes so little for short distances. Basically, it's about within the range of accuracy for most measurements. You can also use a cheap i2c temp sensor if you want to use temperature to calculate ping distance.

If you want to use serial, I would suggest just communicating with the sensor directly. There's no need for a library as each sensor uses a different communication trigger and package anyway.

In any case, NewPing is designed for an easy trigger/echo protocol interface with ultrasonic sensors. Expanding it to serial would be a totally different library or not even need a library as it's much easier to communicate with serial-based sensors.

Tim

Hi there

I am trying to use the newping library with my Particle Photon and I am running into the following compile error when compiling NewPing.cpp:

in line 22:15 _triggerMode = (uint8_t *) portModeRegister(digitalPinToPort(trigger_pin)); // Get the port mode register for the trigger pin.

The error relates to assigning a unit8_t* to a volatile unint32_t*. I'm not sure if the error relates to possible misalignment, the volatile attribute or something else such as missing option selected in my code or a compiler option that needs changing.

I am using Particle dev on a Windows 10 machine to prepare my code which is compiled in the Particle.io cloud.

Any guidance or suggestions how to resolve this issue would be appreciated.

Update: I just realized that the NewPing library is available through the Particle built in libraries, except the version available that way is 1.8, versus the latest version (that I was using is 1.9). Also the line throwing the error is different in v1.8 and does not cast the device register as uint_8t*.

I have a way ahead now, using the v1.8 library. I'll see how that goes.

Tominthevan: Hi there

I am trying to use the newping library with my Particle Photon and I am running into the following compile error when compiling NewPing.cpp:

in line 22:15 _triggerMode = (uint8_t *) portModeRegister(digitalPinToPort(trigger_pin)); // Get the port mode register for the trigger pin.

The error relates to assigning a unit8_t* to a volatile unint32_t*. I'm not sure if the error relates to possible misalignment, the volatile attribute or something else such as missing option selected in my code or a compiler option that needs changing.

I am using Particle dev on a Windows 10 machine to prepare my code which is compiled in the Particle.io cloud.

Any guidance or suggestions how to resolve this issue would be appreciated.

Update: I just realized that the NewPing library is available through the Particle built in libraries, except the version available that way is 1.8, versus the latest version (that I was using is 1.9). Also the line throwing the error is different in v1.8 and does not cast the device register as uint_8t*.

I have a way ahead now, using the v1.8 library. I'll see how that goes.

Never expect Arduino libraries that deal with timers or bitmasks to work on a Photon. Photon is NOT 100% compatible with Arduino. You must use libraries designed for Photon (or in this case a modified version re-written for the Photon). If you purchased a Photon beliving it's 100% Arduino compatible, you were mislead. The Photon is MUCH different. They're both C++ and have the same basic commands, but are VERY different.

Tim

What are the hardware / software requirements for this library? I'm using an older mac laptop running OS X 10.6.8 (the newest OS this computer will run). I'm running Arduino 1.0.5 which I believe is the newest software version this computer will run. The development board I'm using is a Arduino Mega -- not that that matters in this case. -- not yet anyway.

That being said, when I try to import the NewPing library I get a slew of java errors in the Arduino Error Console. They start:

java.lang.NullPointerException at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:36) at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18) at java.lang.reflect.Field.get(Field.java:358) at apple.awt.CAccessibility.getAccessibleRoleFor(CAccessibility.java:200)

And continue on for many lines.

However the library appears to be installed. That is to say the folder appears in the third party libraries folder.

But, the library is not usable. When the compiler comes to the line instantiating a NewPing object, it throws an error that NewPing is not a type.

Is there an older version that might work for me? Or java upgrades? Any ideas would be appreciated.

rbailets: What are the hardware / software requirements for this library? I'm using an older mac laptop running OS X 10.6.8 (the newest OS this computer will run). I'm running Arduino 1.0.5 which I believe is the newest software version this computer will run. The development board I'm using is a Arduino Mega -- not that that matters in this case. -- not yet anyway.

That being said, when I try to import the NewPing library I get a slew of java errors in the Arduino Error Console. They start:

java.lang.NullPointerException at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:36) at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18) at java.lang.reflect.Field.get(Field.java:358) at apple.awt.CAccessibility.getAccessibleRoleFor(CAccessibility.java:200)

And continue on for many lines.

However the library appears to be installed. That is to say the folder appears in the third party libraries folder.

But, the library is not usable. When the compiler comes to the line instantiating a NewPing object, it throws an error that NewPing is not a type.

Is there an older version that might work for me? Or java upgrades? Any ideas would be appreciated.

It sounds like you're having more of a generic system problem rather than a problem with NewPing, which works just fine with Android 1.0.5. I don't do Mac, but I'd start with asking for help with getting Arduino working with your Mac. Sounds like you're having Java issues.

Ok I'll start looking into other possible problems. Not that I've used a lot of third party libraries, but this is the only one I've had any trouble with.

Thank you for your quick response,

Ralph

rbailets: Ok I'll start looking into other possible problems. Not that I've used a lot of third party libraries, but this is the only one I've had any trouble with.

Thank you for your quick response,

Ralph

There's nothing special about the NewPing library install. These are the instructions I link to from the official NewPing page:

https://www.arduino.cc/en/Guide/Libraries#toc4

It says it works with 1.0.5 and even shows Mac instructions. Give it a try.

Tim

Hi Tim,

i need some support in my current project. I would like to trigger and read the data of 64 HCSR04 Ultrasonic distance sensors. I want to build an array of 8x8 sensors with a distance of 6cm to a vibrating wall. Through the distance variations (Amplitude is big enough) I hope to get enough information to analyse the wall.

The echo of the sensors will not effect each other. I have a big distance between each sensor and a special foam behind the sensors which will hopefully absorb the signals. I read on your website that your sketch can handle 15 sensors on a 33ms intervall. When I change the Maximum_Distance even smaller intervalls should be possible, correct?

Do you know a possibility to ping as many sensors as possible at the same time? I know that it will never be possible to trigger 64 sensors at the same time, because even with Port Manipulation it is just possible to to activate 7 Pins at the same time. Do you have any suggestions or do you know if somebody has done something similar before?

Lars

Julio26127:
Hi Tim,

i need some support in my current project. I would like to trigger and read
the data of 64 HCSR04 Ultrasonic distance sensors. I want to build an array
of 8x8 sensors with a distance of 6cm to a vibrating wall.
Through the distance variations (Amplitude is big enough) I hope to
get enough information to analyse the wall.

The echo of the sensors will not effect each other. I have a big distance between
each sensor and a special foam behind the sensors which will hopefully absorb the
signals. I read on your website that your sketch can handle 15 sensors on a 33ms
intervall. When I change the Maximum_Distance even smaller intervalls should be possible, correct?

Do you know a possibility to ping as many sensors as possible at the same time?
I know that it will never be possible to trigger 64 sensors at the same time, because
even with Port Manipulation it is just possible to to activate 7 Pins at the same time.
Do you have any suggestions or do you know if somebody has done something similar before?

Lars

You could ping more frequently if there were no echos. But, cross-talk between sensors prohibits multiple sensors being triggered at the same time. As the sensors must SEND before they can receive, if you would ping two sensors at the same time the cross-talk would result in the echo being garbage yielding bad readings.

The only way to do it is to ping each sensor one at a time, or, look for a different technology. It doesn’t sound like ultrasonic is a good method for what you’re doing, and how much would this wall be vibrating anyway? It would need to be moving more than a CM for it to be detected, and only the center would move I would assume.

Tim

hi Tim,

I would like to use the timer methods for the NewPing library on an ATTiny841.

In the documentation you mention that the ATTinies do not have "a suitable timer". What do you mean by that ?

As far as I can see you are using a 8-bit timer on the atmega328, and there is an 8 bit timer on the ATTiny84 and ATTiny841 as well.

As I use the ATTiny841, which has two 16 bit timers and one 8 bit timer, I'm a bit confused why it would not be possible to use any of the 16 bit timers ?

kind regards,

Gert

GertSanders: hi Tim,

I would like to use the timer methods for the NewPing library on an ATTiny841.

In the documentation you mention that the ATTinies do not have "a suitable timer". What do you mean by that ?

As far as I can see you are using a 8-bit timer on the atmega328, and there is an 8 bit timer on the ATTiny84 and ATTiny841 as well.

As I use the ATTiny841, which has two 16 bit timers and one 8 bit timer, I'm a bit confused why it would not be possible to use any of the 16 bit timers ?

kind regards,

Gert

Maybe the ATTiny841 is newer than the last time I looked into this or maybe I didn't know about the ATTiny841 or maybe it doesn't have the proper timer addresses. Not sure, nor could I test this as I don't have this chip.

But, did you try it? Looking at the code (line 207 of NewPing.h) it seems that it's specifically looking for known ATtiny microcontrollers that don't have the needed timer addresses to work. The ATtiny841 isn't listed, so the timer would be enabled for the ATtiny841.

In other words... If at the time of writing all ATtiny processors didn't work, then the documentation would say as such. But, if at a later date something was released that did support timers in the needed way, and that microcontroller was not known to me, then the documentation wouldn't updated, as how could it if it's unknown.

Basically, you should try it out and report back if it works.

Tim