Pages: 1 ... 17 18 [19] 20 21 ... 30   Go Down
Author Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.5  (Read 89474 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the reply. I will post the code this evening. Here is a picture of the hardware. I am sorry but I can't show the actual set up or the 3in2 object being measured. The red and black wires hanging out from one sensor was temporary to watch on a scope.


* IMG_20121120_111410.jpg (3765.85 KB, 2592x1944 - viewed 42 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the code that I normally use:
Code:
#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13
#define MAX_DISTANCE 200  // In Centimeters (200cm = 78.7") It will not keep waiting for a return if past this value

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {
  float uS = sonar.ping_median(5);
  Serial.print(uS / US_ROUNDTRIP_IN);
  Serial.print("a");
}

Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 457
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the code that I normally use:
Code:
#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13
#define MAX_DISTANCE 200  // In Centimeters (200cm = 78.7") It will not keep waiting for a return if past this value

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {
  float uS = sonar.ping_median(5);
  Serial.print(uS / US_ROUNDTRIP_IN);
  Serial.print("a");
}


I was hoping to get the picture of your testing jig in the test environment to look for possible echo sources.

But, looking at just your sketch I can see a problem.  Your sketch is sending a ping on top of an old ping, with no pause between.  This won't give reliable results.  What you'll get is possible echos.  You *MUST* wait *AT LEAST* 29ms between pings.  The reason you shouldn't go faster has to do with the sensor hardware and the speed of sound.  The sensors can sense up to around 500cm away (1000cm round-trip).  Speed of sound is around 29uS per cm.  That works out to 29ms.  If you don't wait *AT LEAST* 29ms, you could get an ping echo from a previous ping giving you all kinds of strange results.

While the ping_median() method adds a delay between each ping, it doesn't add a delay before or at the end.  So, the first ping is basically on top of the last ping.  It doesn't add a delay at the end of the ping_median() on purpose, so you can add whatever delay you want, or none at all if it's a one-time only ping.

So, after the sonar.ping_median(5), you need to add a line with something like "delay(29);"

Also, you shouldn't use pin 13 if you're using an Arduino as there's an LED on that pin.  Change that to any other pin to avoid possible conflict.  I stopped using pin 13 in my sample sketches due to a couple users having problems with an LED conflict.  Best to avoid that.

If you still have a problem, I would lengthen the delay between pings to something much larger, say 100ms.  You need to change the delay(29) to delay(100) as well as edit the NewPing.h file and change the PING_MEDIAN_DELAY from 29000 to 100000.  While a ping delay of 29ms should be long enough, if the sensor is too sensitive (say it can sense up to 600cm away, it would need at least a 600*2*29=34800uS [35ms] delay between pings).

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thank you for the reply and advice. I did not realize that there was no delay between the last median ping and the next first median ping. I will update like the below (actually, I will probably go with 35 delay in the library and below):
Code:
#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200  // In Centimeters (200cm = 78.7") It will not keep waiting for a return if past this value

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {
  float uS = sonar.ping_median(5);
  delay(29);
  Serial.print(uS / US_ROUNDTRIP_IN);
  Serial.print("a");
}
I did run with no median and a 200 delay, and I still got some stray distance returns (that are oddly similar in value for a certain test period or day or two). I can live with a certain amount of stray distance returns, but some times it gets real bad. I will run on pin 11. I have gone through fixes that seem to fix the problem, and then I find out days later that the problem is back. Your library gave perfect results for a good while (earlier sketches with and without libraries did give stray values). I found that 3 different 9vdc wall warts gave stray returns, and then laptop or wall plug 5v usb had perfect returns....a month later I couldn't tell the difference between the power inputs. I do see some 60 hz at the sensor with the  9vdc wall warts. I have been wondering if I need a separate power supply for the ultrasonic sensor ( I don't know if there is a ground problem doing that). I think I will solder a 10uF capacitor across the sensor Vcc and ground. There seems to be two 47uF caps on the Arduino for its regulation (the new Arduino Due only has one I just noticed). I am going to do more testing, but I have to admit that I may have to change to a different type of sensor set up. My setup possibly does have echo problems. I may try with single pings every 2 seconds just to try to rule out echos as problems (I have tried with 500 delay before). I also am going to try completely unplugging the bluetooth module and watching the serial printer on the laptop (I usually watch distance values on my tablet via bluetooth).
Thank you again.
« Last Edit: November 21, 2012, 07:47:31 pm by CyklKent » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, I am full. I want to eat more though smiley-lol
The below is what I use when I am wanting to see every ping result for testing (yes, I know that delays are to be avoided (even though Tim just told me I needed to add a delay between medians)).
Code:
#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     13
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {
  float uS = sonar.ping();
  Serial.print(uS / US_ROUNDTRIP_IN);
  Serial.print("a");
  delay(500);
}

« Last Edit: November 22, 2012, 11:56:08 pm by CyklKent » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I have a project with 8 HC-SR04.

My problem is that when I trigger a sensor, the sensor closest are also triggered. I'm not yet used this new library. Can anyone give me an idea how can I do that with this new library?

The project summary is:
8 sensors HC-SR04 each sensor being fired one at a time.

The distance that the sensor is triggered should be between 5 and 15 cm.

Best regards.

murphy

[[|]]'s
« Last Edit: November 24, 2012, 01:29:39 pm by murphy » Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 278
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

I have a project with 8 HC-SR04 sharp sensors.

My problem is that when I trigger a sensor, the sensor closest are also triggered. I'm not yet used this new library. Can anyone give me an idea how can I do that with this new library?

The project summary is:
8 sensors sharp HC-SR04 each sensor being fired one at a time.

The distance that the sensor is triggered should be between 5 and 15 cm.

Best regards.

murphy

[[|]]'s

Sharp sensors? Those are completely different sensor to the HC-SR04.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, is only HC-SR04. I thought they were maded by sharp
Logged

Europe
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is it ok to use this:

HC-SR04 -------> > > > >             blank space          < < < < < - ------HC-SR04 sensor 2

is it ok if i limit the max reading range of both sensors to like 50 cm (and i need to read 50 cm), and i place sensors like 200 cm away from each other..
Would this work if each sensor had an independent microcontroller ???
Code:
delay(STEP_TIME);
  unsigned int uS1 = SONAR_L.ping();
  per_l = uS1 / US_ROUNDTRIP_CM;

Is the blank space enough, or should i use a "sponge" to prevent interference between 2 of the sensors ?????
THX smiley-mr-green
Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 457
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a project with 8 HC-SR04.

My problem is that when I trigger a sensor, the sensor closest are also triggered. I'm not yet used this new library. Can anyone give me an idea how can I do that with this new library?

The project summary is:
8 sensors HC-SR04 each sensor being fired one at a time.

The distance that the sensor is triggered should be between 5 and 15 cm.

I have an example sketch that pings 15 sensors that's designed to be scaled to any number of sensors.

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

Just set SONAR_NUM to 8 and modify the sonar object array to 8 sensors and to the pins tied to each.  This sketch works using the timer method so there's no delays and it will therefore work in many complicated sketches (just do everything triggered from events with no delays).  Also, as NewPing will work using the same pin for both trigger and echo, you only need to use 8 pins to work with all 8 sensors.  So, it's possible that your project would fit on a Uno or Teensy 2.0.  Which is a huge bonus for both price and size.

You shouldn't have any problem getting this example sketch working with your 8 sensors.  And it shouldn't be much of a challenge getting it integrated with your overall project sketch.

I'm going to make another post with tips on using the 15 sensor example sketch as some are having problems understanding how to use an event-driven sketch like this.

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 457
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

is it ok to use this:

HC-SR04 -------> > > > >             blank space          < < < < < - ------HC-SR04 sensor 2

is it ok if i limit the max reading range of both sensors to like 50 cm (and i need to read 50 cm), and i place sensors like 200 cm away from each other..
Would this work if each sensor had an independent microcontroller ???
Code:
delay(STEP_TIME);
  unsigned int uS1 = SONAR_L.ping();
  per_l = uS1 / US_ROUNDTRIP_CM;

Is the blank space enough, or should i use a "sponge" to prevent interference between 2 of the sensors ?????
THX smiley-mr-green

The thing to remember is that setting the maximum distance doesn't change how strong the trigger ping is nor how far the sensor will actually sense a ping.  All it does is just make anything beyond the set distance register as "clear" and the Arduino to stop monitoring that sensor.  The strength of the ping and the ability for a sensor to read a ping up to 500cm away is still there.

There's still a benefit in setting this distance as short as possible for your project.  Especially when using the standard ping() method.  But, don't think of it as changing the way the actual sensor works.

What you always need to consider is that when a sensor does a ping, that ping can echo from around 29 to 35ms.  It doesn't matter what you set the maximum distance to, this is how long a ping could be read by another sensor as a stray echo.

So in your example, it really depends on what the time is between pings.  If the ping to ping time is 100ms, there would be no issue.  If, however, you want to ping both sensors at the exact same time, you could have a problem.

Hope this helps.

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Europe
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is it ok to use this:

HC-SR04 -------> > > > >             blank space          < < < < < - ------HC-SR04 sensor 2

is it ok if i limit the max reading range of both sensors to like 50 cm (and i need to read 50 cm), and i place sensors like 200 cm away from each other..
Would this work if each sensor had an independent microcontroller ???
Code:
delay(STEP_TIME);
  unsigned int uS1 = SONAR_L.ping();
  per_l = uS1 / US_ROUNDTRIP_CM;

Is the blank space enough, or should i use a "sponge" to prevent interference between 2 of the sensors ?????
THX smiley-mr-green

The thing to remember is that setting the maximum distance doesn't change how strong the trigger ping is nor how far the sensor will actually sense a ping.  All it does is just make anything beyond the set distance register as "clear" and the Arduino to stop monitoring that sensor.  The strength of the ping and the ability for a sensor to read a ping up to 500cm away is still there.

There's still a benefit in setting this distance as short as possible for your project.  Especially when using the standard ping() method.  But, don't think of it as changing the way the actual sensor works.

What you always need to consider is that when a sensor does a ping, that ping can echo from around 29 to 35ms.  It doesn't matter what you set the maximum distance to, this is how long a ping could be read by another sensor as a stray echo.

So in your example, it really depends on what the time is between pings.  If the ping to ping time is 100ms, there would be no issue.  If, however, you want to ping both sensors at the exact same time, you could have a problem.

Hope this helps.

Tim
Thanks for the anwser...
I will solve the problem by syncing them (only one sensor will work at a time, then a short delay, then the second will work etc...)
Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 457
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There's been some confusion about how to use the 15 Sensors Example Sketch.  I've changed some of the comments in the online code to help.  But, I think a post explaining this further would be beneficial.

The 15 Sensors Example Sketch uses the ping_timer() method which is designed for event-driven sketches that don't use delays.  If you're only used to Arduino sketches that are linear and have delays in the code, this concept will be quite foreign to you.  However, it's a good idea to try and embrace an event-driven paradigm.  Simple example "hello world" sketches work fine using delays.  But, once you try to do a complex project, using delays will often result in a project that just doesn't do what you want.  Consider controlling a motorized robot with remote control that balances and using ping sensors to avoid collisions.  Any delay at all, probably even 1 ms, would cause the balancing to fail and therefore your project would never work.  In other words, it's a good idea to start not using delays at all, or you're going to have a really hard time getting your project off the ground (literally with the balancing robot example).

So, it's good to first note that the 15 sensor example doesn't use any delays and is event driven.  You can think of it as multitasking.  The for() loop in void() polls each sensor array waiting for when it's that sensor's time to ping.  If it's time to ping, it triggers a ping_timer() to run in the background.  After the for() loop in void() where the comment is, you could add other code here that doesn't have anything to do with ping sensors.  For example: monitoring other sensors, controlling status lights, or whatever.  But, it's VERY important that these other things are also designed in an event-driven way with no delays.  If you add delays, it will miss ping times and skip them, resulting in what appears to be an error.

echoCheck() is where the background ping is polled every 24uS to see if a ping was received. If a ping is received, it sets the ping distance in CM in the cm[] array.  You shouldn't add anything to echoCheck() to use this sketch in its designed way.  This function always needs to be very lean as it's called every 24uS during a ping.

oneSensorCycle() is called once all of your sensors have been polled.  Lets say you have 8 sensors.  Every time all 8 sensors are pinged, this function is called and you can do something with the results.  For example, determine the surroundings of your robot and maybe make a motor direction change.  It doesn't have to output the results over a serial connection, this is just here for testing.  In a real-world sketch, you would remove or comment out everything in the oneSensorCycle() function and replace it with your code that does something with the sensor data in the cm[] array.

Also note, the 15 Sensors Example Sketch is designed to ping all the sensors and then do something with the results once all the sensors have been polled.  Some want to ping each sensor and then do something right away maybe between any two pings.  To do that, we no longer need the oneSensorCycle() function nor the if() statement in loop() that calls oneSensorCycle().  The following sketch calls the pingResult() function every time there's a ping within range.  Because this sketch still keeps the cm[] array, you can look at neighboring ping results in the cm[] array to do whatever calculations you need to do.

Code:
#include <NewPing.h>

#define SONAR_NUM      3 // Number of 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(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(8, 9, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer()) {
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
    pingResult(currentSensor);
  }
}

void pingResult(uint8_t sensor) { // Sensor got a ping, do something with the result.
  // The following code would be replaced with your code that does something with the ping result.
  Serial.print(sensor);
  Serial.print(" ");
  Serial.print(cm[sensor]);
  Serial.println("cm");
}

If, however, you don't really care about comparing neighboring ping results and just want to ping multiple sensors and anytime there's something within range you want to trigger something, you can totally get rid of the cm[] array.  The following code is probably the easiest to understand, as it simply pings each sensor and when there's a ping within range, the pingResult() function is called.  You get the sensor number and the cm distance which you can do something with.  It will only call pingResult() when one of the sensors "hears" something within range.

Code:
#include <NewPing.h>

#define SONAR_NUM      3
#define MAX_DISTANCE 200
#define PING_INTERVAL 33

unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;

NewPing sonar[SONAR_NUM] = {
  NewPing(4, 5, MAX_DISTANCE),
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(8, 9, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;
  for (uint8_t i = 1; i < SONAR_NUM; i++)
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += 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");
}

Remember, to analyze the ping results, you do that in the pingResults() function in the above sketches or in the oneSensorCycle() function in the 15 Sensors Example Sketch.  Also, none of these will properly work if you do any delay statements at any point in your sketch.  

If you ever want to stop the pings in your sketch, for example to do something that requires delays or takes longer than 33ms to process, do the following:
Code:
for (uint8_t i = 0; i < SONAR_NUM; i++) pingTimer[i] = -1;

To start the pings again, do the following:
Code:
pingTimer[0] = millis();
for (uint8_t i = 1; i < SONAR_NUM; i++) pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;

Hope this helps!  Future questions about using the 15 Sensors Example Sketch will be directed to this post first in the hope this will explain things better.  I can see some additions/corrections in the future to further clarify.

Tim
« Last Edit: December 01, 2012, 11:11:32 am by teckel » Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've bought a chinese "reverse gear obstacle detector for cars" ultrasonic sensor and the board , i checked the board and find the trigger and echo pins for sensor like SR04 , but this sensor has a different specifications from SR04 , i want to use this new ping library for measuring the distance  with arduino and this sensor, i am not sure witch parts of the code i have to change to work with my sensor , here is the specification of the sensor i got:

center frequency : 40KHZ  (i checked the input pulse on the board with osciloscope it is 16 bursts)
Ringing : 1.2ms max
maximum input voltage : 120 Vp-p
Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 457
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've bought a chinese "reverse gear obstacle detector for cars" ultrasonic sensor and the board , i checked the board and find the trigger and echo pins for sensor like SR04 , but this sensor has a different specifications from SR04 , i want to use this new ping library for measuring the distance  with arduino and this sensor, i am not sure witch parts of the code i have to change to work with my sensor , here is the specification of the sensor i got:

center frequency : 40KHZ  (i checked the input pulse on the board with osciloscope it is 16 bursts)
Ringing : 1.2ms max
maximum input voltage : 120 Vp-p


Have you tried connecting it and using the sample NewPing sketch?  By the pin labels, it sounds like it could work as-is.  The 120V is a little confusing, but the specs seems a little thin to begin with.  Got a picture or more information?

Tim
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

Pages: 1 ... 17 18 [19] 20 21 ... 30   Go Up
Jump to: