Newping example not working with Analog pins

Every time I start it's something incredibly frustrating and basic like this...

I am attempting to use newping.h for ultrasonic HC SR04, on the A0 and A1 pins,

Works fine on other pins, I literally have previous code that used to work with the exact physical configuration, and it simply does not work 10 years later, not a THING has changed, did the library change somehow?
URF01 on the Sensor Shield v5 literally has these exact pins for this exact reason

Thank you so much for any insight you can offer

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

#include <NewPing.h>

#define TRIGGER_PIN  A0 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     A1 // 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.
  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 in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

It needs digital capable pins. You don't set digital pin modes in setup().

Not every Arduino has analog pins that are also digital capable.
On an Uno, analog pins can but not all of the other boards can.

When I used ping modules, I started with example code.. in 2012.

It is an Uno yes, code previously worked without having to declare

I added pinMode (TRIGGER_PIN, output); pinMode (ECHO_PIN, input);

and this is not enough to do this?

(Tried reversing them as well)

What I found in my IDE looks different than what i recall but I don't have what I used 10 or 12 years ago.

/*
  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
	- +V connection of the PING))) attached to +5V
	- GND connection of the PING))) attached to ground
	- SIG connection of the PING))) attached to digital pin 7

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping
*/

// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

I can tell that the speed of sound is first-order affected by air temperature.

@Jack777
Try this simple sketch
If it does not work, I suspect a hardware problem

// Jim

// Speed of sound in air at 25C is 343m/s
// or 0.343mm/us or 0.0343 cm/us

// Change to whatever pins you are using
#define trigger_pin A0
#define echo_pin A1

unsigned long echo_time;

void setup()
{
  pinMode (trigger_pin, OUTPUT);
  pinMode (echo_pin, INPUT);
  digitalWrite (trigger_pin, LOW);
  delay(2);

  Serial.begin(9600);
}

void loop()
{
  digitalWrite (trigger_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite (trigger_pin, LOW);
  echo_time = pulseIn (echo_pin, HIGH);
  Serial.println (echo_time);

  delay(500);

}
//

Hi @Jack777

Have you looked at the example from the current NewPing library and tried it?

// ---------------------------------------------------------------------------
// 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");
}

Try

sonar.ping_cm()

Tom.. :smiley: :+1: :coffee: :australia:

It just returns 0, confirming the analog pins do not seem to work on my arduino for this????

is there a way to test if analog pins are being set to digital? (I assumed the pinMode is doing just that)

sensor works fine on digital pins (triple checked, works on Any digital, not just the pwm ones (as expected)

(example from the current NewPing library is what I'm actually using, identical to code I posted except for weird serial notation. for some reason you cant google for newping example and find it quickly)

Assuming you had something in front of the sensor in order to get an echo, then yes something is wrong with A0, A1
Have you tried other analog pins, A2, A3 maybe?

pinMode essentially makes them digital.

Next thing to try is to see if they actually work as analog pins.

Connect a jumper between A0 and 3.3V
This program should print a value around 675


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

void loop()
{
  int x = analogRead(A0);
  Serial.println (x);
  delay (250);
}
//