Go Down

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

Murat_Artan

Sir, I am starting to use HC-SR04 ultrasonic sensor with Arduino DUE. May I ask which library I should use with NewPing Library. And could you tell important details it may help me to be succeed. I am a begginer.

teckel

Sir, I am starting to use HC-SR04 ultrasonic sensor with Arduino DUE. May I ask which library I should use with NewPing Library. And could you tell important details it may help me to be succeed. I am a begginer.
I assume you mean what methods to use with the NewPing Library?  ping() ping_cm() ping_in() ping_median() will all work with the Due.  The timer methods won't work as they require the ATmega chip.

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

Murat_Artan

I was going to work with  ping() ping_cm() ping_in() ping_median().  Thank you

swilkens

Hello and happy new year!

Quick question regarding platform support; does this library provide support for the newer range of boards? i.e. the Nano Every - which is to replace the nano. It's an ATMega4809 chip.

Based on the output, it looks like this board misses some timers.

*edit* - Looking at the official site (which I couldn't find before posting, apologies) it seems this hardware is indeed unsupported.

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

teckel

Hello and happy new year!

Quick question regarding platform support; does this library provide support for the newer range of boards? i.e. the Nano Every - which is to replace the nano. It's an ATMega4809 chip.

Based on the output, it looks like this board misses some timers.

*edit* - Looking at the official site (which I couldn't find before posting, apologies) it seems this hardware is indeed unsupported.

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
Hello and happy new year!

Quick question regarding platform support; does this library provide support for the newer range of boards? i.e. the Nano Every - which is to replace the nano. It's an ATMega4809 chip.

Based on the output, it looks like this board misses some timers.

*edit* - Looking at the official site (which I couldn't find before posting, apologies) it seems this hardware is indeed unsupported.

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
There's been a lot of new hardware released over the years. As new hardware is released, the library may be tweaked to fully accommodate new boards. The latest  boards are no different.

Like always, the library will work with ALL boards with the non-timer methods. The timer methods may require code to accommodate. Anyone is free to submit a pull request that will add support for a new chipset, just like always.

In other words, NewPing will work with the latest boards, and the timer methods will also work as the community submits a pull request to accommodate. This is how it always has been and still is true today.
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

Olivier31830

Hi,
I try to use SR04T with arduino Uno and newping  to measure distance under water ...
Do you think it's possible and when ?
I dont' find examples ....
I have some results  over 80cm but it' s not so good...
Olivier 31830

teckel

Hi,
I try to use SR04T with arduino Uno and newping  to measure distance under water ...
Do you think it's possible and when ?
I dont' find examples ....
I have some results  over 80cm but it' s not so good...
Olivier 31830

The sensor your using is designed to measure solid surface objects through the air. They won't work underwater.  You would need to use a sensor designed for underwater usage.

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

tsmacinspires

Hello! Thanks for this lovely library!
I'm using the following code to tell an actuator to move positions when something is in the way. I've got it to "stop," to adjust the actuator when something is in the way, but I cannot figure out where/how to add code to say "nothing is in the way, hold actuator in this position"
Said another wait, if nothing is sensed to actuator should be in "Go" position, when something is sense it's changed to "Stop" position.
Thanks!

/*
  Vehicle will have four sensors across the front.
  Sensors are named by location: Left (L), Middle Left (ML), Middle Right (MR), Right (R)
  Sensors Used: JSN-SR04T / RB-Dfr-720 :: Weatherproof Ultrasonic Sensor w/ Separate Probe
  http://www.robotshop.com/en/weatherproof-ultrasonic-sensor-separate-probe.html
  Uses NewPing Library: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
*/

#include <NewPing.h>

#define SONAR_NUM 4      // Number of sensors.
#define MAX_DISTANCE 130 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33
unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;


NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 4, MAX_DISTANCE),
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
};

void setup(void)
{
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pingTimer[0] = millis() + 75;
  for (uint8_t i = 1; i < SONAR_NUM; i++)
  pingTimer = pingTimer[i - 1] + PING_INTERVAL;

  pinMode(0, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

}

void loop() {
 for (uint8_t i = 0; i < SONAR_NUM; i++) {
   if (millis() >= pingTimer) {
     pingTimer += PING_INTERVAL * SONAR_NUM;
     sonar[currentSensor].timer_stop();
     currentSensor = i;
     sonar[currentSensor].ping_timer(echoCheck);
   }
 }
 // Other code that *DOESN'T* analyze ping results can go here.
 
}

void echoCheck() {
 if (sonar[currentSensor].check_timer())
   pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
 
}

void pingResult(uint8_t sensor, int cm) {
 // The following code would be replaced with your code that does something with the ping result.
 Serial.print(sensor);
 Serial.print(" ");
 Serial.print(cm);
 Serial.println("cm");
 digitalWrite(12, HIGH);
 delay(20);
 digitalWrite(11, LOW);
 Serial.println("STOP");
 delay(100);
 
}



teckel

Hello! Thanks for this lovely library!
I'm using the following code to tell an actuator to move positions when something is in the way. I've got it to "stop," to adjust the actuator when something is in the way, but I cannot figure out where/how to add code to say "nothing is in the way, hold actuator in this position"
Said another wait, if nothing is sensed to actuator should be in "Go" position, when something is sense it's changed to "Stop" position.
Thanks!

/*
  Vehicle will have four sensors across the front.
  Sensors are named by location: Left (L), Middle Left (ML), Middle Right (MR), Right (R)
  Sensors Used: JSN-SR04T / RB-Dfr-720 :: Weatherproof Ultrasonic Sensor w/ Separate Probe
  http://www.robotshop.com/en/weatherproof-ultrasonic-sensor-separate-probe.html
  Uses NewPing Library: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
*/

#include <NewPing.h>

#define SONAR_NUM 4      // Number of sensors.
#define MAX_DISTANCE 130 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33
unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;


NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 4, MAX_DISTANCE),
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
};

void setup(void)
{
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pingTimer[0] = millis() + 75;
  for (uint8_t i = 1; i < SONAR_NUM; i++)
  pingTimer = pingTimer[i - 1] + PING_INTERVAL;

  pinMode(0, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

}

void loop() {
 for (uint8_t i = 0; i < SONAR_NUM; i++) {
   if (millis() >= pingTimer) {
     pingTimer += PING_INTERVAL * SONAR_NUM;
     sonar[currentSensor].timer_stop();
     currentSensor = i;
     sonar[currentSensor].ping_timer(echoCheck);
   }
 }
 // Other code that *DOESN'T* analyze ping results can go here.
 
}

void echoCheck() {
 if (sonar[currentSensor].check_timer())
   pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
 
}

void pingResult(uint8_t sensor, int cm) {
 // The following code would be replaced with your code that does something with the ping result.
 Serial.print(sensor);
 Serial.print(" ");
 Serial.print(cm);
 Serial.println("cm");
 digitalWrite(12, HIGH);
 delay(20);
 digitalWrite(11, LOW);
 Serial.println("STOP");
 delay(100);
 
}



That code will never work how you're using it.  Can I ask where you got that base code from?  I need to somehow get people to stop using it and without knowing where you are getting the code I can't stop people from using it.

Basically, you're using two different programming paradigms in the same program, which simply isn't going to work.  I can give you the correct base code you should be using, but I really need to know how exactly you decided to use this code in the first place to stop the insanity!

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

tsmacinspires

I found the code on this forum, about halfway through the 80 pages. I was searching for code that uses four sensors and the median function. I couldn't get median to work with multiple sensors on my own using the tips you have provided (sorry I'm not a great coder). I sincerely appreciate your support to this community and will donate.

Basically I have four sensors attached to the front of a vehicle, and an actuator adjusting throttle (it has two positions controlled by pins 11 and 12). If nothing is in front of the vehicle the Arduino sends:
digitalWrite(11, HIGH); // Pin for actuator position "Go"
delay(20);
digitalWrite(11, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
Serial.println("GO");

If something is detected:
digitalWrite(12, HIGH); // Pin for actuator position "Stop"
delay(20);
digitalWrite(12, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
Serial.println("STOP");

teckel

I found the code on this forum, about halfway through the 80 pages. I was searching for code that uses four sensors and the median function. I couldn't get median to work with multiple sensors on my own using the tips you have provided (sorry I'm not a great coder). I sincerely appreciate your support to this community and will donate.
Better to start with the official page and the included examples.

Specifically, there's a 3 sensor sketch linked on my home page.

For 4 sensors, you just change SONAR_NUM and the sensor array appropriately.

Basically I have four sensors attached to the front of a vehicle, and an actuator adjusting throttle (it has two positions controlled by pins 11 and 12). If nothing is in front of the vehicle the Arduino sends:
digitalWrite(11, HIGH); // Pin for actuator position "Go"
delay(20);
digitalWrite(11, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
Serial.println("GO");

If something is detected:
digitalWrite(12, HIGH); // Pin for actuator position "Stop"
delay(20);
digitalWrite(12, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
Serial.println("STOP");
To do what you want, you would just replace the loop() in the above 3 sensor sketch with something like this:

Code: [Select]
void loop() {
  bool object_detected = false;
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    if (sonar[i].ping(130) > 0) {
      object_detected = true;
      break;
    }
  }
  if (object_detected == true) {
    digitalWrite(12, HIGH); // Pin for actuator position "Stop"
    delay(20);
    digitalWrite(12, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
  } else {
    digitalWrite(11, HIGH); // Pin for actuator position "Go"
    delay(20);
    digitalWrite(11, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
  }
  delay(30);
}


But, that's under the condition that's really what you want to do anytime any sensor detects anything in front of the sensors or not, which I question.  Basically, the NewPing logic is correct, but what you do with the information is difficult if not impossible to assist you with without the hardware and a better understanding of how the logic really should work.  But, that should give you a good start.

In total, here's probably what your code would look like:

Code: [Select]
#include <NewPing.h>

#define SONAR_NUM 4      // Number of sensors.
#define MAX_DISTANCE 130// Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(3, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 4, MAX_DISTANCE),
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE)
};

void setup(void) {
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop() {
  bool object_detected = false;
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    if (sonar[i].ping() > 0) {
      object_detected = true;
      break;
    }
  }
  if (object_detected == true) {
    digitalWrite(12, HIGH); // Pin for actuator position "Stop"
    delay(20);
    digitalWrite(12, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
  } else {
    digitalWrite(11, HIGH); // Pin for actuator position "Go"
    delay(20);
    digitalWrite(11, LOW);  // Turn Off Pin for actuator position "Go" (maintains position)
  }
  delay(30);
}


But again, just a guess without carnal knowledge.  I hope you can see how this is keeping it simple, so it's easy to understand and extend to do what you want, unlike the sample code you were trying to use which requires a much better understanding of timers and event-driven programming.

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

richmona

I cannot seem to get my code to work, I don't know what I am doing wrong.
Please Help
Getting the error:

Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\disstudent\Desktop\fix\fix.ino:15:0: warning: "TRIGGER_PIN" redefined

#define TRIGGER_PIN  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.



C:\Users\disstudent\Desktop\fix\fix.ino:9:0: note: this is the location of the previous definition

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.



C:\Users\disstudent\Desktop\fix\fix.ino:16:0: warning: "ECHO_PIN" redefined

#define ECHO_PIN     9  // Arduino pin tied to echo pin on the ultrasonic sensor.



C:\Users\disstudent\Desktop\fix\fix.ino:10:0: note: this is the location of the previous definition

#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.



Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':

(.text+0x0): multiple definition of `__vector_13'

libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here

c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions

collect2.exe: error: ld returned 1 exit status

Multiple libraries were found for "NewPing.h"
Used: C:\Users\disstudent\Documents\Arduino\libraries\NewPing
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
[/b]





#include <NewPing.h>



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

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // 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 sonar1(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

#define TRIGGER_PIN  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     9  // 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 sonar2(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {

Serial.begin(115200);//Open Serial Monitor
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
 
   delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

 unsigned int uS = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
 Serial.print("Ping: ");
 Serial.print(sonar1.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
 Serial.println("cm");
 
   unsigned int US = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
 Serial.print("Ping: ");
 Serial.print(sonar2.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
 Serial.println("cm");

 if (uS < 18) //If Sonar1 distance is less than 18cm
 tone (3,50,200); //a buzzer on port 3 will go off
 if (US<18) //If Sonar2 distance is less than 18cm
 tone (4,50,2000); // a buzzer on port 4 will go off
}
// Do other stuff here, notice how there's no delays in this sketch, so you have processing cycles to do other things :)
}

tsmacinspires

Ok, cool - so your code is mostly working, but I'm getting a good amount of false "goes." If placing an item in front of any of the sensors it will send a signal to "STOP," however if I keep holding it there it sends a "GO" signal even though I'm in front of the sensors, it happens rather quickly. So instead of it staying at stop, if I'm standing in front of the sensors it basically goes from STOP to GO back and forth. Any ideas? Thanks so much!
Sensors used: JSN-SR04T
Microcontroller: Arduino Uno
Uno has three pins connected to transistors which control grounding thee different wires from the following device (three different wires, when grounded, go to three different positions): https://www.northerntool.com/shop/tools/product_200520503_200520503
That device is connected to:
https://www.northerntool.com/shop/tools/product_200520510_200520510

teckel

I cannot seem to get my code to work, I don't know what I am doing wrong.
You're using code that you don't understand as a basis. And where the hell is everyone getting that code from and trying to use it without knowing how to code? Please tell me were you got it so I can stop these constant questions about using the wrong code for your coding abilities.

Please see the official NewPing site, use code examples from that page, and read the warnings about some of the examples that are HEAVILY discouraged for you to use if you're not an expert.

I can't and will not help debug your code if you're not using my examples and heeding all the warnings.

STOP THE INSANITY!!!
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

Ok, cool - so your code is mostly working, but I'm getting a good amount of false "goes." If placing an item in front of any of the sensors it will send a signal to "STOP," however if I keep holding it there it sends a "GO" signal even though I'm in front of the sensors, it happens rather quickly. So instead of it staying at stop, if I'm standing in front of the sensors it basically goes from STOP to GO back and forth. Any ideas? Thanks so much!
Sensors used: JSN-SR04T
Microcontroller: Arduino Uno
Uno has three pins connected to transistors which control grounding thee different wires from the following device (three different wires, when grounded, go to three different positions): https://www.northerntool.com/shop/tools/product_200520503_200520503
That device is connected to:
https://www.northerntool.com/shop/tools/product_200520510_200520510
It sounds more like the code IS working, but your understanding of the results you are seeing is the issue.

What kind of object are you putting in front of the sensor? How big is it? What kind of surface does it have? What if you replace the object with a large flat board placed at 90 degrees from the sensor? What about doing multiple pings using the ping-median() method instead or writing your own function to ping multiple times?

Basically, this isn't a problem with the NewPing library or even with sample code. The problem is understanding the physics of sonar and knowing how to write code to solve your unique problem.
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