Gas station without pumps

2011 September 23

Physics Lab 1: ultrasonic rangefinders

We had the first official meeting of the home-school physics class today.  It was just my son and me. Originally we were going to have a second student, but he was busy with college application essays, and asked if he could start next week instead.  My son compared his and my solutions to problems 1P89, 1P97, 1P98, and 1P117 (all the non-computational problems from Chapter 1 of Matter and Interactions).  We got all the same results, but I had left out the units on one of the intermediate steps on one problem—a bad habit that I will try to break, as I agree with John Burk that one should tell the full story of the number, throughout a computation.

In Physics Lab 1, I outlined the first experiment for my home-school physics students and me to do, using an ultrasonic rangefinder.

Here is what I proposed:

  1. Hook up the rangefinder to the Arduino and program the Arduino to keep taking measurements and reporting them to the serial line.
  2. Calibrate the sensor by placing it at carefully measured distances from a hard wall and recording the readings.  Repeat at several different distances.  (Record temperature, humidity, and barometric pressure, if possible.)
  3. Plot the sensor readings vs. the actual distance.
  4. Do linear regression to get predictor of actual distance given sensor reading.  (Caveat: need to plot distance vs. readings rather than readings vs. distance to get best fit for calibration.)
  5. Modify Arduino code to use the calibration parameters to provide better distance measurements.
  6. Re-calibrate using new code.  What is the accuracy and precision of the measurements?  What range of distances can be measured? Is the accuracy better expressed in terms of absolute error (±1cm, for example) or relative error (±5%, for example)?
  7. Open-ended: Experiment with detecting different targets (maybe flat targets from wall size down to the size of a quarter, maybe targets of different materials, maybe spherical targets).  What effect does target size, shape, material,  … have on range and accuracy of the measurement?

What we actually did:

My son and I each picked one of the rangefinders (I bought a Maxbotix LV-MaxSonar-EZ2 and a Ping))) sensor), and separately wrote Arduino code to read them. He chose to use the Maxbotix in the pulse-width mode, which is uncalibrated, but which has the greatest resolution.  I used the Ping))) sensor, which has a similar pulse-width mode.  The biggest difference is that the Ping))) needs to be triggered, while the Maxbotix repeats the measurement several times a second.  He had to solder a header onto the Maxbotix in order to connect it up, while I could cheat a little and plug the Ping directly into the Arduino board.  We could probably arrange to have both sensors on the Arduino at once, but have not tried that.

Both of us failed to get busy-wait loops with digitalRead() to work, but we both managed to get pulseIn() to work.  He just reported time in microseconds (then he had to go off to his improv class).  I spent a little more time adding a computed speed of sound (with temperature correction, but not humidity correction), to report distances in cm.  The repeatability of the Ping))) seems pretty good: about ±1mm, but I’ve not tried to calibrate the accuracy yet.  I don’t know whether the limit on the resolution is the coarseness of the pulseIn() measurement of time or variation in the pulse width output by the sensor.

For next week:

My son still needs to do more work on the prelab writeup about how an ultrasonic rangefinder works, so I hope that both students will have drafts of that for next week.  Also assigned for next week is a Vpython programming assignment (Exercise 1p123).

In lab next week we’ll compare our programs for 1p123, then do some calibration experiments with the rangefinder, or try using it to record a series of time and distance measurements for a moving object, or try learning to use Tracker to do video analysis.


  1. It sounds like a fun project. Let us know if you have any questions or if your son would like to speak to one of our techs.

    Great Job!

    Nick Ellenburg
    MaxBotix Inc. 218-454-0766 or email our tech Scott,

    Comment by Nick Ellenburg — 2011 October 13 @ 15:11 | Reply

    • We’ve done some work with both the Maxbotix and the Ping))) sensor now and found neither quite as reliable (in pulse-width mode) as we’d like. We were hoping to get repeatability to a millimeter for a stationary setup and were not able to with either sensor.

      Not only was there more noise in the measurement than we expected, we occasionally got readings that were way off, as if the first echo had been missed.

      We liked the beam patterns documented on
      but it would have been useful to have a theoretical explanation of how the gain is dynamically adjusted to shape the patterns. It would also be useful to have consumer-level comparisons between MaxBotix and Ping))) sensors, though I’m not sure I would trust a manufacturer to compare their design to a competitor’s.

      When we did a least-square fit to the maxbotix data, we did see a largish offset:
      distance = 0.0172 cm/usec * time_of_flight(usec) + 0.305 cm.
      Leaving out the first data point, which may have been too close at 10cm made the offset worse:
      distance = 0.0166 (cm/usec) * time_of_flight (usec) + 2.62 cm

      The deviations from the straight-line fit had an rmsd of about 1.85 cm with all data, and 0.87cm if the 10cm point is excluded.

      For the Ping))) sensor on a different day, I got 0.0173 *time_of_flight -0.926.

      In both cases the fitting routine claims that the speed of sound estimates are good, but that the offsets are dubious. So differences in distances are likely to be more reliable than absolute distances from the sensor.

      Comment by gasstationwithoutpumps — 2011 October 13 @ 16:42 | Reply

  2. What is the resolution of PING sensor?

    Comment by ffm — 2011 October 28 @ 23:22 | Reply

    • Since the Ping))) sensor provides a pulse width output, there are really two different answers to this question, depending on how it is interpreted:

      What resolution do you have on time measurement?
      Using the Arduino pulseIn measurement, one gets a time reported in microseconds, but the resolution seems to be about 3 microseconds. This translates to about 1mm resolution
      What repeatability does the Ping))) sensor have?
      We saw enormous variation in the pulse width, even with a fixed sensor and a hard wall at a constant distance. Counting on accuracy better than ±1cm seems foolish now. The problem is most likely from simple thresholding of the returned signal: small amounts of noise move the time at which the threshold is crossed.

      Comment by gasstationwithoutpumps — 2011 October 29 @ 08:49 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: