Ultrasonic Sensor HC-SR04P returning zero

Issue #75 resolved
Steven van Esch created an issue

For a project I’m using the NewPing 1.9.6 library to read 10 ultrasonic sensors using an Arduino Nano Every. I’ve altered the example code of 3 NewPing3Sensors to read 10 sensors instead but I’m getting only zero’s as output.

I’ve tested my setup using the PulseIn() method as well and that works.

Could it be that the difference between the HC-SR04 and the HC-SR04P is causing this problem?

This is the code I’ve used:

#include <NewPing.h>

#define SONAR_NUM     10 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 50 // 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(2, 12, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
    NewPing(3, 13, MAX_DISTANCE),
    NewPing(4, 14, MAX_DISTANCE),
    NewPing(5, 15, MAX_DISTANCE),
    NewPing(6, 16, MAX_DISTANCE),
    NewPing(7, 17, MAX_DISTANCE),
    NewPing(8, 18, MAX_DISTANCE),
    NewPing(9, 19, MAX_DISTANCE),
    NewPing(10, 20, MAX_DISTANCE),
    NewPing(11, 21, MAX_DISTANCE)
};

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}

void loop() { 
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
        delay(PING_INTERVAL); // 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(" ");
    }
    Serial.println();
}

Comments (6)

  1. Tim Eckel repo owner

    According to documentation, the HC-SR04P should operate the same, just allow a broader voltage range (3.3v to 5.0).

    First, I’d keep it simple if you’re having a problem, and that would mean using code with just one sensor and disconnect all but one sensor. Maybe it’s as simple as there’s not enough current to drive 10 sensors.

    Next, I would try changing #define ONE_PIN_ENABLED true to false in NewPing.h as you’re using different trigger and echo pins anyway.

    If that still doesn’t work, I’d like to see the PulseIn() code you’re using as a comparison.

    Finally, after you get things working, I’d suggest reconsidering using the ATMega4809 processor as it won’t work with any of the timer methods in NewPing. Code using blocking mode for 10 sensors may be a challenge (or impossible) to accomplish what the eventual goal of this circuit/program. See the 15 Sensors Sketch as a guide on how the code would be written to allow for pinging of 10 sensors using a more “event” driven technique. But, that shouldn’t be tried now, that’s only after everything is working, and it’s only if your eventual goal actually requires fast results.

  2. Steven van Esch reporter

    Issue is fixed and the solution was very stupid. When I took over the pins from my PulseIn() arduino code, I accidently switched the echo and trigger pin so it was my own fault after all. However, going back to one sensor and comparing it to my old code helped me find out my mistake so thanks for the tips and help!

    I do notice that it takes quite some time to read all the sensor using the blocking mode, it’s workable but I will try some different arduino’s with the event driven technique.

  3. Tim Eckel repo owner

    @Steven van Esch Glad you got it sorted out. Blocking mode will is not ideal when you have a lot of sensors. You may be able to make it work by doing some logic between each ping. But, if you need the results from many sensors before you can run logic, using the timer methods gives you more flexibility (albeit complicating the project).

  4. Log in to comment