Go Down

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

teckel

hi all anybody can  tell me that what is that US_ROUNDTRIP_IN in the code? and how can i get distance in INCHES kindly waiting for you reply ?
See the NewPing syntax wiki

You can just use the sonar.ping_in() method or do a regular sonar.ping() and convert it to inches with sonar.convert_in().  Basically, for all the examples, just replace the CM with IN and the results are in inches.  I use CM as a default because it's more granular.

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

enfeather

Hi Tim,

Apologies if this falls slightly out width the scope of this thread: I am using your NewPing library with the 4-pin Ultrasonic HC-SR04. I wish to read the distance (serial) data into Maxuino using Firmata. Is there a simple way to integrate the NewPing code into the StandardFirmata sketch and hence read the values in Maxuino?

Any thoughts would be much appreciated,

Thanks,

Michael

mcain99

I am looking at the NewPing15Sensors example and I'm trying to understand the code in the loop():
Code: [Select]
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.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      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).
    }
  }
  // The rest of your code would go here.
}


The currentSensor global variable gets set, then the ping_timer function is called and the for loop goes on to the next sensor check.  However it is possible (but probably not likely) for the next sensor check to overwrite the currentSensor variable before the echoCheck function for the previous sensor is called.  Or am I not understanding what is happening in the example?

z3t0

Not sure if this has been asked... but which pins on the arduino uno r3 can be used for sonar? I mean for the single pin sketch.

Thanks.

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

z3t0

Which sensor are you going to use?
Sorry for late reply. I am currently using the Parallax Ping Sensors.
Are there any better ones you would suggest? Or maybe something slightly... cheaper :D.

Its a school project so I have to consider how much  money it costs so everyone can participate.

Thanks!

NomNom

I am not sure why I'm not getting a return of zero for sensor readings that are out of range.  I get wildly varying distances when there is nothing in front of the sensors, but accurate readings when I have something in front.  Any ideas? I have two HC-SR04s on my bot, so I modified the NewPingExample for 2 sensors, but it does the same thing when only one sensor is being pinged.

Hey TryChick,

Did you ever get this issue resolved?

I've been having the same problem with the sensor setup to read between 100cm-400cm
Using a DYPME007Y sensor and an Attiny84.Instead of showing a '0' for anything past 400cm, the readings are fluctuating all over the place but the sensor is accurate when something is in front of it.

Any assistance would be super appreciated from anyone who can chyme in on this.

teckel

The currentSensor global variable gets set, then the ping_timer function is called and the for loop goes on to the next sensor check.  However it is possible (but probably not likely) for the next sensor check to overwrite the currentSensor variable before the echoCheck function for the previous sensor is called.  Or am I not understanding what is happening in the example?
It would be possible, but only if you had unrealistic timing set between pings.  In other words, in the example sketch it's not possible, but if you change the time between pings to be 1ms, then yes, it could happen.  But then, there would be a ton of other problems anyway.

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

Sorry for late reply. I am currently using the Parallax Ping Sensors.
Are there any better ones you would suggest? Or maybe something slightly... cheaper :D.

Its a school project so I have to consider how much  money it costs so everyone can participate.

Thanks!
You can get sensors on eBay for about $3 (probably less) including shipping.  You can use any pin.  I'd stay away from pin 13 because it has the led on it.  But the rest shouldn't have a problem.  There's not really a best pin to use, all work equally well.

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

Hey TryChick,

Did you ever get this issue resolved?

I've been having the same problem with the sensor setup to read between 100cm-400cm
Using a DYPME007Y sensor and an Attiny84.Instead of showing a '0' for anything past 400cm, the readings are fluctuating all over the place but the sensor is accurate when something is in front of it.

Any assistance would be super appreciated from anyone who can chyme in on this.
It's possible that 400cm is just too far to get consistent readings.  While the specs on most sensors say they work to 500cm, I find that that's no typically the case.  Try setting the limit to maybe 200cm and see if you get more predictable results.

Also, returning zero is correct when something is out of range.  1-max is what you get when something is in range, and zero is out of range.  This is so you can setup logic for in and out of range.  Making out of range be max, for example, is different than something that isn't there at all.  Anyway, with it set to zero, you can use your own logic because you have a unique condition on out of range.  There's a define setting in the library that you could change to set the out of rage to another value.  So, you could do something like 500 if you wanted it to be a different value instead of zero.

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

NomNom

It's possible that 400cm is just too far to get consistent readings.  While the specs on most sensors say they work to 500cm, I find that that's no typically the case.  Try setting the limit to maybe 200cm and see if you get more predictable results.

Also, returning zero is correct when something is out of range.  1-max is what you get when something is in range, and zero is out of range.  This is so you can setup logic for in and out of range.  Making out of range be max, for example, is different than something that isn't there at all.  Anyway, with it set to zero, you can use your own logic because you have a unique condition on out of range.  There's a define setting in the library that you could change to set the out of rage to another value.  So, you could do something like 500 if you wanted it to be a different value instead of zero.

Tim
Hey Tim,

Thanks for getting back to me on this issue. Please bare with me since I'm still in 10th grade and fairly new to some of these aspects

I have tried a simple sketch that will blink an LED if anything is between 100cm to 400cm on an Arduino and the code worked perfectly. The light would turn ON when an object was within that range and if i pointed the sensor straight up towards the sky the LED would turn OFF and on the serial monitor i would see 0cm repeated.The sensor I'm using reads pretty well up to 480cm.

BUT when i put the same code onto an Attiny84@ 8mhz everything works perfect ,including detecting objects at closer range, except for the part where me pointing the sensor to the sky would still activate the LED instead of it being read as a '0' and keeping the LED turned OFF.

The Attiny84 is communicating with the sensor through a logic level converter, since i need to run the Attiny84 from a 3.6v Lipo battery but the sensor is running off a 5v step up module so that i can save power when the sensor is not needed.

Could the issue be the logic level converter?
Or, possibly the power supply for the Sensor and/or Attiny 84.

I attached a very rough drawing of what the circuit looks like but hopefully it could point to a potential fault.
Also I attached the fairly straightforward code I'm using with the Attiny84, all timer related parts are commented out in the NewPing library.

Hope this can be of some help, thanks again.


teckel

Hey Tim,

Thanks for getting back to me on this issue. Please bare with me since I'm still in 10th grade and fairly new to some of these aspects

I have tried a simple sketch that will blink an LED if anything is between 100cm to 400cm on an Arduino and the code worked perfectly. The light would turn ON when an object was within that range and if i pointed the sensor straight up towards the sky the LED would turn OFF and on the serial monitor i would see 0cm repeated.The sensor I'm using reads pretty well up to 480cm.

BUT when i put the same code onto an Attiny84@ 8mhz everything works perfect ,including detecting objects at closer range, except for the part where me pointing the sensor to the sky would still activate the LED instead of it being read as a '0' and keeping the LED turned OFF.

The Attiny84 is communicating with the sensor through a logic level converter, since i need to run the Attiny84 from a 3.6v Lipo battery but the sensor is running off a 5v step up module so that i can save power when the sensor is not needed.

Could the issue be the logic level converter?
Or, possibly the power supply for the Sensor and/or Attiny 84.

I attached a very rough drawing of what the circuit looks like but hopefully it could point to a potential fault.
Also I attached the fairly straightforward code I'm using with the Attiny84, all timer related parts are commented out in the NewPing library.

Hope this can be of some help, thanks again.
Have you tried not using the logic level converter?  I would guess that a 3.6v trigger would cause the sensor to ping and the Arduino would sense the 3.6v output signal as well.

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

MarianaaRoldan

Hello teckel, my name is Mary.

Let me tell you that i'm working on my thesis and I decided to use two HC-SR04 sensors to measure two different distances and then obtain the difference between them. I'm using a 16x2 LCD display to show both numbers but I have a problem with this. This is my program:

#include <LiquidCrystal.h>
#define PIN_TRIG 9
#define PIN_ECO  13
#define PIN_ECO2 8

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

  Serial.begin (9600); 

  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECO, INPUT);
  pinMode(PIN_ECO2, INPUT);



}

void loop() {
  float duration, distance;
  float duration2, distance2; 
  float diference;
 
 
  digitalWrite(PIN_TRIG, LOW); 
  delayMicroseconds(2);
  digitalWrite(PIN_TRIG, HIGH); 
  delayMicroseconds(10);       
  digitalWrite(PIN_TRIG, LOW);
     
 
  duration = pulseIn(PIN_ECO, HIGH);
  duration2 = pulseIn(PIN_ECO2, HIGH);

 
  distance = (duration/2) / 29;
  distance2 = (duration2/2) / 29;
 
 
  difference= distance - distance2;
 
 
  lcd.clear();
  lcd.setCursor(0, 0);
 
 
  lcd.setCursor(0,1);
  lcd.print(difference);
  lcd.print(" cm");
   
   
  delay(100);
 
}

I see that the numbers vary slightly in its decimal part, and my question: Is there any kind of mean filter or way to obtain a "static" result in the decimal part?

Thank you for your time! Greetings!

teckel

Hello teckel, my name is Mary.

Let me tell you that i'm working on my thesis and I decided to use two HC-SR04 sensors to measure two different distances and then obtain the difference between them. I'm using a 16x2 LCD display to show both numbers but I have a problem with this. This is my program:

#include <LiquidCrystal.h>
#define PIN_TRIG 9
#define PIN_ECO  13
#define PIN_ECO2 8

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

  Serial.begin (9600); 

  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECO, INPUT);
  pinMode(PIN_ECO2, INPUT);



}

void loop() {
  float duration, distance;
  float duration2, distance2; 
  float diference;
 
 
  digitalWrite(PIN_TRIG, LOW); 
  delayMicroseconds(2);
  digitalWrite(PIN_TRIG, HIGH); 
  delayMicroseconds(10);       
  digitalWrite(PIN_TRIG, LOW);
     
 
  duration = pulseIn(PIN_ECO, HIGH);
  duration2 = pulseIn(PIN_ECO2, HIGH);

 
  distance = (duration/2) / 29;
  distance2 = (duration2/2) / 29;
 
 
  difference= distance - distance2;
 
 
  lcd.clear();
  lcd.setCursor(0, 0);
 
 
  lcd.setCursor(0,1);
  lcd.print(difference);
  lcd.print(" cm");
   
   
  delay(100);
 
}

I see that the numbers vary slightly in its decimal part, and my question: Is there any kind of mean filter or way to obtain a "static" result in the decimal part?

Thank you for your time! Greetings!
You're using the "standard" way of communicating with ping sensors.  This doesn't work well at all, which is why I developed the NewPing library.

I would suggest implementing my library in your script and use the ping_medium() method.  Also, separating out the triggers to two pins (one for each sensor) instead of trying to do both at the same time.  When using ping_medium(), NewPing will by default do 5 pings and then select the medium distance.  This will be the filter you're looking for to get more stable results.  You can also increase the number of pings to further filter the results.  For example, ping_medium(15) will find the medium ping over 15 pings, which should be very stable.

I also use a very fast algorithm to get the medium value, so the computation is very fast.

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

NomNom

Have you tried not using the logic level converter?  I would guess that a 3.6v trigger would cause the sensor to ping and the Arduino would sense the 3.6v output signal as well.

Tim
Hey Tim,

I believe i might have figured out the issue, turns out the boost converter was outputting an inconsistent voltage which was causing these strange fluctuations. I added a 400uf capacitor which seems to have smoothed out the voltage output and solved the problem.

I just wanted to make sure i came back and wrote down my solution in case someone else is running into a similar issue and also to thank you for being the badass behind this awesome library and for the continued support :)

Go Up