Go Down

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


ultimatehell

hi ;) i spent the past hour trying to figure this out, but just couldnt find an answer ;) so excuse me if i missed it!

here goes:

i want to use a HC-SR04 single pin solution with an Arduino Mega 2560, on the sketch page someone posted

Quote
This works with an Arduino Mega 2560 and a HC-SR04.

Be aware though that it draws 60-85mA during the 10?s trigger HIGH time from the Arduino pin to overrule the echo LOW, which is beyond the datasheet per-pin maximum (40mA). You can also see a small drop in VCC during these 10?s.


is this true? i want to use 4 of these sensors and that would basicly "overload" 4 pins by double of its specs every couple seconds and potentialy damage the arduino?

i dont really see a reason for this using up to 85mA on the ping pin since it should only draw those amounts of power from the 5V connection, or am i wrong?

i do not have the sensors here yet so i can not measure this my self currently ;) so i figured i just ask :) thank you for your reply!


teckel


hi ;) i spent the past hour trying to figure this out, but just couldnt find an answer ;) so excuse me if i missed it!

here goes:

i want to use a HC-SR04 single pin solution with an Arduino Mega 2560, on the sketch page someone posted

Quote
This works with an Arduino Mega 2560 and a HC-SR04.

Be aware though that it draws 60-85mA during the 10?s trigger HIGH time from the Arduino pin to overrule the echo LOW, which is beyond the datasheet per-pin maximum (40mA). You can also see a small drop in VCC during these 10?s.


is this true? i want to use 4 of these sensors and that would basicly "overload" 4 pins by double of its specs every couple seconds and potentialy damage the arduino?

i dont really see a reason for this using up to 85mA on the ping pin since it should only draw those amounts of power from the 5V connection, or am i wrong?

i do not have the sensors here yet so i can not measure this my self currently ;) so i figured i just ask :) thank you for your reply!


Anyone can post any comment, incorrect or not.  I believe this person is just seeing a normal spike they would see with two pins as well.  It's also only for 10?s.  But, you could use a diode across the trigger and echo pins instead of just a jumper.  That would prevent any current from going to the echo pin if you're really worried about this.  I would have no way to accurately measure the current drawn for only 10?s.  But, I'd highly doubt it was 60-85mA.  I've also ran sensors for days and never had a problem.  I have done current draw measurements, but they were substantially lower.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

ultimatehell

great, exactly the response i expected ... just wanted to make sure :D thank you very much!

jahue

I currently have an issue where I am trying to replace a proximity sensor, with a ping sensor... this is mostly due to distance issues (not enough)

The following code works for proximity...
Code: [Select]
const int sensorPin1 = 0; //analog pin 0
const int ledPin1 = 9;   // LED1,   connected to digital pin 9

const int sensorPin2 = 1; //analog pin 0
const int ledPin2 = 10;   // LED2,   connected to digital pin 9

const int sensorPin3 = 2; //analog pin 0
const int ledPin3 = 11;   // LED3,   connected to digital pin 9

int sensorValue1, sensorValue2, sensorValue3;

int ledVal1 = 0;
int ledVal2 = 0;
int ledVal3 = 0;
unsigned long lastFullBrightnessTimeL1 = 0;
const unsigned long fullBrightnessIntervalL1 = 5000;
unsigned long lastFadeTimeL1 = 0;

unsigned long lastFullBrightnessTimeL2 = 0;
const unsigned long fullBrightnessIntervalL2 = 5000;
unsigned long lastFadeTimeL2 = 0;


#include <NewPing.h>

#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // 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.


//const unsigned long fadeInterval = 1;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);// sets the pins as output
  // pinMode(s3Pin, INPUT);
  Serial.begin(9600);  // ...set up the serial ouput in 0004 format
}

// Main program
void loop() {
  unsigned long currentMillis = millis();
  sensorValue1 = analogRead(sensorPin1);
  sensorValue2 = analogRead(sensorPin2);
  sensorValue3 = analogRead(sensorPin3);


   // Set 1
    if (sensorValue1 >= 150) {  // Person in range
      if (ledVal1 <= 254)
        ledVal1++;
      else
        lastFullBrightnessTimeL1 = currentMillis;
    }
       
    else { // Person has moved away   
      if (ledVal1 >= 1 && currentMillis - lastFullBrightnessTimeL1 > fullBrightnessIntervalL1)
        ledVal1--;
    }

    if (sensorValue1 <= 150 && ledVal1 <= 1)
  { ledVal1 = 0;
    analogWrite(ledPin1, ledVal1);
  }   
   
    analogWrite(ledPin1, ledVal1);
   
   
   //Set 2
   
   
   if (sensorValue2 >= 90) {  // Person in range
      if (ledVal2 <= 254)
        ledVal2++;
      else
        lastFullBrightnessTimeL2 = currentMillis;
    }
       
    else { // Person has moved away   
      if (ledVal2 >= 1 && currentMillis - lastFullBrightnessTimeL2 > fullBrightnessIntervalL2)
        ledVal2--;
    }

    if (sensorValue2 <= 90 && ledVal2 <= 1)
  { ledVal2 = 0;
    analogWrite(ledPin2, ledVal2);
  }   
   
    analogWrite(ledPin2, ledVal2);
   
   
   
   //Set3
      // delay(50);
  //Serial.println(sensorValue1);
  Serial.println(sensorValue1);

 
}


I moved over to the Arduino base ping sensor and was getting to many fluctuations and problems with the built in delay... so I tried your version. Much better, but I am having issues with the LEDs. The fade kinda works however it is incredibly slow, and flickers and locks on and never goes off sometimes. The original fade, builds in fairly quickly and builds out after a set time if no ones around.

The whole installation works like this, as some one approaches the LED is triggered and it fades up, if you don't get inside the set distance then it fades back out, if you get inside or stand for a brief period of time the LED locks on for a set amount of time then if the sensor goes back to reading outside of the set distance the LED fades out but only after a set amount of time.

I tried this code, Side note, I need to run several sensors and several different lights at the same time all doing their own thing. I am running all power externally (conditioned block).

Code: [Select]
#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(7, 7, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE),
  NewPing(5, 5, MAX_DISTANCE)
};

const int sensorPin1 = 0; //analog pin 0
const int ledPin1 = 9;   // LED1,   connected to digital pin 9
//int sensorValue1, sensorValue2, sensorValue3;

int ledVal1 = 0;
unsigned long lastFullBrightnessTimeL1 = 0;
const unsigned long fullBrightnessIntervalL1 = 5000;
unsigned long lastFadeTimeL1 = 0;

unsigned long lastFullBrightnessTimeL2 = 0;
const unsigned long fullBrightnessIntervalL2 = 5000;
unsigned long lastFadeTimeL2 = 0;


void setup() {
  Serial.begin(115200);
  pinMode(ledPin1, OUTPUT);
  //pinMode(ledPin2, OUTPUT);
// pinMode(ledPin3, OUTPUT);// sets the pins as output
  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 sensor1) {
  unsigned long currentMillis = millis();
  if (sensor1 <= 20) {  // Person in range
      if (ledVal1 <= 254)
        ledVal1++;
      else
        lastFullBrightnessTimeL1 = currentMillis;
    }
       
    else { // Person has moved away   
      if (ledVal1 >= 1 && currentMillis - lastFullBrightnessTimeL1 > fullBrightnessIntervalL1)
        ledVal1--;
    }

    if (sensor1 >= 20 && ledVal1 <= 1)
  { ledVal1 = 0;
    analogWrite(ledPin1, ledVal1);
  }   
   
    analogWrite(ledPin1, ledVal1);
 
  // The following code would be replaced with your code that does something with the ping result.
  Serial.println(sensor1);
  //Serial.print(" ");
  //Serial.print(cm);
  //Serial.println("cm");
}

teckel


I moved over to the Arduino base ping sensor and was getting to many fluctuations and problems with the built in delay... so I tried your version. Much better, but I am having issues with the LEDs. The fade kinda works however it is incredibly slow, and flickers and locks on and never goes off sometimes. The original fade, builds in fairly quickly and builds out after a set time if no ones around.

The whole installation works like this, as some one approaches the LED is triggered and it fades up, if you don't get inside the set distance then it fades back out, if you get inside or stand for a brief period of time the LED locks on for a set amount of time then if the sensor goes back to reading outside of the set distance the LED fades out but only after a set amount of time.


Probably best to first take a look at my Help for the 15 Sensors Example Sketch.  My guess is that you want to constantly make changes to the LED brightness and not just after a ping happens.  Better understanding and other example code for using the 15 sensor example sketch may help.  Without building your circuit, I can't really tell what you're trying to accomplish or why it's not working.  If the distances you are getting from the sensors using NewPing are correct, then the problem must be either with when you're executing the rest of the code or what your other code is doing.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

jahue

Quote
Probably best to first take a look at my Help for the 15 Sensors Example Sketch.  My guess is that you want to constantly make changes to the LED brightness and not just after a ping happens.  Better understanding and other example code for using the 15 sensor example sketch may help.  Without building your circuit, I can't really tell what you're trying to accomplish or why it's not working.  If the distances you are getting from the sensors using NewPing are correct, then the problem must be either with when you're executing the rest of the code or what your other code is doing.


Thanks, I'm still working on it, I have been trying to use the 15Sensor example. The circuit for this thing is dead simple... ping sensor and led. The biggest problems I am having is when a distance is hit lets say 100cm the led should fade up and lock on for a few seconds, if the 100cm is still triggered then the led stays full bright, if the 100cm goes back to 200 then the led holds for 5 seconds and then fades to off. The fade up is only suppose to last as long as it takes for the Arduino to ++ count to 255 but with the current code the ramp up to 255 is really slow.

Thanks for the help.

NeilM_CB24

I have downloaded the zip and unpacked it. I can see it in my Sketchbook folder as NewPing. When I load the NewPingExample sketch and try to compile I get the following errors:

Arduino: 1.5.4 (Windows 7), Board: "Arduino Uno"

C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=154 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard C:\Users\Neil\AppData\Local\Temp\build1329833015507385255.tmp\NewPingExample.cpp -o C:\Users\Neil\AppData\Local\Temp\build1329833015507385255.tmp\NewPingExample.cpp.o

NewPingExample:11: error: 'NewPing' does not name a type
NewPingExample.ino: In function 'void loop()':
NewPingExample:19: error: 'sonar' was not declared in this scope
NewPingExample:21: error: 'US_ROUNDTRIP_CM' was not declared in this scope

Can anyone tell what's wrong and what I need to do to fix it?

terryking228

Hi Neil,

Your NewPing library is not in the right place. It goes INSIDE the "libraries" folder in your sketchbook.

See the http://ArduinoInfo.Info WIKI and click on "Arduino Software Libraries"...



Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Antoan

I got a lot of bad readings. Is it normal?
Something like this....
Code: [Select]
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 185cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 186cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 187cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 185cm
Ping: 185cm
Ping: 180cm
Ping: 185cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 185cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm

dbinaz

Hi all,

Pretty new to this, but I got a Parallex PING today. It works with the old code, but with NewPing I get 0cm back no matter what.
Once I get this figured out, I want to try and get the 2 dual 7 segment LED's to work with it to use it as a parking guide.

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

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

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

void loop() {
  delay(50);
  int uS = sonar.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
}


Thx.

RbSCR

Code: [Select]
#define TRIGGER_PIN  12
#define ECHO_PIN     11


Why are you using different pinnumbers for the trigger- and echopin ?
The Parallex PING [at least the one I have] has a combined echo/trigger pin.

sfranzyshen

I had the need to change the max_cm_distance after the constructor ... so I added a couple of functions to handle this ...

unsigned int get_max_cm_distance()
void set_max_cm_distance(int max_cm_distance)

Code: [Select]

--- NewPing/NewPing.h 2012-08-15 18:06:40.000000000 -0700
+++ Arduino/libraries/NewPing/NewPing.h 2013-11-08 10:34:16.495017080 -0800
@@ -132,6 +132,8 @@
class NewPing {
public:
NewPing(uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance = MAX_SENSOR_DISTANCE);
+ unsigned int get_max_cm_distance();
+ void set_max_cm_distance(int max_cm_distance = MAX_SENSOR_DISTANCE);
unsigned int ping();
unsigned int ping_in();
unsigned int ping_cm();
@@ -152,6 +154,7 @@
volatile uint8_t *_triggerOutput;
volatile uint8_t *_triggerMode;
volatile uint8_t *_echoInput;
+ unsigned int _maxCMDistance;
unsigned int _maxEchoTime;
unsigned long _max_time;
static void timer_setup();
@@ -159,4 +162,4 @@
};

--- NewPing/NewPing.cpp 2012-08-15 18:01:10.000000000 -0700
+++ Arduino/libraries/NewPing/NewPing.cpp 2013-11-08 10:35:21.943341620 -0800
@@ -13,6 +13,7 @@
// ---------------------------------------------------------------------------

NewPing::NewPing(uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance) {
+ _maxCMDistance = min(max_cm_distance, MAX_SENSOR_DISTANCE);
_triggerBit = digitalPinToBitMask(trigger_pin); // Get the port register bitmask for the trigger pin.
_echoBit = digitalPinToBitMask(echo_pin);       // Get the port register bitmask for the echo pin.

@@ -21,13 +22,21 @@

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

- _maxEchoTime = min(max_cm_distance, MAX_SENSOR_DISTANCE) * US_ROUNDTRIP_CM + (US_ROUNDTRIP_CM / 2); // Calculate the maximum distance in uS.
+ _maxEchoTime = _maxCMDistance * US_ROUNDTRIP_CM + (US_ROUNDTRIP_CM / 2); // Calculate the maximum distance in uS.

#if DISABLE_ONE_PIN == true
*_triggerMode |= _triggerBit; // Set trigger pin to output.
#endif
}

+unsigned int NewPing::get_max_cm_distance() {
+ return (_maxCMDistance); //
+}
+
+void NewPing::set_max_cm_distance(int max_cm_distance) {
+ _maxCMDistance = min(max_cm_distance, MAX_SENSOR_DISTANCE);
+ _maxEchoTime = _maxCMDistance * US_ROUNDTRIP_CM + (US_ROUNDTRIP_CM / 2); // Calculate the maximum distance in uS.
+}

// ---------------------------------------------------------------------------
// Standard ping methods

makerman

I am having problems compiling v1.5 for the Arduino Due. It compiles and works great for Uno, Mega 2560, Yun and Leonardo.

I am using the 1.5.5 version of the IDE on Ubuntu 13.10.

Here is the result of the compile:
Code: [Select]
In file included from NewPingExample.ino:5:
/home/afy/sketchbook/libraries/NewPing/NewPing.h:113: fatal error: avr/io.h: No such file or directory
compilation terminated.


I realize that this is an IDE issue, but am asking if it has already been solved for others.

Thanks.

dene1988



  Nice job! I just got 4 of those sensors, and I have one hooked up at home. I will can't wait to try it out!


Hye can you send me the codes for using 4 sensors

Go Up