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

Neonz:
Hello, I have a big problem and I need help. I'm trying to use the Ultrasonic Sensor HC-SR04 to make a project, I have this sensor connected to the arduino via USB and the problem is that i get multiple values of 0cm. For example i mesure 3cm, 4cm, 4cm, 5cm, 0cm,0cm,0cm,0cm, 6cm, 0cm,0cm,0cm, 8cm. I tried with the Newping library and with another library called Ultrasonic but it was the same. Something awkward is that when i use the Ultrasonic Library with the example code, it works great. But when i upload my example the problem starts.

I can't really help with someone else's library, but I do see why it's not working for you. Use the following script with my NewPing library and then if there's still a problem I can diagnose it for you:

#include <NewPing.h>

#define alarmPin 7
#define alarm2Pin 6

#define TRIGGER_PIN   9  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN      8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600);
  pinMode(alarmPin, OUTPUT);
  pinMode(alarm2Pin, OUTPUT);
}

void loop() {
  unsigned int cm = sonar.ping_cm(); // Send ping, get ping distance in cm.
  Serial.print(cm);
  Serial.println(" cm");
  if (cm >= 10) { 
    digitalWrite(alarmPin, HIGH); 
  } else {
    digitalWrite(alarmPin, LOW); 
  }
  if (cm < 10 && cm > 5)  { 
    digitalWrite(alarm2Pin, HIGH); 
  } else {
    digitalWrite(alarm2Pin, LOW); 
  }
  delay(100);
}

That script should be a drop-in replacement using NewPing for your defective script. Let me know what happens.

Tim

Unfortunately for me, I am using the tone library and wanted to use the NewPing library too.
Do you know any way around the conflict?
It can be also a new tone library (or a modified one) or another way of playing a tone without the usage of the Timer2

TIA,
basile

basile-laderchi:
Unfortunately for me, I am using the tone library and wanted to use the NewPing library too.
Do you know any way around the conflict?
It can be also a new tone library (or a modified one) or another way of playing a tone without the usage of the Timer2

TIA,
basile

NewPing only uses a timer interrupt when using the event-driven ping method "ping_timer". The standard "ping" method as in the example sketch that starts this thread doesn't use any timer interrupts at all.

Do you need to use the event-driven ping method? Or, do you somehow believe that NewPing always uses a timer interrupt? If you believe NewPing always uses a timer interrupt, maybe you could explain why you believed this so I can make this more clear to other users.

Tim

This is part of the sketch I am using:

#include <NewPing.h>

NewPing sonar = NewPing(A4, A4, 300);

void setup() {
}

void loop() {
  tone(12, 2999, 3000);
}

When I try to verify the sketch I always get this error:

core.a(Tone.cpp.o): In function `__vector_7':
C:\...\arduino-1.0\hardware\arduino\cores\arduino/Tone.cpp:523: multiple definition of `__vector_7'
NewPing\NewPing.cpp.o:C:\...\Documents\Arduino\libraries\NewPing/NewPing.cpp:214: first defined here

Even when I comment out or delete the 3rd line ("NewPing sonar = ...") I still get the same error.

basile

P.S.: I am using a Parallax PING))) sensor on an Arduino UNO board and I think (I am not sure because the test has been performed by a friend) that when it is connected to A4 (as in the example) it doesn't work. Unfortunately right now there are no digital ports available.

This is part of the sketch I am using:

This is part of an answer I am giving: tone.

AWOL:

basile-laderchi:
This is part of the sketch I am using:

This is part of an answer I am giving: tone.

That's exactly why I replaced the tone function with this function:

void toneWorkaround(byte tonePin, int frequency, int duration) {
  int period = 1000000L / frequency;
  int pulse = period / 2;
  for (long i = 0; i < duration * 1000L; i += period) {
    digitalWrite(tonePin, HIGH);
    delayMicroseconds(pulse);
    digitalWrite(tonePin, LOW);
    delayMicroseconds(pulse);
  }
}

Even after this modification to my code though the Parallax PING))) refuses to work when using the NewPing library.

Right now the Parallax PING))) is working and returning correct values with this code:

long ping(byte pingPin) {
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  long duration = pulseIn(pingPin, HIGH);
  return duration;
}

basile

P.S.: robot's full code as is right now: Wheeled-Robot Arduino Code - Pastebin.com

basile-laderchi:
This is part of the sketch I am using:

#include <NewPing.h>

NewPing sonar = NewPing(A4, A4, 300);

void setup() {
}

void loop() {
  tone(12, 2999, 3000);
}




When I try to verify the sketch I always get this error:


~~~
core.a(Tone.cpp.o): In function `__vector_7':

C:...\arduino-1.0\hardware\arduino\cores\arduino/Tone.cpp:523: multiple definition of `__vector_7'
NewPing\NewPing.cpp.o:C:...\Documents\Arduino\libraries\NewPing/NewPing.cpp:214: first defined here




Even when I comment out or delete the 3rd line ("NewPing sonar = ...") I still get the same error.

basile

P.S.: I am using a Parallax PING))) sensor on an Arduino UNO board and I think (I am not sure because the test has been performed by a friend) that when it is connected to A4 (as in the example) it doesn't work. Unfortunately right now there are no digital ports available.

Just comment out line 214, 216, and 217 in NewPing.cpp if you're not using the interrupt method of NewPing. Commenting out the "NewPing sonar=" line won't help as the library files are loaded via the #include <NewPing.h>

Not sure what can be done to fix this without manually commenting out the lines. If anyone has any suggestions I'd appreciate it. At least the solution is very straight-forward and simple.

Tim

For this line in the example:
Serial.print(uS / US_ROUNDTRIP_IN); // Convert ping time to distance in cm and print result (0 = outside set distance range)

Is there a way to have "outside distance range" read the MAX_DISTANCE value instead of 0?

caseyd:
For this line in the example:
Serial.print(uS / US_ROUNDTRIP_IN); // Convert ping time to distance in cm and print result (0 = outside set distance range)

Is there a way to have "outside distance range" read the MAX_DISTANCE value instead of 0?

First, that would probably be a bad idea because a value of MAX_DISTANCE would indicate there was something there. In other words, you wouldn't know if there was actually something exactly at 200cm away or if there was no ping echo if MAX_DISTANCE was set to 200.

With that said, you can easily do what you want. See the following code snippet:

  long uS = sonar.ping();
  if (uS == NO_ECHO) uS = MAX_DISTANCE;
  Serial.print(uS);

Tim

Hi Tim,

I want to use 12 sensors, which is obviously achievable with your code, I have got 6 working really well so far so thank you.... however there are only 12 pins on the arduino uno, so I can only use 6 sensors (obviously 2pins per sensor). I see you can buy a Mux shield to add more inputs. Would this shield work with your code? (I'm very new to code). Would I need to change the code much to make it work? or is there a better / easier option?

Thanks again

@ bassmagnetic

Some time in the earlier pages, Tim made it possible to run each sensor from one digital pin, +5, and GND. So, the result is that 12 sensors require 12 digital pins.

Check the examples with his library.

EDIT: his discovery of the one pin method was reported on page 7 of this thread. I am not sure what page the addition was made to the examples.

bassmagnetic:
Hi Tim,

I want to use 12 sensors, which is obviously achievable with your code, I have got 6 working really well so far so thank you.... however there are only 12 pins on the arduino uno, so I can only use 6 sensors (obviously 2pins per sensor). I see you can buy a Mux shield to add more inputs. Would this shield work with your code? (I'm very new to code). Would I need to change the code much to make it work? or is there a better / easier option?

Thanks again

cyclegadget is quite right, I've written the NewPing library to work using only one one pin for both trigger and echo. So, with even the Uno you can connect 12 ping sensors using the NewPing library. Basically, the library sets the pin to output, initiates the ping, quickly switches the same pin to input, and waits for the ping echo. The syntax is just to specify the same pin for both trigger and echo. Below is an example sketch as well as a schematic of how you connect the sensor. You simply tie the pin to trigger and then jumper trigger to echo.

http://code.google.com/p/arduino-new-ping/wiki/NewPing_Single_Pin_Sketch

Before you rip your project apart and use the one pin method I would suggest doing just one sensor first. The reason is that for some sensors this one pin technique doesn't work. If you're using the HC-SR04, you should be good to go. Others may need a little capacitor instead of the jumper. I believe I docucmented in the release notes in NewPing.h exactly which sensors worked without the cap and those that needed the cap. Check the release notes in NewPing.h for details.

Also, I'm always interested to see what multi ping sensor projects people are using with NewPing. Once you get things working, post a pic.

Tim

Brilliant, thanks Tim, just tried that and it works fine with 1 sensor, I am using HC-SR04's.... much better than buying more bits!!

Now I just need to figure out how to combine this code with the NewPing15sensor code to make it work with more sensors, any ideas, is it easy to do?

I am needing to increase the number of sensors as I want to make sure that proximately to the speakers does not trigger sounds (so no one goes deaf!)

I am making an installation as part of a final piece for an MA, i'd be happy to send details and photos when its all up and running...

thanks again

andre

bassmagnetic:
Brilliant, thanks Tim, just tried that and it works fine with 1 sensor, I am using HC-SR04's.... much better than buying more bits!!

Now I just need to figure out how to combine this code with the NewPing15sensor code to make it work with more sensors, any ideas, is it easy to do?

I am needing to increase the number of sensors as I want to make sure that proximately to the speakers does not trigger sounds (so no one goes deaf!)

I am making an installation as part of a final piece for an MA, i'd be happy to send details and photos when its all up and running...

thanks again

andre

Just take the 15 sensor sketch and for each sensor use the same trigger and echo pin. For example, the first part of your sketch would look something like this:

#include <NewPing.h>

#define SONAR_NUM     12 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(2, 2, MAX_DISTANCE), // Using same pin for trigger and echo.
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(4, 4, MAX_DISTANCE),
  NewPing(5, 5, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE),
  NewPing(7, 7, MAX_DISTANCE),
  NewPing(8, 8, MAX_DISTANCE),
  NewPing(9, 9, MAX_DISTANCE),
  NewPing(10, 10, MAX_DISTANCE),
  NewPing(11, 11, MAX_DISTANCE),
  NewPing(12, 12, MAX_DISTANCE),
  NewPing(13, 13, MAX_DISTANCE)
};

Tim

After the substitution of the tone function with the toneWorkaround one and the change of the power supply of the PING))) (attached to A4) to getting it's power from the Arduino everything is working juuuust fine!!!
I am now using the ping_median for the pings just in case there is some interference.

Thanks again for the great library.

basile

basile-laderchi:
After the substitution of the tone function with the toneWorkaround one and the change of the power supply of the PING))) (attached to A4) to getting it's power from the Arduino everything is working juuuust fine!!!
I am now using the ping_median for the pings just in case there is some interference.

Thanks again for the great library.

basile

Another solution would be to comment out line 214, 216, and 217 in NewPing.cpp if you're not using the interrupt method of NewPing. You can then use the tone library without conflict. In my development version, I've created a switch to do this if you're not using the timer interrupt method of NewPing.

Tim

I'm getting close to releasing a new version of the NewPing library. Is anyone having any problems with it or want to suggest something new to add? It's getting quite mature now so the updates probably won't happen as frequently.

I'm going to test it with the new 32-bit ARM Teensy 3.0 that I got last week. If someone has the Arduino Due and would like to test NewPing with it, that would be appreciated. Note, that the timer interrupt methods probably won't work with the new crop of 32-bit Arduinos. So, all I really care about testing and supporting at this time is the standard ping() method.

Tim

I've downloaded the v1.5 and tried it in arduino 1.0 but it wont compile. im using a cloned arduino board with atmega8. does it support older boards?

http://www.elab.ph/forum/index.php?topic=6698.0

this is the board im using.

jackryan:
I've downloaded the v1.5 and tried it in arduino 1.0 but it wont compile. im using a cloned arduino board with atmega8. does it support older boards?

http://www.elab.ph/forum/index.php?topic=6698.0

this is the board im using.

I know the timer interrupt methods won't work correctly with the ATmega8 due to the clock rate. Never had anyone try a ATmega8 to know if it worked or not. Be sure to start with the basics and try the sample sketch (using the standard ping() method). If you're still getting compile errors, including them with your reply would be helpful to detect if it's a simple thing or something more complex. As it stands, without the errors I can't even guess as v1.5 works just fine with Arduino 0023, 1.0, 1.0.1, and 1.0.2.

Tim

First, thanks for the lib! I love it.
Second I will apologize in advance if this has already been asked, but I can't seem to search just this topic so I may have missed it, if it was.

Has any one else experienced an error when attempting to use tone() along with this lib? When I attempt to include tone [e.g. tone(11,1000,20)] I get an error:
core.a(Tone.cpp.o): In function __vector_7':* *C:\Users\Dave\arduino-1.0.1-windows\arduino-1.0.1\hardware\arduino\cores\arduino/Tone.cpp:523: multiple definition of __vector_7'
NewPing\NewPing.cpp.o:C:\Users\Dave\arduino-1.0.1-windows\arduino-1.0.1\libraries\NewPing/NewPing.cpp:214: first defined here