HC-SR04 NewPing.h: No such file or directory

Mac OSX 10.13
Elegoo Mega 2560 with HC-SR04

Hello,

This is my first post and I’m sure there’s some protocol I’m breaking so apologies in advance and I plan to get better. I’ve searched around the forum in advance for any existing topics and searched the web and YouTube for solutions and will continue to do so.

New to Arduino and to programming and I’m attempting to execute NewPing to test the ultrasonic sensor. I downloaded the library from teckel12 / Arduino New Ping / Downloads — Bitbucket

I made sure I selected the right controller and port in settings. However, when trying to execute the program and clicking “verify” I run into a “no such file or directory” error. I tried replacing NewPing.h with the full location extension “/Users/admin/Downloads/NewPing/examples/NewPing.h” but still the same error when compiling. I’m sure this is a quite basic issue and I will continue to self-educate. Thanks in advance.


Arduino: 1.8.13 (Mac OS X), Board: “Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)”

NewPingExample:10:1: error: ‘NewPing’ does not name a type; did you mean ‘rewind’?
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
^~~~~~~
rewind
/Users/admin/Downloads/NewPing/examples/NewPingExample/NewPingExample.ino: In function ‘void loop()’:
NewPingExample:19:16: error: ‘sonar’ was not declared in this scope
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
^~~~~
exit status 1
‘NewPing’ does not name a type; did you mean ‘rewind’?

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.


// ---------------------------------------------------------------------------

// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

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

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.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

You probably did not install the library properly. The best way to install a library is the use the IDE Library Manager. Fortunately the NewPing library is available through the Library Manager. This page has instructions on how to install a library with Library manager.

In the future, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Thanks for the quick response and the assistance. I'll give that a shot. Thanks for the nudge on proper posting as well.