Issues with Ping Parallax Sensor

Hi guys, I’m having a bit of trouble getting data out of my Parallax Ping ultrasonic sensors. I’m using two, but testing with one, and I can’t get it to work.

It looks like from a quick search, some people have gotten that working in the past, such as https://community.spark.io/t/ping-sensor-code-not-working/2988

I used the pulseIn function that Tim B. made. My issue is that the cloud variables are either loading indefinitely or showing red “!” symbols. It’s hard to tell if this is a code problem or a sensor problem. Please help!

My code is a compilation of Tim B’s, and the Arduino sample code from their website:


int inchesReading = 0;
int centReading = 0;
int inchesReading1 = 0;
int centReading1 = 0;
// Create a variable to hold the light reading

int pingPin = D7;
int pingPin1 = D6;

void setup(){

  Spark.variable("distanceInches", &inchesReading, INT);
  Spark.variable("distanceCent", &centReading, INT);
  Spark.variable("distanceInches1", &inchesReading1, INT);
  Spark.variable("distanceCent1", &centReading1, INT);
}

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

  // 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);
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin1, 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);
  pinMode(pingPin1, INPUT);
  duration = pulseIn(pingPin, HIGH);
  duration1 = pulseIn(pingPin1, HIGH);

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

  delay(100);
}

unsigned long pulseIn(uint16_t pin, uint8_t state) {

    GPIO_TypeDef* portMask = (PIN_MAP[pin].gpio_peripheral); // Cache the target's peripheral mask to speed up the loops.
    uint16_t pinMask = (PIN_MAP[pin].gpio_pin); // Cache the target's GPIO pin mask to speed up the loops.
    unsigned long pulseCount = 0; // Initialize the pulseCount variable now to save time.
    unsigned long loopCount = 0; // Initialize the loopCount variable now to save time.
    unsigned long loopMax = 20000000; // Roughly just under 10 seconds timeout to maintain the Spark Cloud connection.

    // Wait for the pin to enter target state while keeping track of the timeout.
    while (GPIO_ReadInputDataBit(portMask, pinMask) != state) {
        if (loopCount++ == loopMax) {
            return 0;
        }
    }

    // Iterate the pulseCount variable each time through the loop to measure the pulse length; we also still keep track of the timeout.
    while (GPIO_ReadInputDataBit(portMask, pinMask) == state) {
        if (loopCount++ == loopMax) {
            return 0;
        }
        pulseCount++;
    }

    // Return the pulse time in microseconds by multiplying the pulseCount variable with the time it takes to run once through the loop.
    return pulseCount * 0.405; // Calculated the pulseCount++ loop to be about 0.405uS in length.
}

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: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  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;
}

Not sure about your original problem (could be a network or a timing problem).

But I would not perform interleaved sensing, due to its time sensitive nature. I’d rather put the complete sensing procedure for a single sensor in a function which takes the sensor pin as parameter and do one sensor after the other.

1 Like