No readings beyond 200cm

Issue #10 resolved
SdP created an issue

Arduino Mega 2560 R3 IDE 1.1.10 (on Raspberry PI 3) New Ping Library v1.8 - Released 07/30/2016

I use this code

#include <NewPing.h>

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

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

All readings beyond 200cm (I once saw a 209) are set as 0. readings up to 200cm are all OK.

Using with same wiring standard code with triggers sketch goes to 400cm (the theorical limit for HC-SR04 sensor)

Regards SdP

Comments (9)

  1. Tim Eckel repo owner

    Just tried it and with a max distance of 400 I get readings up to 400. What do you mean by "standard code with triggers"? Also, your code example is incomplete. Try using the Simple NewPing Sketch before trying to ping to multiple sensors. But, I'd need to see your full sketch, what the "standard code with triggers" is, and what sensor you're using to help further as it's working perfectly for me.

  2. SdP reporter

    This is the full code using NEW PING (no reading over 200cm with #define MAX_DISTANCE 400)

    // ---------------------------------------------------------------------------
    // Example NewPing library sketch that pings 3 sensors 20 times a second.
    // ---------------------------------------------------------------------------
    
    #include <NewPing.h>
    
    #define SONAR_NUM 2      // Number of sensors.
    #define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
    
    NewPing sonar[SONAR_NUM] = {   // Sensor object array.
      NewPing(8, 7, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
      NewPing(6, 5, MAX_DISTANCE), 
    };
    
    void setup() {
      Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
    }
    
    void loop() { 
      for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
        delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
        Serial.print(i);
        Serial.print("=");
        Serial.print(sonar[i].ping_cm());
        Serial.print("cm ");
      }
      Serial.println();
    }
    

    And this is the "standard" code using trigger and Echo This code works fine with 400cm same wiring as for the sample before

    /*
     HC-SR04 Ping distance sensor:
     VCC to arduino 5v 
     GND to arduino GND
     Echo to Arduino pin 7 
     Trig to Arduino pin 8
    
     This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
     Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
     And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
     on 10 Nov 2012.
     */
    
    
    #define echoPin 7 // Echo Pin
    #define trigPin 8 // Trigger Pin
    
    #define echoPin2 5 // Echo Pin
    #define trigPin2 6 // Trigger Pin
    
    #define LEDPin 13 // Onboard LED
    
    int maximumRange = 400; // Maximum range needed
    int minimumRange = 0; // Minimum range needed
    long duration, duration2, distance, distance2; // Duration used to calculate distance
    
    void setup() {
     Serial.begin (115200);
     pinMode(trigPin, OUTPUT);
     pinMode(echoPin, INPUT);
     pinMode(trigPin2, OUTPUT);
     pinMode(echoPin2, INPUT);
     pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
    }
    
    void loop() {
    /* The following trigPin/echoPin cycle is used to determine the
     distance of the nearest object by bouncing soundwaves off of it. */ 
     digitalWrite(trigPin, LOW);
     digitalWrite(trigPin2, LOW); 
     delayMicroseconds(2); 
    
     digitalWrite(trigPin, HIGH);
     digitalWrite(trigPin2, HIGH);
     delayMicroseconds(10); 
    
     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);
     digitalWrite(trigPin2, LOW);
     duration2 = pulseIn(echoPin2, HIGH);
    
     //Calculate the distance (in cm) based on the speed of sound.
     distance = duration/58.2;
     distance2 = duration2/58.2;
    
     if (distance >= maximumRange || distance <= minimumRange){
     /* Send a negative number to computer and Turn LED ON 
     to indicate "out of range" */
     Serial.println("-1");
     digitalWrite(LEDPin, HIGH); 
     }
     else {
     /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
     Serial.println(distance);
     digitalWrite(LEDPin, LOW); 
     }
    
     if (distance2 >= maximumRange || distance2 <= minimumRange){
     /* Send a negative number to computer and Turn LED ON 
     to indicate "out of range" */
     Serial.println("#2 -1");
     digitalWrite(LEDPin, HIGH); 
     }
     else {
     /* Send the distance to the computer using Serial protocol, and
     turn LED OFF to indicate successful reading. */
     Serial.print("#2 ");
     Serial.println(distance2);
     digitalWrite(LEDPin, LOW); 
     }
    
     //Delay 50ms before next reading.
     delay(50);
    }
    
  3. SdP reporter

    As you requested, I used also the Simple NewPing Sketch with New Ping Library, and IT DOES WORKS OK on 400cm

    // ---------------------------------------------------------------------------
    // Example NewPing library sketch that does a ping about 20 times per second.
    // ---------------------------------------------------------------------------
    
    #include <NewPing.h>
    
    #define TRIGGER_PIN  8  // 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 400 // 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(115200); // Open serial monitor at 115200 baud to see ping results.
    }
    
    void loop() {
      delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
      unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
      Serial.print("Ping: ");
      Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
      Serial.println("cm");
    }
    
  4. SdP reporter

    And the sensors I'm using are cheap made-in-china HC-SR04 (on the back there is HC-SR04+). I show the pictures in case you need some info about the chips.

    HC-SR04-FRONT.jpg

    HC-SR04-BACK.jpg

  5. SdP reporter

    And now it works...

    I'm wandering what happened.

    The sketch is the same.

    I re-uploaded to the board just before submitting the full code and the issue was there!

    I uploaded the standard code and all was ok

    I re-re-uploaded the sketch with new ping library and no readings longer than 200cm.

    This morning i re-re-re-uploaded the sketch with new ping library over the same code that was running all night long (no reading over 200cm) AND IT WAS WORKING, reading up to 400cm as supposed to do from the beginning!

    No code was touched, no wire was moved.

    SORRY FOR YOUR TIME!

    Please close the issue.

    SdP

  6. Tim Eckel repo owner

    Maybe the code running on the Arduino was at the 200cm default? That's the only thing I can think of. Anyway, glad it works, let me know if you have any other problems.

  7. Log in to comment